Class: Rack::UrlAuth::Signer
- Inherits:
-
Object
- Object
- Rack::UrlAuth::Signer
- Defined in:
- lib/rack/url_auth/signer.rb
Instance Attribute Summary collapse
-
#secret ⇒ Object
readonly
Returns the value of attribute secret.
Instance Method Summary collapse
-
#initialize(secret) ⇒ Signer
constructor
A new instance of Signer.
- #sign(message) ⇒ Object
- #sign_url(url, method) ⇒ Object
- #verify(message, signature) ⇒ Object
- #verify_url(url, method) ⇒ Object
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
#secret ⇒ Object (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() HMAC::SHA256.hexdigest(secret, ) 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(, signature) actual = Digest::SHA1.hexdigest sign() 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 = method.to_s.downcase + build_url(purl, query) verify(, signature) end |