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


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

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.


30
31
32
# File 'lib/asana/authentication/oauth2/client.rb', line 30

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.


37
38
39
# File 'lib/asana/authentication/oauth2/client.rb', line 37

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.


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

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