Module: Formi9::OAuth
- Included in:
- API
- Defined in:
- lib/formi9/oauth.rb
Instance Method Summary collapse
- #access_token ⇒ Object
- #access_token_cache_key ⇒ Object
- #reset_token ⇒ Object
- #token_cache_duration(timestamp) ⇒ Object
Instance Method Details
#access_token ⇒ Object
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/formi9/oauth.rb', line 5 def access_token token = Rails.cache.read(access_token_cache_key) if token.present? return token else reset_token Rails.cache.read(access_token_cache_key) end end |
#access_token_cache_key ⇒ Object
26 27 28 |
# File 'lib/formi9/oauth.rb', line 26 def access_token_cache_key Digest::MD5.hexdigest([partner_id, username, password].join) end |
#reset_token ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/formi9/oauth.rb', line 16 def reset_token connection = Faraday.new endpoint, :ssl => {:verify => ssl_verify } response = connection.post('login', {id: partner_id, username: username, password: password} ) raise CredentialAreInvalid.new('Credentials are missing or invalid.') if response.status != 200 body = JSON.parse(response.body) if Rails.cache.read(access_token_cache_key).nil? Rails.cache.write(access_token_cache_key, body['accessToken'], expires_in: token_cache_duration(body['expirationDateUtc'])) end end |
#token_cache_duration(timestamp) ⇒ Object
30 31 32 33 34 |
# File 'lib/formi9/oauth.rb', line 30 def token_cache_duration() max = 6.days.to_i duration = .to_datetime.utc - Time.now duration > max ? max : duration end |