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
- #authenticate(header, options) ⇒ Object
- #build(options, only = nil) ⇒ Object
- #parse(header) ⇒ Object
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, ) = .dup parts = parse(header) .delete(:payload) unless parts[:hash] now = Time.now.to_i [:timestamp_skew] ||= DEFAULT_TIMESTAMP_SKEW if [:server_response] credentials = [:credentials] parts.merge!( :ts => [:ts], :nonce => [:nonce] ) else unless [:credentials_lookup].respond_to?(:call) && (credentials = [:credentials_lookup].call(parts[:id])) return AuthenticationFailure.new(:id, "Unidentified id") end if (now - parts[:ts].to_i > [:timestamp_skew]) || (parts[:ts].to_i - now > [:timestamp_skew]) # Stale timestamp return AuthenticationFailure.new(:ts, "Stale ts", :credentials => credentials) end unless parts[:nonce] return AuthenticationFailure.new(:nonce, "Missing nonce") end if [:nonce_lookup].respond_to?(:call) && [:nonce_lookup].call(parts[:nonce]) # Replay return AuthenticationFailure.new(:nonce, "Invalid nonce") end end expected_mac = Crypto.mac(.merge( :credentials => credentials, :ts => parts[:ts], :nonce => parts[:nonce], :ext => parts[:ext], :app => [:app] || parts[:app], :dig => [:dig] || parts[:dig] )) unless expected_mac == parts[:mac] return AuthenticationFailure.new(:mac, "Invalid mac") end expected_hash = parts[:hash] ? Crypto.hash(.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(, only=nil) [:ts] ||= Time.now.to_i [:nonce] ||= SecureRandom.hex(4) REQUIRED_OPTIONS.each do |key| unless .has_key?(key) raise MissingOptionError.new("#{key.inspect} is missing!") end end credentials = [: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() mac = Crypto.mac() parts = { :id => credentials[:id], :ts => [:ts], :nonce => [:nonce], :mac => mac } parts[:hash] = hash if .has_key?(:payload) && ![:payload].nil? parts[:ext] = [:ext] if .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 |