Module: Enviso::Authentication
- Defined in:
- lib/enviso/authentication.rb
Class Attribute Summary collapse
-
.auth_token ⇒ Object
Returns the value of attribute auth_token.
-
.refresh_token ⇒ Object
Returns the value of attribute refresh_token.
Class Method Summary collapse
- .api_key_valid_until ⇒ Object
-
.calculate_login_signature(timestamp) ⇒ Object
2.
-
.get_new_token ⇒ Object
Get new access token using refresh token from login request response.
-
.has_valid_api_key ⇒ Object
Parses the current API key and checks if the ‘exp’ attribute represents a date in the future.
-
.init! ⇒ Hash
Set’s the default value’s to nil and false.
-
.login(signed_key: nil) ⇒ Object
This method combines all the login steps 1.
-
.sign_login_signature(key) ⇒ Object
From the authentication documentation, chapter 4.1: 3.
-
.timestamp_for_authentication ⇒ Object
in the authentication documentation, chapter 4.1: 1.
Class Attribute Details
.auth_token ⇒ Object
Returns the value of attribute auth_token.
9 10 11 |
# File 'lib/enviso/authentication.rb', line 9 def auth_token @auth_token end |
.refresh_token ⇒ Object
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_until ⇒ Object
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
-
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 calculate_login_signature() api_key = Enviso::Config.api_key return Digest::SHA256.hexdigest("#{api_key}_#{}") # return OpenSSL::Digest::SHA256.new("#{api_key}_#{timestamp}") end |
.get_new_token ⇒ Object
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 login end end |
.has_valid_api_key ⇒ Object
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
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
-
generate a key (SHA256)
-
signs the key with public_key
-
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 login(signed_key: nil) self.auth_token = nil # reset to prevent giving auth token in login request headers = key = calculate_login_signature() signed_key = sign_login_signature(key) unless signed_key body = { apikey: Enviso::Config.api_key, 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:
-
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 sign_login_signature(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_authentication ⇒ Object
in the authentication documentation, chapter 4.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 return Time.now.utc.strftime('%Y-%m-%dT%H:%M:%S.%LZ') end |