url_signer 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 56366ad2bd9634d9a762f6e0759dba5738656f18
4
- data.tar.gz: 9917576c556cfe739a5c4a90a3a126041f86a665
3
+ metadata.gz: a6cf7635deb5dc16a22e44a26652d737bea127f6
4
+ data.tar.gz: b386a8d8a59ef637b68b7430a0760c821a2aa018
5
5
  SHA512:
6
- metadata.gz: d38f39b64abaf780b9d366c399b359487772083a0c5f02b6b93c108c6414c233d11f824cc2a6ef4c536a36cbaa092a4f1102715ca3e5ad6d355968420f4d4c9d
7
- data.tar.gz: 7f0c6eb90f44434f807c363048f686fb0d5a12343898111f3fbf010dad6a5b62614b4a3972c2fcf853f38d5998f0a5c2b2815b2e7a0869730d7028c36bdd184c
6
+ metadata.gz: 53f896dae067179575e8b197f6f991436796097dc8c253ae16fcfb454b0e4d25070946661f9f15970658e8375438ec895c02bedd0fc0d355e30b350f35f6812c
7
+ data.tar.gz: fde1aa26138de8ef4951dc19d427181e7be4fa507002142b7566d4f59b720a3733c40af0a5124029e1e543ac97f2dcdfffed1302125aab3fab7acbbbfe24afb6
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # UrlSign
1
+ # UrlSigner
2
2
  [![Build Status](https://travis-ci.org/ushu/url_signer.svg?branch=master)](https://travis-ci.org/ushu/url_signer)
3
3
  [![Code Climate](https://codeclimate.com/github/ushu/url_signer/badges/gpa.svg)](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
- >>> signed_url = UrlSigner.sign('http://google.fr?q=test', key='mykey')
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
- >>> UrlSigner.valid?(signed_url)
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
- # finally to check for signature authenticity
57
- signed_url.signature_valid?(key: 'test')
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 )
@@ -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/extensions/uri'
48
- # Insert helpers in String
49
- require 'url_signer/extensions/string'
48
+ require 'url_signer/uri'
49
+ # Insert helpers in Rails controllers
50
+ require 'url_signer/rails' if defined?(Rails)
@@ -4,7 +4,8 @@ require 'digest/hmac'
4
4
  require 'digest/sha1'
5
5
 
6
6
  module UrlSigner
7
- class Base < Struct.new(:url, :key, :hash_method)
7
+
8
+ class Base < Struct.new(:url, :key, :hash_method) # :nodoc:
8
9
 
9
10
  def initialize(url, key: nil, hash_method: nil)
10
11
  # load and check url
@@ -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)
@@ -1,7 +1,7 @@
1
1
  require 'url_signer/base'
2
2
 
3
3
  module UrlSigner
4
- class Signer < Base
4
+ class Signer < Base # :nodoc:
5
5
 
6
6
  def sign
7
7
  raise "this URL is already signed !" if signed?
@@ -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
@@ -1,7 +1,7 @@
1
1
  require 'url_signer/base'
2
2
 
3
3
  module UrlSigner
4
- class Verifier < Base
4
+ class Verifier < Base # :nodoc:
5
5
 
6
6
  def valid?
7
7
  return false unless signed?
@@ -1,3 +1,3 @@
1
1
  module UrlSigner
2
- VERSION = "0.2"
2
+ VERSION = "0.3"
3
3
  end
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.2'
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: 2014-12-30 00:00:00.000000000 Z
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/extensions/string.rb
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
@@ -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