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.



36
37
38
# File 'lib/asana/authentication/oauth2/access_token_authentication.rb', line 36

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:



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

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.



46
47
48
49
50
# File 'lib/asana/authentication/oauth2/access_token_authentication.rb', line 46

def configure(connection)
  @token = @token.refresh! if @token.expired?

  connection.request :authorization, 'Bearer', @token.token
end