Class: SpotifyWebApi::OAuth2

Inherits:
CoreLibrary::HeaderAuth
  • Object
show all
Includes:
CoreLibrary
Defined in:
lib/spotify_web_api/http/auth/o_auth2.rb

Overview

Utility class for OAuth 2 authorization and token management.

Instance Method Summary collapse

Constructor Details

#initialize(authorization_code_auth_credentials, config) ⇒ OAuth2

Initialization constructor.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/spotify_web_api/http/auth/o_auth2.rb', line 17

def initialize(authorization_code_auth_credentials, config)
  auth_params = {}
  @_o_auth_client_id = authorization_code_auth_credentials.o_auth_client_id unless
    authorization_code_auth_credentials.nil? || authorization_code_auth_credentials.o_auth_client_id.nil?
  @_o_auth_client_secret = authorization_code_auth_credentials.o_auth_client_secret unless
    authorization_code_auth_credentials.nil? || authorization_code_auth_credentials.o_auth_client_secret.nil?
  @_o_auth_redirect_uri = authorization_code_auth_credentials.o_auth_redirect_uri unless
    authorization_code_auth_credentials.nil? || authorization_code_auth_credentials.o_auth_redirect_uri.nil?
  @_o_auth_token = authorization_code_auth_credentials.o_auth_token unless
    authorization_code_auth_credentials.nil? || authorization_code_auth_credentials.o_auth_token.nil?
  @_o_auth_scopes = authorization_code_auth_credentials.o_auth_scopes unless
    authorization_code_auth_credentials.nil? || authorization_code_auth_credentials.o_auth_scopes.nil?
  @_config = config
  @_o_auth_api = OAuthAuthorizationController.new(config)
  auth_params['Authorization'] = "Bearer #{@_o_auth_token.access_token}" unless @_o_auth_token.nil?

  super auth_params
end

Instance Method Details

#build_basic_auth_headerString

Builds the basic auth header for endpoints in the OAuth Authorization Controller.

Returns:

  • (String)

    The value of the Authentication header.



66
67
68
# File 'lib/spotify_web_api/http/auth/o_auth2.rb', line 66

def build_basic_auth_header
  "Basic #{AuthHelper.get_base64_encoded_value(@_o_auth_client_id, @_o_auth_client_secret)}"
end

#error_messageObject

Display error message on occurrence of authentication failure.



12
13
14
# File 'lib/spotify_web_api/http/auth/o_auth2.rb', line 12

def error_message
  'AuthorizationCodeAuth: OAuthToken is undefined or expired.'
end

#fetch_token(auth_code, additional_params: nil) ⇒ OAuthToken

Fetches the token.

Parameters:

  • auth_code (String)

    The authentication code.

  • additional_params (Hash) (defaults to: nil)

    Any additional form parameters.

Returns:



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/spotify_web_api/http/auth/o_auth2.rb', line 74

def fetch_token(auth_code, additional_params: nil)
  token = @_o_auth_api.request_token(
    build_basic_auth_header,
    auth_code,
    @_o_auth_redirect_uri,
    _field_parameters: additional_params
  ).data
  if token.respond_to?('expires_in') && !token.expires_in.nil?
    token.expiry = AuthHelper.get_token_expiry(token.expires_in, Time.now.utc.to_i)
  end
  token
end

#get_authorization_url(state: nil, additional_params: nil) ⇒ String

Builds and returns an authorization URL. The user is expected to obtain an authorization code from this URL and then call the fetch token function with that authorization code.

Parameters:

  • state (String) (defaults to: nil)

    An opaque state string.

  • additional_params (Hash) (defaults to: nil)

    Any additional query parameters to be added to the URL.

Returns:

  • (String)

    additional_params The authorization URL.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/spotify_web_api/http/auth/o_auth2.rb', line 48

def get_authorization_url(state: nil, additional_params: nil)
  auth_url = @_config.get_base_uri(Server.AUTH_SERVER)
  auth_url += '/authorize'
  query_params = {
    'response_type' => 'code',
    'client_id' => @_o_auth_client_id,
    'redirect_uri' => @_o_auth_client_id
  }
  query_params['scope'] = Array(@_o_auth_scopes).compact.join(' ') if @_o_auth_scopes
  query_params['state'] = state if state
  query_params.merge!(additional_params) if additional_params
  auth_url = APIHelper.append_url_with_query_parameters(auth_url,
                                                        query_params)
  APIHelper.clean_url(auth_url)
end

#refresh_token(additional_params: nil) ⇒ OAuthToken

Refreshes OAuth token.

Parameters:

  • additional_params (Hash) (defaults to: nil)

    Any additional form parameters.

Returns:



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/spotify_web_api/http/auth/o_auth2.rb', line 97

def refresh_token(additional_params: nil)
  token = @_o_auth_api.refresh_token(
    OAuth2.build_basic_auth_header,
    @_o_auth_token.refresh_token,
    scope: !@_o_auth_scopes.nil? ? Array(@_o_auth_scopes).compact.join(' ') : nil,
    _field_parameters: additional_params
  ).data
  if token.respond_to?('expires_in') && !token.expires_in.nil?
    token.expiry = AuthHelper.get_token_expiry(token.expires_in, Time.now.utc.to_i)
  end
  token
end

#token_expired?(token) ⇒ Boolean

Checks if OAuth token has expired.

Parameters:

  • token (OAuthToken)

    The oAuth token instance.

Returns:

  • (Boolean)

    true if the token is present and not expired.



90
91
92
# File 'lib/spotify_web_api/http/auth/o_auth2.rb', line 90

def token_expired?(token)
  token.respond_to?('expiry') && AuthHelper.token_expired?(token.expiry)
end

#validBoolean

Validates the oAuth token.

Returns:

  • (Boolean)

    true if the token is present and not expired.



38
39
40
# File 'lib/spotify_web_api/http/auth/o_auth2.rb', line 38

def valid
  !@_o_auth_token.nil? && !token_expired?(@_o_auth_token)
end