Sha256: 3cfcb35cdf9609aa760d3733d841f99858cd3ddeadf5c58e85222573b244a7f3

Contents?: true

Size: 898 Bytes

Versions: 2

Compression:

Stored size: 898 Bytes

Contents

require 'rest-core/event'

module RestCore
  # http://tools.ietf.org/html/rfc6749
  class Oauth2Header
    def self.members; [:access_token_type, :access_token]; end
    include Middleware

    def call env, &k
      start_time = Time.now
      headers    = build_headers(env)
      auth       = headers['Authorization']
      event      = Event::WithHeader.new(Time.now - start_time,
                     "Authorization: #{auth}") if auth

      app.call(log(env.merge(REQUEST_HEADERS => headers), event), &k)
    end

    def build_headers env
      auth = case token = access_token(env)
             when String
               token
             when Hash
              token.map{ |(k, v)| "#{k}=\"#{v}\"" }.join(', ')
             end

      if auth
        {'Authorization' => "#{access_token_type(env)} #{auth}"}
      else
        {}
      end.merge(env[REQUEST_HEADERS])
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rest-core-4.0.1 lib/rest-core/middleware/oauth2_header.rb
rest-core-4.0.0 lib/rest-core/middleware/oauth2_header.rb