Class: Marginalia::IO::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/marginalia-io/auth.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#credentialsObject

Returns the value of attribute credentials.



4
5
6
# File 'lib/marginalia-io/auth.rb', line 4

def credentials
  @credentials
end

#hostObject

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_credentialsObject



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_credentialsObject

:nodoc:



20
21
22
# File 'lib/marginalia-io/auth.rb', line 20

def get_credentials    # :nodoc:
  @credentials ||= (read_credentials || )
end

#loginObject



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/marginalia-io/auth.rb', line 58

def 
  print "Email address: "
  username = gets
  print "Password: "
  system "stty -echo"
  password = gets
  system "stty echo"

  self.credentials = [username, password]
  write_credentials
  self.credentials
end

#netrcObject

: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.message =~ /^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_pathObject



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_credentialsObject



44
45
46
47
48
# File 'lib/marginalia-io/auth.rb', line 44

def read_credentials
  if netrc
    netrc[host]
  end
end

#write_credentialsObject



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