url_signer 0.2 → 0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +35 -7
- data/lib/url_signer.rb +4 -3
- data/lib/url_signer/base.rb +2 -1
- data/lib/url_signer/rails.rb +99 -0
- data/lib/url_signer/signer.rb +1 -1
- data/lib/url_signer/{extensions/uri.rb → uri.rb} +4 -0
- data/lib/url_signer/verifier.rb +1 -1
- data/lib/url_signer/version.rb +1 -1
- metadata +4 -6
- data/lib/url_signer/extensions/string.rb +0 -13
- data/spec/string_spec.rb +0 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a6cf7635deb5dc16a22e44a26652d737bea127f6
|
4
|
+
data.tar.gz: b386a8d8a59ef637b68b7430a0760c821a2aa018
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 53f896dae067179575e8b197f6f991436796097dc8c253ae16fcfb454b0e4d25070946661f9f15970658e8375438ec895c02bedd0fc0d355e30b350f35f6812c
|
7
|
+
data.tar.gz: fde1aa26138de8ef4951dc19d427181e7be4fa507002142b7566d4f59b720a3733c40af0a5124029e1e543ac97f2dcdfffed1302125aab3fab7acbbbfe24afb6
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# UrlSigner
|
2
2
|
[](https://travis-ci.org/ushu/url_signer)
|
3
3
|
[](https://codeclimate.com/github/ushu/url_signer)
|
4
4
|
|
@@ -27,7 +27,8 @@ Or install it yourself as:
|
|
27
27
|
To convert a URL into a signed url, pass it to `UrlSigner.sign`, passing it either a string of an instance of `URI`.
|
28
28
|
|
29
29
|
```ruby
|
30
|
-
|
30
|
+
# generate a new URI instance with `signature` param populated
|
31
|
+
signed_url = UrlSigner.sign('http://google.fr?q=test', key: 'mykey')
|
31
32
|
```
|
32
33
|
|
33
34
|
the returned value `signed_url` is an instance of `URI`.
|
@@ -37,11 +38,11 @@ the returned value `signed_url` is an instance of `URI`.
|
|
37
38
|
Given a signed URL, you can check its authenticity by calling `UrlSigner.valid?` on it:
|
38
39
|
|
39
40
|
```ruby
|
40
|
-
|
41
|
-
true
|
41
|
+
# verify url validity for a given URI instance
|
42
|
+
UrlSigner.valid?(signed_url, key: 'mykey') # => true
|
42
43
|
```
|
43
44
|
|
44
|
-
### helper methods
|
45
|
+
### helper methods on URI
|
45
46
|
|
46
47
|
The gem adds helper methods to <tt>String</tt> and <tt>URI</tt> classes:
|
47
48
|
|
@@ -52,11 +53,38 @@ signed_url = 'http://google.fr'.to_signed_uri(key: 'test')
|
|
52
53
|
# or if we have a URI insance already
|
53
54
|
url = URI.parse('http://google.fr')
|
54
55
|
signed_url = url.signed(key: 'test')
|
56
|
+
```
|
57
|
+
|
58
|
+
## Rails integration
|
59
|
+
|
60
|
+
When using `Rails`, a set of helpers are added to `ActionController::Base`:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
|
64
|
+
class MyController < ActionController::Base
|
65
|
+
def get_signed_url
|
66
|
+
@signed_url = sign_url(signed_url_my_controller_url)
|
67
|
+
# Template will link to @signed_url
|
68
|
+
end
|
55
69
|
|
56
|
-
|
57
|
-
signed_url
|
70
|
+
before_action :verify_signature!
|
71
|
+
def signed_url
|
72
|
+
# This method is only accessible with a signed url
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
```
|
77
|
+
|
78
|
+
The key and hash method used in `sign_url` and `verify_signature!` are provided through `Rails.configuration.url_signer`, which default to:
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
# defaults values:
|
82
|
+
Rails.configuration.url_signer.key = ENV['URL_SIGNING_KEY']
|
83
|
+
Rails.configuration.url_signer.hash_method = Digest::SHA1
|
58
84
|
```
|
59
85
|
|
86
|
+
Note that provided env `URL_SIGNING_KEY` environment variable is usually enough to get a working URL signing environment.
|
87
|
+
|
60
88
|
## Contributing
|
61
89
|
|
62
90
|
1. Fork it ( https://github.com/[my-github-username]/url_sign/fork )
|
data/lib/url_signer.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# Sign and verify URLs
|
1
2
|
module UrlSigner
|
2
3
|
autoload :Base, 'url_signer/base'
|
3
4
|
autoload :Signer, 'url_signer/signer'
|
@@ -44,6 +45,6 @@ module UrlSigner
|
|
44
45
|
end
|
45
46
|
|
46
47
|
# Insert helpers in URI
|
47
|
-
require 'url_signer/
|
48
|
-
# Insert helpers in
|
49
|
-
require 'url_signer/
|
48
|
+
require 'url_signer/uri'
|
49
|
+
# Insert helpers in Rails controllers
|
50
|
+
require 'url_signer/rails' if defined?(Rails)
|
data/lib/url_signer/base.rb
CHANGED
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'url_signer'
|
2
|
+
require 'ostruct'
|
3
|
+
require 'rails/railtie'
|
4
|
+
|
5
|
+
module UrlSigner
|
6
|
+
|
7
|
+
module Rails
|
8
|
+
module ControllerHelpers
|
9
|
+
extend ActiveSupport::Concern
|
10
|
+
|
11
|
+
included do
|
12
|
+
helper_method :sign_url
|
13
|
+
end
|
14
|
+
|
15
|
+
# Sign a +url+.
|
16
|
+
#
|
17
|
+
# @signed_url = sign_url(some_route_helper_url)
|
18
|
+
#
|
19
|
+
# Can also be used as a view helper:
|
20
|
+
#
|
21
|
+
# <%= link_to 'Some secret', sign_url(some_secret_action_url) %>
|
22
|
+
#
|
23
|
+
# For +options+, see UrlSigner#sign.
|
24
|
+
def sign_url(url, options={})
|
25
|
+
options = url_signer_options(options)
|
26
|
+
UrlSigner.sign(url, options).to_s
|
27
|
+
end
|
28
|
+
|
29
|
+
# Verify a +url+.
|
30
|
+
#
|
31
|
+
# class MyController < ActionController::Base
|
32
|
+
# def my_action
|
33
|
+
#
|
34
|
+
# # verify the validity of the current called url
|
35
|
+
# current_url_valid = signature_valid?
|
36
|
+
#
|
37
|
+
# # or with another url
|
38
|
+
# orher_url_valid = signature_valid?(orher_url)
|
39
|
+
#
|
40
|
+
# end
|
41
|
+
# end
|
42
|
+
#
|
43
|
+
# For +options+, see UrlSigner#valid?.
|
44
|
+
def signature_valid?(url=nil, options={})
|
45
|
+
url ||= request.url
|
46
|
+
options = url_signer_options(options)
|
47
|
+
UrlSigner.valid?(url, options)
|
48
|
+
end
|
49
|
+
|
50
|
+
# Verify the current url and call #signature_invalid! on failure.
|
51
|
+
# This method is intended to be used in a before action.
|
52
|
+
#
|
53
|
+
# class MyController < ActionController::Base
|
54
|
+
# before_action :verify_signature!
|
55
|
+
#
|
56
|
+
# def secure_action
|
57
|
+
# # can only be accessed from a signed url
|
58
|
+
# end
|
59
|
+
# end
|
60
|
+
def verify_signature!
|
61
|
+
signature_invalid! unless signature_valid?
|
62
|
+
end
|
63
|
+
|
64
|
+
# Called when an action is called with an invalid signature attached.
|
65
|
+
# Will be overridden to enhance behaviour:
|
66
|
+
#
|
67
|
+
# class MyController < ActionController::Base
|
68
|
+
# before_action :verify_signature!
|
69
|
+
#
|
70
|
+
# # ...
|
71
|
+
#
|
72
|
+
# def signature_invalid!
|
73
|
+
# redirect_to root_path, notice: 'you URL is not valid anymore'
|
74
|
+
# end
|
75
|
+
# end
|
76
|
+
def signature_invalid!
|
77
|
+
head :forbidden
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
def url_signer_options(options={}) # :nodoc:
|
83
|
+
defaults = ::Rails.configuration.url_signer.defaults
|
84
|
+
defaults.to_h.merge(options)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
class Railtie < ::Rails::Railtie # :nodoc:
|
89
|
+
config.url_signer = ActiveSupport::OrderedOptions.new
|
90
|
+
|
91
|
+
# setup sensible defaults
|
92
|
+
config.url_signer.key = ENV['URL_SIGNING_KEY']
|
93
|
+
config.url_signer.hash_method = Digest::SHA1
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
ActionController::Base.send(:include, UrlSigner::Rails::ControllerHelpers)
|
data/lib/url_signer/signer.rb
CHANGED
@@ -8,6 +8,8 @@ module URI
|
|
8
8
|
#
|
9
9
|
# url = URI.parse('http://google.fr')
|
10
10
|
# signed_url = url.signed
|
11
|
+
#
|
12
|
+
# for +options+ see UrlSigner#sign.
|
11
13
|
def signed(*options)
|
12
14
|
UrlSigner.sign(self, *options)
|
13
15
|
end
|
@@ -16,6 +18,8 @@ module URI
|
|
16
18
|
#
|
17
19
|
# signed_url = URI.parse('http://google.fr').signed
|
18
20
|
# signed_url.signature_valid? # => true
|
21
|
+
#
|
22
|
+
# for +options+ see UrlSigner#verify.
|
19
23
|
def signature_valid?(*options)
|
20
24
|
UrlSigner.valid?(self, *options)
|
21
25
|
end
|
data/lib/url_signer/verifier.rb
CHANGED
data/lib/url_signer/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: url_signer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.3'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aurélien Noce
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -68,15 +68,14 @@ files:
|
|
68
68
|
- Rakefile
|
69
69
|
- lib/url_signer.rb
|
70
70
|
- lib/url_signer/base.rb
|
71
|
-
- lib/url_signer/
|
72
|
-
- lib/url_signer/extensions/uri.rb
|
71
|
+
- lib/url_signer/rails.rb
|
73
72
|
- lib/url_signer/signer.rb
|
73
|
+
- lib/url_signer/uri.rb
|
74
74
|
- lib/url_signer/verifier.rb
|
75
75
|
- lib/url_signer/version.rb
|
76
76
|
- spec/base_spec.rb
|
77
77
|
- spec/signer_spec.rb
|
78
78
|
- spec/spec_helper.rb
|
79
|
-
- spec/string_spec.rb
|
80
79
|
- spec/uri_spec.rb
|
81
80
|
- spec/url_signer_spec.rb
|
82
81
|
- spec/verifier_spec.rb
|
@@ -109,7 +108,6 @@ test_files:
|
|
109
108
|
- spec/base_spec.rb
|
110
109
|
- spec/signer_spec.rb
|
111
110
|
- spec/spec_helper.rb
|
112
|
-
- spec/string_spec.rb
|
113
111
|
- spec/uri_spec.rb
|
114
112
|
- spec/url_signer_spec.rb
|
115
113
|
- spec/verifier_spec.rb
|
@@ -1,13 +0,0 @@
|
|
1
|
-
require 'uri'
|
2
|
-
require 'url_signer'
|
3
|
-
|
4
|
-
class String
|
5
|
-
|
6
|
-
# Return a signed <tt>URI</tt> form the current <tt>String</tt>.
|
7
|
-
#
|
8
|
-
# 'http://google.fr'.to_signed_uri # => <URI::HTTP...>
|
9
|
-
def to_signed_uri(*options)
|
10
|
-
UrlSigner.sign(self, *options)
|
11
|
-
end
|
12
|
-
|
13
|
-
end
|
data/spec/string_spec.rb
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe "String extension" do
|
4
|
-
let(:url_string) { 'http://google.fr' }
|
5
|
-
let(:url) { URI.parse(url_string) }
|
6
|
-
let(:signed_url) { UrlSigner.sign(url, key: 'toto') }
|
7
|
-
|
8
|
-
describe "#to_signed_uri" do
|
9
|
-
it "returns a signed version of the URI" do
|
10
|
-
expect(url_string.to_signed_uri(key: 'toto')).to eq(signed_url)
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
end # URI extension
|