Module: Legate::Auth::HttpClientUtils
- Defined in:
- lib/legate/auth/http_client_utils.rb
Overview
Utility module for integrating authentication with HTTP clients. Provides methods for configuring Excon clients with authentication middleware.
Class Method Summary collapse
-
.authenticate_request(request, scheme:, credential:, **options) ⇒ Hash
Apply authentication to a request using the given scheme and credential.
-
.configure_connection(connection, scheme:, credential:, **options) ⇒ Excon::Connection
Configure an Excon connection with authentication middleware.
-
.create_api_key_connection(url, api_key:, location: 'header', name: 'X-API-Key', **options) ⇒ Excon::Connection
Create a new Excon connection with API key authentication.
-
.create_basic_auth_connection(url, username:, password:, **options) ⇒ Excon::Connection
Create a new Excon connection with Basic authentication.
-
.create_bearer_connection(url, token:, **options) ⇒ Excon::Connection
Create a new Excon connection with bearer token authentication.
-
.create_connection(url, scheme:, credential:, **options) ⇒ Excon::Connection
Create a new Excon connection with authentication middleware.
-
.create_connection_from_provider(url, provider_id, **options) ⇒ Excon::Connection
Create a new Excon connection from a pre-configured authentication provider.
-
.create_oauth2_connection(url, client_id:, client_secret:, authorization_url:, token_url:, scopes: nil, **options) ⇒ Excon::Connection
Create a new Excon connection with OAuth2 authentication.
-
.create_oidc_connection(url, client_id:, client_secret:, discovery_url:, **options) ⇒ Excon::Connection
Create a new Excon connection with OpenID Connect authentication.
-
.create_service_account_connection(url, service_account_key:, scopes: nil, audience: nil, **options) ⇒ Excon::Connection
Create a new Excon connection with service account authentication.
Class Method Details
.authenticate_request(request, scheme:, credential:, **options) ⇒ Hash
Apply authentication to a request using the given scheme and credential
331 332 333 334 335 336 337 338 |
# File 'lib/legate/auth/http_client_utils.rb', line 331 def authenticate_request(request, scheme:, credential:, **) # Extract token store and manager from options token_store = [:token_store] token_manager = [:token_manager] # Apply authentication ToolIntegration.apply_authentication(request, scheme, credential, token_store, token_manager) end |
.configure_connection(connection, scheme:, credential:, **options) ⇒ Excon::Connection
Configure an Excon connection with authentication middleware
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/legate/auth/http_client_utils.rb', line 20 def configure_connection(connection, scheme:, credential:, **) # Create the middleware middleware = MiddlewareFactory.create(scheme: scheme, credential: credential, **) # Add the middleware to the connection connection.data[:middlewares] ||= connection.data[:middlewares].dup # Remove any existing auth middleware of the same type connection.data[:middlewares].reject! { |m| m == middleware.class } # Add our middleware - ensure it's actually added to the stack connection.data[:middlewares] << middleware.class unless connection.data[:middlewares].include?(middleware.class) # Store the middleware instance in the connection connection.data[:auth_middleware] = middleware connection end |
.create_api_key_connection(url, api_key:, location: 'header', name: 'X-API-Key', **options) ⇒ Excon::Connection
Create a new Excon connection with API key authentication
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/legate/auth/http_client_utils.rb', line 63 def create_api_key_connection(url, 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 ) # Extract middleware options from the options hash = () # Create the middleware using the factory middleware_instance = MiddlewareFactory.create( scheme: scheme, credential: credential, ** ) # Prepare Excon options, ensuring we don't modify the original options hash directly excon_opts = .dup # Remove our custom options that shouldn't be passed directly to Excon.new if they were in **options excon_opts.delete(:token_store) # Example, add others if necessary excon_opts.delete(:token_manager) excon_opts.delete(:session_service) # if MiddlewareFactory might add it to options # also retry options if they are only for our middleware and not excon directly %i[auto_retry max_retries backoff_strategy backoff_factor retry_non_idempotent retry_on].each do |k| excon_opts.delete(k) end # Add retry configuration for Idempotent middleware excon_opts[:retry_limit] = 3 excon_opts[:retry_interval] = 0.5 excon_opts[:idempotent] = true # Ensure SSL verification is enabled excon_opts[:ssl_verify_peer] = true # Increase default timeouts if not specified excon_opts[:connect_timeout] ||= 30 excon_opts[:read_timeout] ||= 30 excon_opts[:write_timeout] ||= 30 # Create the connection with our middleware connection = Excon.new(url, excon_opts) # Configure the middleware stack connection.data[:middlewares] ||= connection.data[:middlewares].dup connection.data[:middlewares].reject! { |m| m == Legate::Auth::ExconMiddleware } # Add our middleware class to the stack unless connection.data[:middlewares].include?(Legate::Auth::ExconMiddleware) # Find the position after Idempotent middleware idempotent_index = connection.data[:middlewares].index(Excon::Middleware::Idempotent) if idempotent_index connection.data[:middlewares].insert(idempotent_index + 1, Legate::Auth::ExconMiddleware) else connection.data[:middlewares] << Legate::Auth::ExconMiddleware end end # Store the middleware configuration for use by the shell instance connection.data[:auth_middleware_config] = middleware_instance connection end |
.create_basic_auth_connection(url, username:, password:, **options) ⇒ Excon::Connection
Create a new Excon connection with Basic authentication
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/legate/auth/http_client_utils.rb', line 271 def create_basic_auth_connection(url, username:, password:, **) # Extract middleware options from the options hash = () # Create middleware using the factory middleware = MiddlewareFactory.create_basic_auth( username: username, password: password, ** ) # Create the connection connection = Excon.new(url, ) # Add the middleware to the connection connection.data[:middlewares] ||= connection.data[:middlewares].dup connection.data[:middlewares].reject! { |m| m == Legate::Auth::ExconMiddleware } connection.data[:middlewares] << middleware.class unless connection.data[:middlewares].include?(middleware.class) # Store the middleware instance in the connection connection.data[:auth_middleware] = middleware connection end |
.create_bearer_connection(url, token:, **options) ⇒ Excon::Connection
Create a new Excon connection with bearer token authentication
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/legate/auth/http_client_utils.rb', line 138 def create_bearer_connection(url, token:, **) # Extract middleware options from the options hash = () # Create middleware using the factory middleware = MiddlewareFactory.create_bearer( token: token, ** ) # Create the connection connection = Excon.new(url, ) # Add the middleware to the connection connection.data[:middlewares] ||= connection.data[:middlewares].dup connection.data[:middlewares].reject! { |m| m == Legate::Auth::ExconMiddleware } connection.data[:middlewares] << middleware.class unless connection.data[:middlewares].include?(middleware.class) # Store the middleware instance in the connection connection.data[:auth_middleware] = middleware connection end |
.create_connection(url, scheme:, credential:, **options) ⇒ Excon::Connection
Create a new Excon connection with authentication middleware
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/legate/auth/http_client_utils.rb', line 45 def create_connection(url, scheme:, credential:, **) # Extract middleware options from the options hash = () # Create the connection connection = Excon.new(url, ) # Configure the connection with authentication configure_connection(connection, scheme: scheme, credential: credential, **) end |
.create_connection_from_provider(url, provider_id, **options) ⇒ Excon::Connection
Create a new Excon connection from a pre-configured authentication provider
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/legate/auth/http_client_utils.rb', line 301 def create_connection_from_provider(url, provider_id, **) # Extract middleware options from the options hash = () # Create middleware using the factory middleware = MiddlewareFactory.create_from_provider( provider_id, ** ) # Create the connection connection = Excon.new(url, ) # Add the middleware to the connection connection.data[:middlewares] ||= connection.data[:middlewares].dup connection.data[:middlewares].reject! { |m| m == Legate::Auth::ExconMiddleware } connection.data[:middlewares] << middleware.class unless connection.data[:middlewares].include?(middleware.class) # Store the middleware instance in the connection connection.data[:auth_middleware] = middleware connection end |
.create_oauth2_connection(url, client_id:, client_secret:, authorization_url:, token_url:, scopes: nil, **options) ⇒ Excon::Connection
Create a new Excon connection with OAuth2 authentication
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 |
# File 'lib/legate/auth/http_client_utils.rb', line 171 def create_oauth2_connection(url, client_id:, client_secret:, authorization_url:, token_url:, scopes: nil, **) # Extract middleware options from the options hash = () # Create middleware using the factory middleware = MiddlewareFactory.create_oauth2( client_id: client_id, client_secret: client_secret, authorization_url: , token_url: token_url, scopes: scopes, ** ) # Create the connection connection = Excon.new(url, ) # Add the middleware to the connection connection.data[:middlewares] ||= connection.data[:middlewares].dup connection.data[:middlewares].reject! { |m| m == Legate::Auth::ExconMiddleware } connection.data[:middlewares] << middleware.class unless connection.data[:middlewares].include?(middleware.class) # Store the middleware instance in the connection connection.data[:auth_middleware] = middleware connection end |
.create_oidc_connection(url, client_id:, client_secret:, discovery_url:, **options) ⇒ Excon::Connection
Create a new Excon connection with OpenID Connect authentication
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/legate/auth/http_client_utils.rb', line 239 def create_oidc_connection(url, client_id:, client_secret:, discovery_url:, **) # Extract middleware options from the options hash = () # Create middleware using the factory middleware = MiddlewareFactory.create_oidc( client_id: client_id, client_secret: client_secret, discovery_url: discovery_url, ** ) # Create the connection connection = Excon.new(url, ) # Add the middleware to the connection connection.data[:middlewares] ||= connection.data[:middlewares].dup connection.data[:middlewares].reject! { |m| m == Legate::Auth::ExconMiddleware } connection.data[:middlewares] << middleware.class unless connection.data[:middlewares].include?(middleware.class) # Store the middleware instance in the connection connection.data[:auth_middleware] = middleware connection end |
.create_service_account_connection(url, service_account_key:, scopes: nil, audience: nil, **options) ⇒ Excon::Connection
Create a new Excon connection with service account authentication
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/legate/auth/http_client_utils.rb', line 206 def create_service_account_connection(url, service_account_key:, scopes: nil, audience: nil, **) # Extract middleware options from the options hash = () # Create middleware using the factory middleware = MiddlewareFactory.create_service_account( service_account_key: service_account_key, scopes: scopes, audience: audience, ** ) # Create the connection connection = Excon.new(url, ) # Add the middleware to the connection connection.data[:middlewares] ||= connection.data[:middlewares].dup connection.data[:middlewares].reject! { |m| m == Legate::Auth::ExconMiddleware } connection.data[:middlewares] << middleware.class unless connection.data[:middlewares].include?(middleware.class) # Store the middleware instance in the connection connection.data[:auth_middleware] = middleware connection end |