Module: Hawk::AuthorizationHeader

Extended by:
AuthorizationHeader
Included in:
AuthorizationHeader
Defined in:
lib/hawk/authorization_header.rb

Constant Summary collapse

REQUIRED_OPTIONS =
[:method, :request_uri, :host, :port].freeze
REQUIRED_CREDENTIAL_MEMBERS =
[:id, :key, :algorithm].freeze
SUPPORTED_ALGORITHMS =
['sha256', 'sha1'].freeze
HEADER_PARTS =
[:id, :ts, :nonce, :hash, :ext, :mac].freeze
DEFAULT_TIMESTAMP_SKEW =

±60 seconds

60.freeze
MissingOptionError =
Class.new(StandardError)
InvalidCredentialsError =
Class.new(StandardError)
InvalidAlgorithmError =
Class.new(StandardError)

Instance Method Summary collapse

Instance Method Details

#authenticate(header, options) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/hawk/authorization_header.rb', line 56

def authenticate(header, options)
  options = options.dup

  parts = parse(header)
  options.delete(:payload) unless parts[:hash]

  now = Time.now.to_i

  options[:timestamp_skew] ||= DEFAULT_TIMESTAMP_SKEW

  if options[:server_response]
    credentials = options[:credentials]
    parts.merge!(
      :ts => options[:ts],
      :nonce => options[:nonce]
    )
  else
    unless options[:credentials_lookup].respond_to?(:call) && (credentials = options[:credentials_lookup].call(parts[:id]))
      return AuthenticationFailure.new(:id, "Unidentified id")
    end

    if (now - parts[:ts].to_i > options[:timestamp_skew]) || (parts[:ts].to_i - now > options[:timestamp_skew])
      # Stale timestamp
      return AuthenticationFailure.new(:ts, "Stale ts", :credentials => credentials)
    end

    unless parts[:nonce]
      return AuthenticationFailure.new(:nonce, "Missing nonce")
    end

    if options[:nonce_lookup].respond_to?(:call) && options[:nonce_lookup].call(parts[:nonce])
      # Replay
      return AuthenticationFailure.new(:nonce, "Invalid nonce")
    end
  end

  expected_mac = Crypto.mac(options.merge(
    :credentials => credentials,
    :ts => parts[:ts],
    :nonce => parts[:nonce],
    :ext => parts[:ext],
    :app => options[:app] || parts[:app],
    :dig => options[:dig] || parts[:dig]
  ))
  unless expected_mac == parts[:mac]
    return AuthenticationFailure.new(:mac, "Invalid mac")
  end

  expected_hash = parts[:hash] ? Crypto.hash(options.merge(:credentials => credentials)) : nil
  if expected_hash && expected_hash != parts[:hash]
    return AuthenticationFailure.new(:hash, "Invalid hash")
  end

  credentials
end

#build(options, only = nil) ⇒ Object



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)
  mac = Crypto.mac(options)

  parts = {
    :id => credentials[:id],
    :ts => options[:ts],
    :nonce => options[:nonce],
    :mac => mac
  }
  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

#parse(header) ⇒ Object



112
113
114
115
116
117
118
119
120
# File 'lib/hawk/authorization_header.rb', line 112

def parse(header)
  parts = header.sub(/\AHawk\s+/, '').split(/,\s*/)
  parts.inject(Hash.new) do |memo, part|
    next memo unless part =~ %r{([a-z]+)=(['"])([^\2]+)\2}
    key, val = $1, $3
    memo[key.to_sym] = val
    memo
  end
end