Class: DnsMadeEasy::Credentials::ApiKeys

Inherits:
Object
  • Object
show all
Includes:
Sym
Defined in:
lib/dnsmadeeasy/credentials/api_keys.rb

Overview

Immutable instance with key and secret.

Constant Summary collapse

API_KEY_REGEX =
/^([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, secret, encryption_key = nil, default: false, account_name: nil) ⇒ ApiKeys

Returns a new instance of ApiKeys.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dnsmadeeasy/credentials/api_keys.rb', line 24

def initialize(key, secret, encryption_key = nil, default: false, account_name: nil)
  raise InvalidCredentialKeys, "Key and Secret can not be nil" if key.nil? || secret.nil?

  @default      = default
  @account_name = 

  if !valid?(key, secret) && encryption_key
    @encryption_key = sym_resolve(encryption_key)
    @api_key    = decr(key, @encryption_key)
    @api_secret = decr(secret, @encryption_key)
  else
    @api_key    = key
    @api_secret = secret
  end

  raise InvalidCredentialKeys, "Key [#{api_key}] or Secret [#{api_secret}] has failed validation for its format" unless valid?
end

Instance Attribute Details

#account_nameObject (readonly)

Returns the value of attribute account_name.



16
17
18
# File 'lib/dnsmadeeasy/credentials/api_keys.rb', line 16

def 
  @account_name
end

#api_keyObject (readonly)

Returns the value of attribute api_key.



16
17
18
# File 'lib/dnsmadeeasy/credentials/api_keys.rb', line 16

def api_key
  @api_key
end

#api_secretObject (readonly)

Returns the value of attribute api_secret.



16
17
18
# File 'lib/dnsmadeeasy/credentials/api_keys.rb', line 16

def api_secret
  @api_secret
end

#defaultObject (readonly)

Returns the value of attribute default.



16
17
18
# File 'lib/dnsmadeeasy/credentials/api_keys.rb', line 16

def default
  @default
end

#encryption_keyObject (readonly)

Returns the value of attribute encryption_key.



16
17
18
# File 'lib/dnsmadeeasy/credentials/api_keys.rb', line 16

def encryption_key
  @encryption_key
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



67
68
69
70
71
72
# File 'lib/dnsmadeeasy/credentials/api_keys.rb', line 67

def ==(other)
  other.is_a?(ApiKeys) &&
    other.valid? &&
    other.api_key == api_key &&
    other.api_secret == api_secret
end

#rofl(key) ⇒ Object



56
57
58
# File 'lib/dnsmadeeasy/credentials/api_keys.rb', line 56

def rofl(key)
  Digest::SHA256::hexdigest(key) if key
end

#sym_resolve(encryption_key) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/dnsmadeeasy/credentials/api_keys.rb', line 42

def sym_resolve(encryption_key)
  null_output = ::File.open('/dev/null', 'w')
  result = Sym::Application.new({ cache_passwords: true, key: encryption_key }, $stdin, null_output, null_output).execute
  if result.is_a?(Hash)
    raise InvalidCredentialKeys, "Unable to decrypt the data, error is: #{result[:exception]}"
  else
    result
  end
end

#to_sObject



52
53
54
# File 'lib/dnsmadeeasy/credentials/api_keys.rb', line 52

def to_s
  "<#{self.class.name}#key=[s#{rofl(api_key)}] secret=[#{rofl(api_secret)}] encryption_key=[#{rofl(encryption_key)}]>"
end

#valid?(key = self.api_key, secret = self.api_secret) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
# File 'lib/dnsmadeeasy/credentials/api_keys.rb', line 60

def valid?(key = self.api_key, secret = self.api_secret)
  key &&
    secret &&
    API_KEY_REGEX.match(key) &&
    API_KEY_REGEX.match(secret)
end