Module: UrlSigner
- Defined in:
- lib/url_signer.rb,
lib/url_signer/base.rb,
lib/url_signer/rails.rb,
lib/url_signer/signer.rb,
lib/url_signer/version.rb,
lib/url_signer/verifier.rb
Overview
Sign and verify URLs
Defined Under Namespace
Modules: Rails Classes: Base, Signer, Verifier
Constant Summary collapse
- VERSION =
"0.3"
Class Method Summary collapse
-
.sign(url, *options) ⇒ Object
Returns a new
URIinstance by appending asignatureparameter to the query ofurl. -
.valid?(url, *options) ⇒ Boolean
Verify the authenticity of the
urlby checking the value of thesignaturequery parameter (if present).
Class Method Details
.sign(url, *options) ⇒ Object
Returns a new URI instance by appending a signature parameter to the query of url. The method accepts that url can be either a String or a URI instance.
signed_url = UrlSigner.sign('http://google.fr&q=test') # => <URI::HTTP...>
The following key/value parameters can be given to options:
-
:key- the secret key used for encryption -
:hash_method- the hash function to pass toDigest::HMAC. Defaults toDigest::SHA1.
Note that if a URL_SIGNING_KEY environment variable is defined, it will be used as a default value for the :key option.
19 20 21 22 |
# File 'lib/url_signer.rb', line 19 def sign(url, *) temp_signer = UrlSigner::Signer.new(url, *) temp_signer.sign end |
.valid?(url, *options) ⇒ Boolean
Verify the authenticity of the url by checking the value of the signature query parameter (if present). The method accepts that url can be either a String or a URI instance.
The following key/value parameters can be given to options:
-
:key- the secret key used for encryption -
:hash_method- the hash function to pass toDigest::HMAC. Defaults toDigest::SHA1.
Note that if a URL_SIGNING_KEY environment variable is defined, it will be used as a default value for the :key option.
Examples
dummy_url = 'http://google.fr?q=test
UrlSigner.valid?(dummy_url) # => false
signed_url = UrlSigner.sign('http://google.fr&q=test')
UrlSigner.valid?(signed_url) # => true
40 41 42 43 |
# File 'lib/url_signer.rb', line 40 def valid?(url, *) temp_verifier = UrlSigner::Verifier.new(url, *) temp_verifier.valid? end |