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
|
# File 'lib/hawk/authorization_header.rb', line 16
def build(options, only=nil)
options[:ts] ||= Time.now.to_i
options[:nonce] ||= SecureRandom.hex(4)
REQUIRED_OPTIONS.each do |key|
unless options.has_key?(key)
raise MissingOptionError.new("#{key.inspect} is missing!")
end
end
credentials = options[:credentials]
REQUIRED_CREDENTIAL_MEMBERS.each do |key|
unless credentials.has_key?(key)
raise InvalidCredentialsError.new("#{key.inspect} is missing!")
end
end
unless SUPPORTED_ALGORITHMS.include?(credentials[:algorithm])
raise InvalidAlgorithmError.new("#{credentials[:algorithm].inspect} is not a supported algorithm! Use one of the following: #{SUPPORTED_ALGORITHMS.join(', ')}")
end
hash = Crypto.hash(options).to_s
mac = Crypto.mac(options)
parts = {
:id => credentials[:id],
:ts => options[:ts],
:nonce => options[:nonce],
:mac => mac.to_s
}
parts[:hash] = hash if options.has_key?(:payload) && !options[:payload].nil?
parts[:ext] = options[:ext] if options.has_key?(:ext)
"Hawk " << (only || HEADER_PARTS).inject([]) { |memo, key|
next memo unless parts.has_key?(key)
memo << %(#{key}="#{parts[key]}")
memo
}.join(', ')
end
|