Class: Legate::Auth::MiddlewareFactory
- Inherits:
-
Object
- Object
- Legate::Auth::MiddlewareFactory
- Defined in:
- lib/legate/auth/middleware_factory.rb
Overview
Factory for creating authentication middleware instances based on different scheme types and configurations.
Class Method Summary collapse
-
.create(scheme:, credential:, **options) ⇒ Legate::Auth::ExconMiddleware
Create middleware for any authentication scheme.
-
.create_api_key(api_key:, location: 'header', name: 'X-API-Key', **options) ⇒ Legate::Auth::ExconMiddleware
Create middleware specifically for API key authentication.
-
.create_basic_auth(username:, password:, **options) ⇒ Legate::Auth::ExconMiddleware
Create middleware specifically for Basic authentication.
-
.create_bearer(token:, **options) ⇒ Legate::Auth::ExconMiddleware
Create middleware specifically for Bearer token authentication.
-
.create_from_provider(provider_id, **options) ⇒ Legate::Auth::ExconMiddleware
Create middleware for a pre-configured authentication provider.
-
.create_oauth2(client_id:, client_secret:, authorization_url:, token_url:, scopes: nil, **options) ⇒ Legate::Auth::ExconMiddleware
Create middleware specifically for OAuth2 authentication.
-
.create_oidc(client_id:, client_secret:, discovery_url: nil, authorization_url: nil, token_url: nil, userinfo_url: nil, jwks_url: nil, scopes: nil, **options) ⇒ Legate::Auth::ExconMiddleware
Create middleware specifically for OpenID Connect authentication.
-
.create_service_account(service_account_key:, token_url: nil, scopes: nil, audience: nil, **options) ⇒ Legate::Auth::ExconMiddleware
Create middleware specifically for Service Account authentication.
Class Method Details
.create(scheme:, credential:, **options) ⇒ Legate::Auth::ExconMiddleware
Create middleware for any authentication scheme
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/middleware_factory.rb', line 32 def create(scheme:, credential:, **) # Create a token store if not provided token_store = [:token_store] unless token_store session_service = [:session_service] token_store = Legate::Auth::TokenStore.new(session_service) if session_service end # Create a token manager if not provided token_manager = [:token_manager] # Configure retry options auto_retry = .key?(:auto_retry) ? [:auto_retry] : true max_retries = [:max_retries] || 3 backoff_strategy = [:backoff_strategy] || :exponential backoff_factor = [:backoff_factor] || 1.0 retry_non_idempotent = [:retry_non_idempotent] || false retry_on = [:retry_on] || [] # Create the middleware instance with nil stack (will be set by Excon later) Legate::Auth::ExconMiddleware.new(nil, { scheme: scheme, credential: credential, token_store: token_store, token_manager: token_manager, auto_retry: auto_retry, max_retries: max_retries, backoff_strategy: backoff_strategy, backoff_factor: backoff_factor, retry_non_idempotent: retry_non_idempotent, retry_on: retry_on }) end |
.create_api_key(api_key:, location: 'header', name: 'X-API-Key', **options) ⇒ Legate::Auth::ExconMiddleware
Create middleware specifically for API key authentication
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/legate/auth/middleware_factory.rb', line 72 def create_api_key(api_key:, location: 'header', name: 'X-API-Key', **) # Create the scheme scheme = Legate::Auth::Schemes::ApiKey.new # Create the credential credential = Legate::Auth::Credential.new( auth_type: :api_key, api_key: api_key, location: location, name: name ) # Create and return the middleware create(scheme: scheme, credential: credential, **) end |
.create_basic_auth(username:, password:, **options) ⇒ Legate::Auth::ExconMiddleware
Create middleware specifically for Basic authentication
256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/legate/auth/middleware_factory.rb', line 256 def create_basic_auth(username:, password:, **) # Basic auth is handled by the HTTPBearer scheme with a different type scheme = Legate::Auth::Schemes::HTTPBearer.new(auth_type: :basic) # Create the credential credential = Legate::Auth::Credential.new( auth_type: :basic, username: username, password: password ) # Create and return the middleware create(scheme: scheme, credential: credential, **) end |
.create_bearer(token:, **options) ⇒ Legate::Auth::ExconMiddleware
Create middleware specifically for Bearer token authentication
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/legate/auth/middleware_factory.rb', line 92 def create_bearer(token:, **) # Create the scheme scheme = Legate::Auth::Schemes::HTTPBearer.new # Create the credential credential = Legate::Auth::Credential.new( auth_type: :http_bearer, bearer_token: token ) # Create and return the middleware create(scheme: scheme, credential: credential, **) end |
.create_from_provider(provider_id, **options) ⇒ Legate::Auth::ExconMiddleware
Create middleware for a pre-configured authentication provider
275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/legate/auth/middleware_factory.rb', line 275 def create_from_provider(provider_id, **) # Retrieve the stored credential from Legate::Auth exchanged_credential = Legate::Auth.get_exchanged_credential(provider_id) raise ArgumentError, "No credential found for provider ID: #{provider_id}" unless exchanged_credential # Get the scheme scheme = Legate::Auth.get_scheme_for_provider(provider_id) raise ArgumentError, "No scheme found for provider ID: #{provider_id}" unless scheme # Create and return the middleware create(scheme: scheme, credential: exchanged_credential, **) end |
.create_oauth2(client_id:, client_secret:, authorization_url:, token_url:, scopes: nil, **options) ⇒ Legate::Auth::ExconMiddleware
Create middleware specifically for OAuth2 authentication
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/legate/auth/middleware_factory.rb', line 116 def create_oauth2(client_id:, client_secret:, authorization_url:, token_url:, scopes: nil, **) # Extract OAuth2-specific options redirect_uri = .delete(:redirect_uri) additional_params = .delete(:additional_params) || {} # Create the scheme with additional params = { authorization_url: , token_url: token_url, scopes: scopes } # Add redirect_uri if provided [:redirect_uri] = redirect_uri if redirect_uri # Add any additional parameters [:additional_params] = additional_params unless additional_params.empty? # Create the scheme scheme = Legate::Auth::Schemes::OAuth2.new(**) # Create the credential credential = Legate::Auth::Credential.new( auth_type: :oauth2, client_id: client_id, client_secret: client_secret ) # Create and return the middleware create(scheme: scheme, credential: credential, **) end |
.create_oidc(client_id:, client_secret:, discovery_url: nil, authorization_url: nil, token_url: nil, userinfo_url: nil, jwks_url: nil, scopes: nil, **options) ⇒ Legate::Auth::ExconMiddleware
Create middleware specifically for OpenID Connect authentication
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/legate/auth/middleware_factory.rb', line 162 def create_oidc(client_id:, client_secret:, discovery_url: nil, authorization_url: nil, token_url: nil, userinfo_url: nil, jwks_url: nil, scopes: nil, **) # Extract OIDC-specific options redirect_uri = .delete(:redirect_uri) additional_params = .delete(:additional_params) || {} verify_id_token = .key?(:verify_id_token) ? .delete(:verify_id_token) : true # Determine how to initialize the scheme = if discovery_url { discovery_url: discovery_url, scopes: scopes, verify_id_token: verify_id_token } else { authorization_url: , token_url: token_url, userinfo_url: userinfo_url, jwks_url: jwks_url, scopes: scopes, verify_id_token: verify_id_token } end # Add redirect_uri if provided [:redirect_uri] = redirect_uri if redirect_uri # Add any additional parameters [:additional_params] = additional_params unless additional_params.empty? # Create the scheme scheme = Legate::Auth::Schemes::OIDC.new(**) # Create the credential credential = Legate::Auth::Credential.new( auth_type: :oidc, client_id: client_id, client_secret: client_secret ) # Create and return the middleware create(scheme: scheme, credential: credential, **) end |
.create_service_account(service_account_key:, token_url: nil, scopes: nil, audience: nil, **options) ⇒ Legate::Auth::ExconMiddleware
Create middleware specifically for Service Account authentication
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/legate/auth/middleware_factory.rb', line 215 def create_service_account(service_account_key:, token_url: nil, scopes: nil, audience: nil, **) # Extract service account specific options token_lifetime = .delete(:token_lifetime) || 3600 # Parse the key if it's a string key_data = if service_account_key.is_a?(String) begin JSON.parse(service_account_key) rescue JSON::ParserError raise ArgumentError, 'Invalid service account key: not valid JSON' end else service_account_key end # Use token_url from the key if not provided token_url ||= key_data['token_uri'] # Create the scheme scheme = Legate::Auth::Schemes::ServiceAccount.new( token_url: token_url, audience: audience, scopes: scopes, token_lifetime: token_lifetime ) # Create the credential credential = Legate::Auth::Credential.new( auth_type: :service_account, service_account_key: service_account_key.is_a?(String) ? service_account_key : service_account_key.to_json ) # Create and return the middleware create(scheme: scheme, credential: credential, **) end |