Module: PaylocityWebService::Authentication

Included in:
Client, Connection
Defined in:
lib/paylocity_web_service/authentication.rb

Instance Method Summary collapse

Instance Method Details

#access_tokenObject



7
8
9
10
11
12
13
14
15
# File 'lib/paylocity_web_service/authentication.rb', line 7

def access_token
  token = PaylocityWebService::Cache.read(access_token_cache_key)

  if token.nil?
    refresh_token
  else
    token
  end
end

#access_token_cache_keyObject



36
37
38
# File 'lib/paylocity_web_service/authentication.rb', line 36

def access_token_cache_key
  'access_token_cache_key'
end

#basic_auth_tokenObject



40
41
42
43
44
45
46
47
# File 'lib/paylocity_web_service/authentication.rb', line 40

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

#refresh_tokenObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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']
  PaylocityWebService::Cache.write(access_token_cache_key, token, expires_in)
  token
end