Class: Remotus::Auth::Credential

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

Overview

Authentication credential

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, password = nil, **options) ⇒ Credential

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

Parameters:

  • user (String)

    user name

  • password (String) (defaults to: nil)

    user password

  • options (Hash)

    options hash

Options Hash (**options):

  • :private_key (String)

    private key path

  • :private_key_data (String)

    private key data as a string



44
45
46
47
48
49
50
# 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

Instance Attribute Details

#private_keyString

Returns gets or sets private key path.

Returns:

  • (String)

    gets or sets private key path



13
14
15
# File 'lib/remotus/auth/credential.rb', line 13

def private_key
  @private_key
end

#userString

Returns gets or sets user.

Returns:

  • (String)

    gets or sets user



10
11
12
# File 'lib/remotus/auth/credential.rb', line 10

def user
  @user
end

Class Method Details

.from_hash(hash) ⇒ Remotus::Auth::Credential

Generates a new credential from a hash

Parameters:

  • hash (Hash)

    hash with :user, :password, :private_key, and :private_key_data keys

Options Hash (hash):

  • :user (String)

    user name

  • :password (String)

    user password

  • :private_key (String)

    private key path

  • :private_key_data (String)

    private key data as a string

Returns:



26
27
28
29
30
31
32
33
# 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

Instance Method Details

#inspectString

Inspects credential

Returns:

  • (String)

    Credential represented as an inspection string



106
107
108
# File 'lib/remotus/auth/credential.rb', line 106

def inspect
  "#{self.class.name}: (#{self})"
end

#passwordString?

Retrieved decrypted password

Returns:

  • (String, nil)

    decrypted password or nil if unset



57
58
59
60
61
# File 'lib/remotus/auth/credential.rb', line 57

def password
  return unless @password

  decrypt(@password, :password)
end

#password=(password) ⇒ Object

Sets password

Parameters:

  • password (String)

    new password



68
69
70
# File 'lib/remotus/auth/credential.rb', line 68

def password=(password)
  @password = password ? encrypt(password.to_s, :password) : nil
end

#private_key_dataString?

Retrieves decrypted private key data

Returns:

  • (String, nil)

    decrypted private key data or nil if unset



77
78
79
80
81
# 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) ⇒ Object

Sets private key data

Parameters:

  • private_key_data (String)

    private key data



88
89
90
# 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_sString

Converts credential to a string

Returns:

  • (String)

    Credential represented as a string



97
98
99
# File 'lib/remotus/auth/credential.rb', line 97

def to_s
  "user: #{@user}"
end