class Smartdc::Auth

Attributes

rsa_path[R]
use_key[R]
username[R]

Public Class Methods

new(options={}) click to toggle source
# File lib/smartdc/auth.rb, line 7
def initialize(options={})
  @username = options[:username]
  @rsa_path = options[:rsa_path]
  @use_key = options[:use_key]
end

Public Instance Methods

find_key_file() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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