Class: Marginalia::IO::Auth
- Inherits:
-
Object
- Object
- Marginalia::IO::Auth
- Defined in:
- lib/marginalia-io/auth.rb
Instance Attribute Summary collapse
-
#credentials ⇒ Object
Returns the value of attribute credentials.
-
#host ⇒ Object
Returns the value of attribute host.
Instance Method Summary collapse
- #delete_credentials ⇒ Object
-
#get_credentials ⇒ Object
:nodoc:.
-
#initialize(host = "www.marginalia.io") ⇒ Auth
constructor
A new instance of Auth.
- #login ⇒ Object
-
#netrc ⇒ Object
:nodoc:.
- #netrc_path ⇒ Object
- #read_credentials ⇒ Object
- #write_credentials ⇒ Object
Constructor Details
#initialize(host = "www.marginalia.io") ⇒ Auth
Returns a new instance of Auth.
6 7 8 |
# File 'lib/marginalia-io/auth.rb', line 6 def initialize(host="www.marginalia.io") @host = host end |
Instance Attribute Details
#credentials ⇒ Object
Returns the value of attribute credentials.
4 5 6 |
# File 'lib/marginalia-io/auth.rb', line 4 def credentials @credentials end |
#host ⇒ Object
Returns the value of attribute host.
4 5 6 |
# File 'lib/marginalia-io/auth.rb', line 4 def host @host end |
Instance Method Details
#delete_credentials ⇒ Object
37 38 39 40 41 42 |
# File 'lib/marginalia-io/auth.rb', line 37 def delete_credentials if netrc netrc.delete(host) netrc.save end end |
#get_credentials ⇒ Object
:nodoc:
20 21 22 |
# File 'lib/marginalia-io/auth.rb', line 20 def get_credentials # :nodoc: @credentials ||= (read_credentials || login) end |
#login ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/marginalia-io/auth.rb', line 58 def login print "Email address: " username = gets print "Password: " system "stty -echo" password = gets system "stty echo" self.credentials = [username, password] write_credentials self.credentials end |
#netrc ⇒ Object
:nodoc:
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/marginalia-io/auth.rb', line 24 def netrc # :nodoc: @netrc ||= begin File.exists?(netrc_path) && Netrc.read(netrc_path) rescue => error if error. =~ /^Permission bits for/ perm = File.stat(netrc_path).mode & 0777 abort("Permissions #{perm} for '#{netrc_path}' are too open. You should run `chmod 0600 #{netrc_path}` so that your credentials are NOT accessible by others.") else raise error end end end |
#netrc_path ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/marginalia-io/auth.rb', line 10 def netrc_path default = Netrc.default_path encrypted = default + ".gpg" if File.exists?(encrypted) encrypted else default end end |
#read_credentials ⇒ Object
44 45 46 47 48 |
# File 'lib/marginalia-io/auth.rb', line 44 def read_credentials if netrc netrc[host] end end |
#write_credentials ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/marginalia-io/auth.rb', line 50 def write_credentials FileUtils.mkdir_p(File.dirname(netrc_path)) FileUtils.touch(netrc_path) FileUtils.chmod(0600, netrc_path) netrc[host] = self.credentials netrc.save end |