Class: SwaggerPetstore::PetstoreAuth

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

Overview

Utility class for OAuth 2 authorization and token management.

Instance Method Summary collapse

Constructor Details

#initialize(petstore_auth_credentials, config) ⇒ PetstoreAuth

Initialization constructor.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/swagger_petstore/http/auth/petstore_auth.rb', line 17

def initialize(petstore_auth_credentials, config)
  auth_params = {}
  @_o_auth_client_id = petstore_auth_credentials.o_auth_client_id unless
    petstore_auth_credentials.nil? || petstore_auth_credentials.o_auth_client_id.nil?
  @_o_auth_redirect_uri = petstore_auth_credentials.o_auth_redirect_uri unless
    petstore_auth_credentials.nil? || petstore_auth_credentials.o_auth_redirect_uri.nil?
  @_o_auth_token = petstore_auth_credentials.o_auth_token unless
    petstore_auth_credentials.nil? || petstore_auth_credentials.o_auth_token.nil?
  @_o_auth_scopes = petstore_auth_credentials.o_auth_scopes unless
    petstore_auth_credentials.nil? || petstore_auth_credentials.o_auth_scopes.nil?
  @_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

#error_messageObject

Display error message on occurrence of authentication failure.



12
13
14
# File 'lib/swagger_petstore/http/auth/petstore_auth.rb', line 12

def error_message
  'PetstoreAuth: OAuthToken is undefined or expired.'
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.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/swagger_petstore/http/auth/petstore_auth.rb', line 45

def get_authorization_url(state: nil, additional_params: nil)
  auth_url = @_config.get_base_uri
  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

#validBoolean

Validates the oAuth token.

Returns:

  • (Boolean)

    true if the token is present and not expired.



35
36
37
# File 'lib/swagger_petstore/http/auth/petstore_auth.rb', line 35

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