Class: Asana::Authentication::OAuth2::Client

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

Overview

Deals with the details of obtaining an OAuth2 authorization URL and obtaining access tokens from either authorization codes or refresh tokens.

Instance Method Summary collapse

Constructor Details

#initialize(client_id: required('client_id'), client_secret: required('client_secret'), redirect_uri: required('redirect_uri')) ⇒ Client

Initializes a new client with client credentials associated with a registered Asana API application.

Parameters:

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

    a client id from the registered application

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

    a client secret from the registered application

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

    a redirect uri from the registered application



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

def initialize(client_id: required('client_id'),
               client_secret: required('client_secret'),
               redirect_uri: required('redirect_uri'))
  @client = ::OAuth2::Client.new(client_id, client_secret,
                                 site: 'https://app.asana.com',
                                 authorize_url: '/-/oauth_authorize',
                                 token_url: '/-/oauth_token')
  @redirect_uri = redirect_uri
end

Instance Method Details

#authorize_urlString

Returns the String OAuth2 authorize URL.

Returns:

  • (String)

    Returns the String OAuth2 authorize URL.



32
33
34
# File 'lib/asana/authentication/oauth2/client.rb', line 32

def authorize_url
  @client.auth_code.authorize_url(redirect_uri: @redirect_uri)
end

#token_from_auth_code(auth_code) ⇒ ::OAuth2::AccessToken

Retrieves a token from an authorization code.

Returns:

  • (::OAuth2::AccessToken)

    Returns the ::OAuth2::AccessToken token.



39
40
41
# File 'lib/asana/authentication/oauth2/client.rb', line 39

def token_from_auth_code(auth_code)
  @client.auth_code.get_token(auth_code, redirect_uri: @redirect_uri)
end

#token_from_refresh_token(token) ⇒ ::OAuth2::AccessToken

Retrieves a token from a refresh token.

Returns:

  • (::OAuth2::AccessToken)

    Returns the refreshed ::OAuth2::AccessToken token.



46
47
48
# File 'lib/asana/authentication/oauth2/client.rb', line 46

def token_from_refresh_token(token)
  ::OAuth2::AccessToken.new(@client, '', refresh_token: token).refresh!
end