Class: FaradayOauth2CcgMiddleware::ClientCredentialsGrant

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/faraday_oauth2_ccg_middleware.rb

Overview

Authorizes the request with the OAUTH2 Client Credentials Grant and injects the received token into the Authorization header

Faraday.new do |conn|

conn.request :oauth2_ccg,
             oauth_host:    'https://server.example.com',
             token_url:     '/token',
             client_id:     's6BhdRkqt3',
             client_secret: '7Fjfp0ZBr1KtDRbnfVdmIw',
             cache_store:   ::ActiveSupport::Cache.lookup_store(:redis_store, 'redis://127.0.0.1:6379')

conn.adapter(:net_http) # NB: Last middleware must be the adapter

end

Examples:

Configure OAUTH Client Credentials Grant middleware

Defined Under Namespace

Classes: Options

Constant Summary collapse

AUTHORIZATION_HEADER =
'Authorization'
BEARER_AUTHORIZATION =
'Bearer'
CLIENT_CREDENTIALS_GRANT =
'client_credentials'

Instance Method Summary collapse

Constructor Details

#initialize(app, options = nil) ⇒ ClientCredentialsGrant

Returns a new instance of ClientCredentialsGrant.

Parameters:

  • app (#call)
  • options (Hash) (defaults to: nil)

Options Hash (options):

  • :oauth_host (String) — default: ''

    OAUTH2 server host

  • :token_url (String) — default: ''

    Token endpoint to request authorization

  • :client_id (String) — default: ''

    Client authentication id

  • :client_secret (String) — default: ''

    Client authentication password

  • :cache_store (Class) — default: nil

    An ActiveSupport::Cache::Store instance for token caching



66
67
68
69
# File 'lib/faraday_oauth2_ccg_middleware.rb', line 66

def initialize(app, options = nil)
  super(app)
  @options = Options.from(options)
end

Instance Method Details

#call(env) ⇒ Object

Parameters:

  • env (Faraday::Env)


72
73
74
75
76
# File 'lib/faraday_oauth2_ccg_middleware.rb', line 72

def call(env)
  env[:request_headers][AUTHORIZATION_HEADER] = "#{BEARER_AUTHORIZATION} #{token}"

  @app.call env
end