Module: Multiwoven::Integrations::Core::OauthClientCredentials
- Included in:
- Destination::Http::Client, Source::Http::Client
- Defined in:
- lib/multiwoven/integrations/core/oauth_client_credentials.rb
Overview
Shared OAuth2 client_credentials flow. Include in a connector client to:
* inject `Authorization: Bearer <token>` when connection_config[:auth_type]
is `oauth_client_credentials`
* cache the token in the connector's `configuration` JSON and refresh it
shortly before expiry
The including class is expected to set @connector_instance (an object
responding to configuration and update!) before making requests that
should benefit from the cache. Without it, a fresh token is fetched every
call — safe but wasteful.
Constant Summary collapse
- AUTH_TYPE_OAUTH_CLIENT_CREDENTIALS =
"oauth_client_credentials"- TOKEN_EXPIRY_BUFFER_SECONDS =
Refresh access tokens this many seconds before their advertised expiry, so a token that expires mid-request doesn't leave the connector.
300
Instance Method Summary collapse
Instance Method Details
#build_headers(connection_config) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/multiwoven/integrations/core/oauth_client_credentials.rb', line 21 def build_headers(connection_config) headers = (connection_config[:headers] || {}).to_h.dup return headers unless connection_config[:auth_type] == AUTH_TYPE_OAUTH_CLIENT_CREDENTIALS headers["Authorization"] = "Bearer #{ensure_oauth_token(connection_config)}" headers end |
#ensure_oauth_token(connection_config) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/multiwoven/integrations/core/oauth_client_credentials.rb', line 29 def ensure_oauth_token(connection_config) cached = cached_oauth_token return cached if cached fetch_and_cache_oauth_token(connection_config) end |