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: nil) ⇒ ApiKeys

Returns a new instance of ApiKeys.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/dnsmadeeasy/credentials/api_keys.rb', line 26

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

  @default = default
  @account = 

  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

  return if valid?

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

Instance Attribute Details

#accountObject (readonly)

Returns the value of attribute account.



18
19
20
# File 'lib/dnsmadeeasy/credentials/api_keys.rb', line 18

def 
  @account
end

#api_keyObject (readonly)

Returns the value of attribute api_key.



18
19
20
# File 'lib/dnsmadeeasy/credentials/api_keys.rb', line 18

def api_key
  @api_key
end

#api_secretObject (readonly)

Returns the value of attribute api_secret.



18
19
20
# File 'lib/dnsmadeeasy/credentials/api_keys.rb', line 18

def api_secret
  @api_secret
end

#defaultObject (readonly)

Returns the value of attribute default.



18
19
20
# File 'lib/dnsmadeeasy/credentials/api_keys.rb', line 18

def default
  @default
end

#encryption_keyObject (readonly)

Returns the value of attribute encryption_key.



18
19
20
# File 'lib/dnsmadeeasy/credentials/api_keys.rb', line 18

def encryption_key
  @encryption_key
end

Instance Method Details

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



71
72
73
74
75
76
# File 'lib/dnsmadeeasy/credentials/api_keys.rb', line 71

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

#rofl(key) ⇒ Object



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

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

#sym_resolve(encryption_key) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/dnsmadeeasy/credentials/api_keys.rb', line 47

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
  raise InvalidCredentialKeys, "Unable to decrypt the data, error is: #{result[:exception]}" if result.is_a?(Hash)

  result
end

#to_sObject



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

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

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

Returns:

  • (Boolean)


64
65
66
67
68
69
# File 'lib/dnsmadeeasy/credentials/api_keys.rb', line 64

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