class Remotus::Auth::Credential

Authentication credential

Attributes

private_key[RW]

@return [String] gets or sets private key path

user[RW]

@return [String] gets or sets user

Public Class Methods

from_hash(hash) click to toggle source

Generates a new credential from a hash

@param [Hash] hash hash with :user, :password, :private_key, and :private_key_data keys @option hash [String] :user user name @option hash [String] :password user password @option hash [String] :private_key private key path @option hash [String] :private_key_data private key data as a string

@return [Remotus::Auth::Credential] newly initialized credential

# File lib/remotus/auth/credential.rb, line 26
def self.from_hash(hash)
  Credential.new(
    hash[:user],
    hash[:password],
    private_key: hash[:private_key],
    private_key_data: hash[:private_key_data]
  )
end
new(user, password = nil, **options) click to toggle source

Creates a new instance of a Remotus::Auth::Credential

@param [String] user user name @param [String] password user password @param [Hash] options options hash @option options [String] :private_key private key path @option options [String] :private_key_data private key data as a string

# File lib/remotus/auth/credential.rb, line 44
def initialize(user, password = nil, **options)
  @user = user
  @crypt_info = { password: {}, private_key_data: {} }
  @private_key = options[:private_key]
  self.password = password
  self.private_key_data = options[:private_key_data]
end

Public Instance Methods

inspect() click to toggle source

Inspects credential

@return [String] Credential represented as an inspection string

# File lib/remotus/auth/credential.rb, line 106
def inspect
  "#{self.class.name}: (#{self})"
end
password() click to toggle source

Retrieved decrypted password

@return [String, nil] decrypted password or nil if unset

# File lib/remotus/auth/credential.rb, line 57
def password
  return unless @password

  decrypt(@password, :password)
end
password=(password) click to toggle source

Sets password

@param [String] password new password

# File lib/remotus/auth/credential.rb, line 68
def password=(password)
  @password = password ? encrypt(password.to_s, :password) : nil
end
private_key_data() click to toggle source

Retrieves decrypted private key data

@return [String, nil] decrypted private key data or nil if unset

# File lib/remotus/auth/credential.rb, line 77
def private_key_data
  return unless @private_key_data

  decrypt(@private_key_data, :private_key_data)
end
private_key_data=(private_key_data) click to toggle source

Sets private key data

@param [String] private_key_data private key data

# File lib/remotus/auth/credential.rb, line 88
def private_key_data=(private_key_data)
  @private_key_data = private_key_data ? encrypt(private_key_data.to_s, :private_key_data) : nil
end
to_s() click to toggle source

Converts credential to a string

@return [String] Credential represented as a string

# File lib/remotus/auth/credential.rb, line 97
def to_s
  "user: #{@user}"
end

Private Instance Methods

decrypt(data, crypt_key) click to toggle source

Decrypts data to a string

@param [Object] data encrypted data @param [Symbol] crypt_key key in @crypt_info containing the key and iv for decryption

@return [String] decrypted string

# File lib/remotus/auth/credential.rb, line 136
def decrypt(data, crypt_key)
  decipher = OpenSSL::Cipher.new("aes-256-cbc")
  decipher.decrypt
  decipher.key = @crypt_info[crypt_key][:key]
  decipher.iv = @crypt_info[crypt_key][:iv]
  decipher.update(data) + decipher.final
end
encrypt(data, crypt_key) click to toggle source

Encrypts string data

@param [String] data data to encrypt @param [Symbol] crypt_key key in @crypt_info to store the key and iv for decryption

@return [Object] encrypted data

# File lib/remotus/auth/credential.rb, line 120
def encrypt(data, crypt_key)
  cipher = OpenSSL::Cipher.new("aes-256-cbc")
  cipher.encrypt
  @crypt_info[crypt_key][:key] = cipher.random_key
  @crypt_info[crypt_key][:iv] = cipher.random_iv
  cipher.update(data) + cipher.final
end