Class: Legate::Auth::Schemes::ApiKey
- Inherits:
-
Legate::Auth::Scheme
- Object
- Legate::Auth::Scheme
- Legate::Auth::Schemes::ApiKey
- Defined in:
- lib/legate/auth/schemes/api_key.rb
Overview
API Key authentication scheme. This scheme applies an API key to requests via a header, query parameter, or cookie.
Constant Summary collapse
- DEFAULT_HEADER_NAME =
Default header name for API key authentication
'X-API-Key'
Instance Method Summary collapse
-
#apply_to_request(request, credential) ⇒ Hash
Apply authentication to a request.
-
#exchange_token(credential) ⇒ Legate::Auth::ExchangedCredential
Exchange a credential for a token.
-
#scheme_type ⇒ Symbol
Get the type of authentication scheme.
-
#to_h ⇒ Hash
Get hash representation of the scheme.
Methods inherited from Legate::Auth::Scheme
#authentication_error?, #build_authorization_uri, #refresh_token, #revoke_token, #supports_refresh?, #to_s, #validate!
Instance Method Details
#apply_to_request(request, credential) ⇒ Hash
Apply authentication to a request
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/legate/auth/schemes/api_key.rb', line 28 def apply_to_request(request, credential) # Create a deep copy of the request to avoid modifying the original request_copy = Marshal.load(Marshal.dump(request)) # Handle the case where we get a stack object from Excon if request_copy.is_a?(Hash) if request_copy[:stack] # Extract the data from stack (Excon middleware format) %i[scheme method path host port query].each do |key| request_copy[key] = request_copy[:stack][key] if request_copy[:stack][key] && !request_copy[key] end end # Ensure headers hash exists request_copy[:headers] ||= {} end # Extract the API key from the credential api_key = extract_api_key(credential) raise Legate::Auth::Error, 'API key not found in credential' unless api_key # Get parameters for applying the API key location = credential[:location] || 'header' name = credential[:name] || DEFAULT_HEADER_NAME # Apply the API key based on location case location.to_s.downcase when 'header' apply_to_header(request_copy, name, api_key) when 'query', 'querystring' apply_to_query(request_copy, name, api_key) when 'cookie' (request_copy, name, api_key) else raise Legate::Auth::Error, "Unsupported API key location: #{location}" end end |
#exchange_token(credential) ⇒ Legate::Auth::ExchangedCredential
Exchange a credential for a token
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/legate/auth/schemes/api_key.rb', line 69 def exchange_token(credential) # For API keys, we simply create a "token" that wraps the API key # This is useful for token management consistency api_key = extract_api_key(credential) raise Legate::Auth::TokenExchangeError, 'API key not found in credential' unless api_key # Create a simple exchanged credential that never expires Legate::Auth::ExchangedCredential.new( auth_type: :api_key, api_key: api_key, location: credential[:location] || 'header', name: credential[:name] || DEFAULT_HEADER_NAME ) end |
#scheme_type ⇒ Symbol
Get the type of authentication scheme
19 20 21 |
# File 'lib/legate/auth/schemes/api_key.rb', line 19 def scheme_type :api_key end |
#to_h ⇒ Hash
Get hash representation of the scheme
86 87 88 89 90 |
# File 'lib/legate/auth/schemes/api_key.rb', line 86 def to_h { type: scheme_type } end |