Class: Rack::UrlAuth::Signer

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/url_auth/signer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(secret) ⇒ Signer

Returns a new instance of Signer.



9
10
11
# File 'lib/rack/url_auth/signer.rb', line 9

def initialize(secret)
  @secret = secret
end

Instance Attribute Details

#secretObject (readonly)

Returns the value of attribute secret.



7
8
9
# File 'lib/rack/url_auth/signer.rb', line 7

def secret
  @secret
end

Instance Method Details

#sign(message) ⇒ Object



13
14
15
# File 'lib/rack/url_auth/signer.rb', line 13

def sign(message)
  HMAC::SHA256.hexdigest(secret, message)
end

#sign_url(url, method) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/rack/url_auth/signer.rb', line 23

def sign_url(url, method)
  purl, query = parse_and_extract_query(url)
  normalized = purl.normalize.to_s
  query['signature'] = sign(method.to_s.downcase + normalized)

  build_url(purl, query)
end

#verify(message, signature) ⇒ Object



17
18
19
20
21
# File 'lib/rack/url_auth/signer.rb', line 17

def verify(message, signature)
  actual = Digest::SHA1.hexdigest sign(message)
  expected = Digest::SHA1.hexdigest signature
  actual == expected
end

#verify_url(url, method) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/rack/url_auth/signer.rb', line 31

def verify_url(url, method)
  purl, query = parse_and_extract_query(url)
  signature = query.delete('signature').to_s
  message = method.to_s.downcase + build_url(purl, query)

  verify(message, signature)
end