Module: Enviso::Authentication

Defined in:
lib/enviso/authentication.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.auth_tokenObject

Returns the value of attribute auth_token.



9
10
11
# File 'lib/enviso/authentication.rb', line 9

def auth_token
  @auth_token
end

.refresh_tokenObject

Returns the value of attribute refresh_token.



10
11
12
# File 'lib/enviso/authentication.rb', line 10

def refresh_token
  @refresh_token
end

Class Method Details

.api_key_valid_untilObject



98
99
100
101
# File 'lib/enviso/authentication.rb', line 98

def api_key_valid_until
  decoded_token = JWT.decode self.auth_token, nil, false
  Time.at(decoded_token.first["exp"])
end

.calculate_login_signature(timestamp) ⇒ Object

  1. Create a SHA256 hash of this value. Based on the hashed value, a digital signature will be created.



33
34
35
36
37
# File 'lib/enviso/authentication.rb', line 33

def (timestamp)
	api_key = Enviso::Config.api_key
	return Digest::SHA256.hexdigest("#{api_key}_#{timestamp}")
   # return OpenSSL::Digest::SHA256.new("#{api_key}_#{timestamp}")
end

.get_new_tokenObject

Get new access token using refresh token from login request response



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/enviso/authentication.rb', line 77

def get_new_token
  body = { refreshToken: refresh_token }
  puts "Refreshing token" if Enviso::Config.verbose
  begin
    result = Enviso::API.send_request(type: :post, endpoint: "apis/getnewtoken", body: body)
    if result["token"]
      self.auth_token = result["token"]
    end
    return result
  rescue
    puts "Signing in again to refresh token" if Enviso::Config.verbose
    
  end
end

.has_valid_api_keyObject

Parses the current API key and checks if the ‘exp’ attribute represents a date in the future.



94
95
96
# File 'lib/enviso/authentication.rb', line 94

def has_valid_api_key
  return self.auth_token != nil && api_key_valid_until > Time.now
end

.init!Hash

Set’s the default value’s to nil and false

Returns:

  • (Hash)

    configuration options



14
15
16
17
18
19
# File 'lib/enviso/authentication.rb', line 14

def init!
  @defaults = {
    :@auth_token    => nil,
    :@refresh_token => nil
  }
end

.login(signed_key: nil) ⇒ Object

This method combines all the login steps

  1. generate a key (SHA256)

  2. signs the key with public_key

  3. sends request to API endpoint



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/enviso/authentication.rb', line 56

def (signed_key: nil)
  self.auth_token  = nil # reset to prevent giving auth token in login request headers
  
  timestamp   = timestamp_for_authentication
  key         = (timestamp)
  signed_key  = (key) unless signed_key

  body = {
    apikey: Enviso::Config.api_key,
    timestamp: timestamp,
    signature: signed_key
  }
  result = Enviso::API.send_request(type: :post, endpoint: "apis/login", body: body)
  if result["authToken"]
    self.refresh_token = result["refreshToken"]
    self.auth_token = result["authToken"]
  end
  return result
end

.sign_login_signature(key) ⇒ Object

From the authentication documentation, chapter 4.1:

  1. The combination of the created signature along with the provided API secret key will act as the digital signature of the call.



44
45
46
47
48
49
# File 'lib/enviso/authentication.rb', line 44

def (key)
   secret = Enviso::Config.api_secret
   public_key = OpenSSL::PKey::RSA.new(secret)
   encrypted_string = Base64.encode64(public_key.public_encrypt(key))
   return encrypted_string
end

.timestamp_for_authenticationObject

in the authentication documentation, chapter 4.1:

  1. Concatenate the API key with the current timestamp in the format below: <<APIKEY>>_<<timestamp(yyyy’-‘MM’-‘ddTHH:mm:ss.fffZ)>>

NOTE: The timestamp is in ISO 8601



26
27
28
# File 'lib/enviso/authentication.rb', line 26

def timestamp_for_authentication
  return Time.now.utc.strftime('%Y-%m-%dT%H:%M:%S.%LZ')
end