Class: Asana::Authentication::OAuth2::AccessTokenAuthentication

Inherits:
Object
  • Object
show all
Defined in:
lib/asana/authentication/oauth2/access_token_authentication.rb

Overview

A mechanism to authenticate with an OAuth2 access token (a bearer token and a refresh token) or just a refresh token.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token) ⇒ AccessTokenAuthentication

Initializes a new AccessTokenAuthentication.

Parameters:

  • access_token (::OAuth2::AccessToken)

    An ::OAuth2::AccessToken object.


34
35
36
# File 'lib/asana/authentication/oauth2/access_token_authentication.rb', line 34

def initialize(access_token)
  @token = access_token
end

Class Method Details

.from_refresh_token(refresh_token, client_id: required('client_id'), client_secret: required('client_secret'), redirect_uri: required('redirect_uri')) ⇒ AccessTokenAuthentication

Builds an AccessTokenAuthentication from a refresh token and client credentials, by refreshing into a new one.

Parameters:

  • refresh_token (String)

    a refresh token

  • client_id (String) (defaults to: required('client_id'))

    the client id of the registered Asana API Application.

  • client_secret (String) (defaults to: required('client_secret'))

    the client secret of the registered Asana API Application.

  • redirect_uri (String) (defaults to: required('redirect_uri'))

    the redirect uri of the registered Asana API Application.

Returns:


20
21
22
23
24
25
26
27
28
# File 'lib/asana/authentication/oauth2/access_token_authentication.rb', line 20

def self.from_refresh_token(refresh_token,
                            client_id: required('client_id'),
                            client_secret: required('client_secret'),
                            redirect_uri: required('redirect_uri'))
  client = Client.new(client_id: client_id,
                      client_secret: client_secret,
                      redirect_uri: redirect_uri)
  new(client.token_from_refresh_token(refresh_token))
end

Instance Method Details

#configure(connection) ⇒ void

This method returns an undefined value.

Configures a Faraday connection injecting a bearer token, auto-refreshing it when needed.

Parameters:

  • connection (Faraday::Connection)

    the Faraday connection instance.


44
45
46
47
# File 'lib/asana/authentication/oauth2/access_token_authentication.rb', line 44

def configure(connection)
  @token = @token.refresh! if @token.expired?
  connection.request :oauth2, @token.token
end