Module: PaylocityWebService::Authentication
- Included in:
- Client, Connection
- Defined in:
- lib/paylocity_web_service/authentication.rb
Instance Method Summary collapse
- #access_token ⇒ Object
- #access_token_cache_key ⇒ Object
- #basic_auth_token ⇒ Object
- #cache_store ⇒ Object
- #refresh_token ⇒ Object
Instance Method Details
#access_token ⇒ Object
7 8 9 10 11 12 13 14 15 |
# File 'lib/paylocity_web_service/authentication.rb', line 7 def access_token token = cache_store.read(access_token_cache_key) if token.nil? refresh_token else token end end |
#access_token_cache_key ⇒ Object
35 36 37 38 |
# File 'lib/paylocity_web_service/authentication.rb', line 35 def access_token_cache_key raise 'client_id is required' if client_id.nil? "PaylocityClientID/#{client_id}/AccessToken/#{endpoint}" end |
#basic_auth_token ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/paylocity_web_service/authentication.rb', line 49 def basic_auth_token secret_value = if client_secret.is_a?(Proc) client_secret.call else client_secret end Base64.strict_encode64("#{ client_id }:#{ secret_value }") end |
#cache_store ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/paylocity_web_service/authentication.rb', line 41 def cache_store if defined?(Rails) Rails.cache else PaylocityWebService::Cache end end |
#refresh_token ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/paylocity_web_service/authentication.rb', line 17 def refresh_token resp = Faraday.post(endpoint + '/IdentityServer/connect/token') do |req| req.body = { grant_type: 'client_credentials', scope: 'WebLinkAPI' } req.headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'Authorization': "Basic #{ basic_auth_token }" } end body = JSON.parse(resp.body) token, expires_in = body['access_token'], body['expires_in'] cache_store.write(access_token_cache_key, token, expires_in: expires_in) token end |