Class: BusinessInsightApiClient::Helpers::Authorization

Inherits:
Object
  • Object
show all
Defined in:
lib/business_insight_api_client/helpers/authorization.rb

Overview

Authorization helper, used as helper to interact with the OAuth2 authorization server.

Constant Summary collapse

DEFAULT_OPTIONS =
{
  authorization_url: 'https://nedap-bi.com',
  client_id: '',
  client_secret: ''
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Authorization

Creates a new authorization helper

Parameters:

  • options (Hash) (defaults to: {})

    the options to create the Authorization Helper with.

Options Hash (options):

  • :authorization_url (String) — default: 'https://nedap-bi.com'

    the authorization url.

  • :client_id (String) — default: ''

    the client id

  • :client_secret (String) — default: ''

    the client secret

Raises:

  • (URI::InvalidURIError)

    when the authorization_url given can not be parsed into an uri.



34
35
36
37
38
# File 'lib/business_insight_api_client/helpers/authorization.rb', line 34

def initialize(options = {})
  @authorization_url = URI(options[:authorization_url] || DEFAULT_OPTIONS[:authorization_url])
  @client_id = options[:client_id] || DEFAULT_OPTIONS[:client_id]
  @client_secret = options[:client_secret] || DEFAULT_OPTIONS[:client_secret]
end

Instance Attribute Details

#authorization_urlURI (readonly)

Returns the uri of the authorization server.

Returns:

  • (URI)

    the uri of the authorization server



19
20
21
# File 'lib/business_insight_api_client/helpers/authorization.rb', line 19

def authorization_url
  @authorization_url
end

#client_idString (readonly)

Returns the client id.

Returns:

  • (String)

    the client id



22
23
24
# File 'lib/business_insight_api_client/helpers/authorization.rb', line 22

def client_id
  @client_id
end

#client_secretString (readonly)

Returns the client secret.

Returns:

  • (String)

    the client secret



25
26
27
# File 'lib/business_insight_api_client/helpers/authorization.rb', line 25

def client_secret
  @client_secret
end

#current_tokenOAuth2:AccessToken

Returns the access token.

Returns:

  • (OAuth2:AccessToken)

    the access token.



16
17
18
# File 'lib/business_insight_api_client/helpers/authorization.rb', line 16

def current_token
  @current_token
end

Instance Method Details

#access_token_from_hash(options = {}) ⇒ OAuth2::AccessToken

Creates an AccessToken from hash

Parameters:

  • options (Hash) (defaults to: {})

    the options to create the Access Token with

Options Hash (options):

  • :token (String)

    token the Access Token value

  • :refresh_token (String) — default: nil

    the refresh_token value

  • :expires_in (FixNum, String) — default: nil

    the number of seconds in which the AccessToken will expire

  • :expires_at (FixNum, String) — default: nil

    the epoch time in seconds in which AccessToken will expire

Returns:

  • (OAuth2::AccessToken)

    an access token that can be used for API requests.

See Also:



82
83
84
# File 'lib/business_insight_api_client/helpers/authorization.rb', line 82

def access_token_from_hash(options = {})
  OAuth2::AccessToken.new oauth_client, options.delete(:token), options
end

#auth_code_authorize_url(params = {}) ⇒ Object

Creates an URL to the authorization endpoint of the provider. When the user authorizes he will direct the user agent that follows this link to the redirect_uri.

Parameters:

  • params (Hash) (defaults to: {})

    additional query parameters for the URL.

Options Hash (params):

  • :redirect_uri (String)

    the redirect uri to which the client has to be redirected to.



57
58
59
# File 'lib/business_insight_api_client/helpers/authorization.rb', line 57

def auth_code_authorize_url(params = {})
  oauth_client.auth_code.authorize_url(params)
end

#auth_code_token_from_access_grant(access_grant, params = {}, options = {}) ⇒ Object

Retrieve an access token given the specified validation code. When the token is verified it will direct the user agent that follows this link to the redirect_uri.

Parameters:

  • access_grant (String)

    The Authorization Code value (Access Grant)

  • params (Hash) (defaults to: {})

    additional params

  • options (Hash) (defaults to: {})

    options

Options Hash (params):

  • :redirect_uri (String)

    the redirect uri to which the client has to be redirected to.



68
69
70
# File 'lib/business_insight_api_client/helpers/authorization.rb', line 68

def auth_code_token_from_access_grant(access_grant, params={},options={})
  oauth_client.auth_code.get_token(access_grant,params,options)
end

#client_credential_tokenOAuth2::AccessToken

Returns the client credential token for the application.

Returns:

  • (OAuth2::AccessToken)

    returns the client credential token for the application.



41
42
43
# File 'lib/business_insight_api_client/helpers/authorization.rb', line 41

def client_credential_token
  oauth_client.client_credentials.get_token
end

#current_token_headersHash

Returns the headers hash (includes Authorization token) as formatted with the configuration.

Returns:

  • (Hash)

    the headers hash (includes Authorization token) as formatted with the configuration



47
48
49
50
# File 'lib/business_insight_api_client/helpers/authorization.rb', line 47

def current_token_headers
  return {} if current_token.nil?
  current_token.headers
end