Class: Procore::Auth::AccessTokenCredentials

Inherits:
Object
  • Object
show all
Defined in:
lib/procore/auth/access_token_credentials.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id:, client_secret:, host:) ⇒ AccessTokenCredentials

Returns a new instance of AccessTokenCredentials.



7
8
9
10
11
# File 'lib/procore/auth/access_token_credentials.rb', line 7

def initialize(client_id:, client_secret:, host:)
  @client_id = client_id
  @client_secret = client_secret
  @host = host
end

Instance Attribute Details

#client_idObject (readonly)

Returns the value of attribute client_id.



6
7
8
# File 'lib/procore/auth/access_token_credentials.rb', line 6

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



6
7
8
# File 'lib/procore/auth/access_token_credentials.rb', line 6

def client_secret
  @client_secret
end

#hostObject (readonly)

Returns the value of attribute host.



6
7
8
# File 'lib/procore/auth/access_token_credentials.rb', line 6

def host
  @host
end

Instance Method Details

#refresh(token:, refresh:) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/procore/auth/access_token_credentials.rb', line 13

def refresh(token:, refresh:)
  begin
    token = OAuth2::AccessToken.new(client, token, refresh_token: refresh)
    new_token = token.refresh!

    Procore::Auth::Token.new(
      access_token: new_token.token,
      refresh_token: new_token.refresh_token,
      expires_at: new_token.expires_at,
    )
  rescue OAuth2::Error => e
    raise OAuthError.new(e.description, response: e.response)
  rescue Faraday::ConnectionFailed => e
    raise APIConnectionError.new("Connection Error: #{e.message}")
  rescue URI::BadURIError
    raise OAuthError.new(
      "Host is not a valid URI. Check your host option to make sure it " \
      "is a properly formed url",
    )
  end
end

#revoke(token:) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/procore/auth/access_token_credentials.rb', line 35

def revoke(token:)
  request = {
    client_id: @client_id,
    client_secret: @client_secret,
    token: token.access_token,
  }
  client.request(:post, "/oauth/revoke", body: request)
rescue RestClient::ExceptionWithResponse
  raise OAuthError.new(e.description, response: e.response)
end