/
authorization.rb
62 lines (55 loc) · 1.77 KB
/
authorization.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# frozen_string_literal: true
require 'base64'
module Faraday
class Request
# Request middleware for the Authorization HTTP header
class Authorization < Faraday::Middleware
unless defined?(::Faraday::Request::Authorization::KEY)
KEY = 'Authorization'
end
# @param type [String, Symbol]
# @param token [String, Symbol, Hash]
# @return [String] a header value
def self.header(type, token)
case token
when String, Symbol, Proc
token = token.call if token.is_a?(Proc)
"#{type} #{token}"
when Hash
build_hash(type.to_s, token)
else
raise ArgumentError,
"Can't build an Authorization #{type}" \
"header from #{token.inspect}"
end
end
# @param type [String]
# @param hash [Hash]
# @return [String] type followed by comma-separated key=value pairs
# @api private
def self.build_hash(type, hash)
comma = ', '
values = []
hash.each do |key, value|
value = value.call if value.is_a?(Proc)
values << "#{key}=#{value.to_s.inspect}"
end
"#{type} #{values * comma}"
end
# @param app [#call]
# @param type [String, Symbol] Type of Authorization
# @param param [String, Symbol, Hash, Proc] parameter to build the Authorization header.
# This value can be a proc, in which case it will be invoked on each request.
def initialize(app, type, param)
@type = type
@param = param
super(app)
end
# @param env [Faraday::Env]
def on_request(env)
return if env.request_headers[KEY]
env.request_headers[KEY] = self.class.header(@type, @param)
end
end
end
end