-
Notifications
You must be signed in to change notification settings - Fork 13
/
oauth2_header.rb
35 lines (29 loc) · 898 Bytes
/
oauth2_header.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
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