Class: SwaggerPetstore::PetstoreAuth
- Inherits:
-
CoreLibrary::HeaderAuth
- Object
- CoreLibrary::HeaderAuth
- SwaggerPetstore::PetstoreAuth
- 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
-
#error_message ⇒ Object
Display error message on occurrence of authentication failure.
-
#get_authorization_url(state: nil, additional_params: nil) ⇒ String
Builds and returns an authorization URL.
-
#initialize(petstore_auth_credentials, config) ⇒ PetstoreAuth
constructor
Initialization constructor.
-
#valid ⇒ Boolean
Validates the oAuth token.
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_message ⇒ Object
Display error message on occurrence of authentication failure.
12 13 14 |
# File 'lib/swagger_petstore/http/auth/petstore_auth.rb', line 12 def '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.
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 (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 |
#valid ⇒ Boolean
Validates the oAuth token.
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 |