Sha256: 8c05bd0f30c1d0087466815fbc6cca648e8ff9e251abc91600bdc8e94c31dbc7

Contents?: true

Size: 1001 Bytes

Versions: 2

Compression:

Stored size: 1001 Bytes

Contents

require 'hawk'

module Transistor
  module Middleware
    class Authentication < Faraday::Middleware
      AUTHORIZATION_HEADER = 'Authorization'.freeze
      CONTENT_TYPE_HEADER = 'Content-Type'.freeze
      
      def initalize(app, options = {})
        super(app)
      end
      
      def call(env)
        env[:request_headers][AUTHORIZATION_HEADER] = authorize_request(env)
        @app.call(env)
      end
      
      private
      
      def authorize_request(env)
        Hawk::Client.build_authorization_header(
          credentials: Transistor.config.credentials,
          ts: Time.now.to_i,
          content_type: env[:request_headers][CONTENT_TYPE_HEADER].to_s.split(';').first,
          method: env[:method].to_s.upcase,
          port: env[:url].port || (env[:url].scheme == 'https' ? 443 : 80),
          host: env[:url].host,
          request_uri: env[:url].to_s.sub(%r{\A#{env[:url].scheme}://#{env[:url].host}(:#{env[:url].port})?}, '')
        )
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
transistor-client-0.0.2 lib/transistor/middleware/authentication.rb
transistor-client-0.0.1 lib/transistor/middleware/authentication.rb