Class: Smartdc::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/smartdc/auth.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Auth

Returns a new instance of Auth.



7
8
9
10
11
# File 'lib/smartdc/auth.rb', line 7

def initialize(options={})
  @username = options[:username]
  @rsa_path = options[:rsa_path]
  @use_key = options[:use_key]
end

Instance Attribute Details

#rsa_pathObject (readonly)

Returns the value of attribute rsa_path.



5
6
7
# File 'lib/smartdc/auth.rb', line 5

def rsa_path
  @rsa_path
end

#use_keyObject (readonly)

Returns the value of attribute use_key.



5
6
7
# File 'lib/smartdc/auth.rb', line 5

def use_key
  @use_key
end

#usernameObject (readonly)

Returns the value of attribute username.



5
6
7
# File 'lib/smartdc/auth.rb', line 5

def username
  @username
end

Instance Method Details

#find_key_fileObject



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/smartdc/auth.rb', line 13

def find_key_file
  path = File.expand_path(self.rsa_path)
  if File.directory?(path)
    Dir[File.join(path, '*')].each do |path|
      return path if File.file?(path) and self.use_key == self.fingerprint(path)
    end
    return ''
  else
    return path
  end
end

#fingerprint(path) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/smartdc/auth.rb', line 38

def fingerprint(path)
  rsa = read_key_file(path)
  str = [7].pack('N') + 'ssh-rsa' + rsa.public_key.e.to_s(0) + rsa.public_key.n.to_s(0)
  OpenSSL::Digest::MD5.hexdigest(str).scan(/../).join(':')
rescue
  ''
end

#read_key_file(path) ⇒ Object



25
26
27
# File 'lib/smartdc/auth.rb', line 25

def read_key_file(path)
  OpenSSL::PKey::RSA.new(File.read(File.expand_path(path)), '')
end

#signature(data) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/smartdc/auth.rb', line 29

def signature(data)
  rsa = read_key_file(self.find_key_file)
  sha256 = OpenSSL::Digest::SHA256.new
  str = [rsa.sign(sha256, data)].pack('m').delete("\r\n")
  "Signature keyId=\"/#{self.username}/keys/#{self.use_key}\",algorithm=\"rsa-sha256\" #{str}"
rescue
  ''
end