sha256_seal 0.1.4 → 0.1.5

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
  SHA256:
3
- metadata.gz: 64db1f61edfc475755906217a51b68ae2fb6cc73b222464c57a5a8c2225d7ca5
4
- data.tar.gz: cb01c03364b3e87db6e8f8765c820bf97a56e52007313a8ca03d3843cf91bb93
3
+ metadata.gz: 2d5bc9bfba878593900947fc081b861ecd4470df6721e41645088276663351ac
4
+ data.tar.gz: 6cc71d89a3aef7555dcb4343753c862764014c5948961fc3167d9e50bccf0335
5
5
  SHA512:
6
- metadata.gz: ff9b8f59a96112330fd803d01f3cffb360aaf9cc3adcc30fa0f03a8a13db849bfc4a5b10c19648e78baa2a22141fabd645b3d100730afb6fc615e0589abbdb83
7
- data.tar.gz: 7439cb713a0696d19e129980d5269aa9d1af7a03b02b372888e57191b78ae78d5ea26337decf2561a1b4fff61f27cd3732e419362f59415c40c602eecff3fe26
6
+ metadata.gz: c309221d7e727a863b89a4faae7b9f417d5ea42f10c59c9adb7c09a4d992e04e77e58eb0bbc86912abc2676fd2f0f98561d687699caca82054b8a04ba59b4f7e
7
+ data.tar.gz: 04d5c6640cb92472856166bdfb2dbb205c89fe8d040b33a790fb153acd56167bf3dc7bb8eedb1ba0e65b5fc852f70d90b377a184f98129f14d82eff161c1d726
@@ -1,6 +1 @@
1
- Metrics/LineLength:
2
- Max: 176
3
-
4
- Metrics/BlockLength:
5
- Exclude:
6
- - 'fix/**/*'
1
+ inherit_from: .rubocop_todo.yml
@@ -0,0 +1,20 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2019-10-09 21:12:32 +0200 using RuboCop version 0.75.0.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 1
10
+ # Configuration parameters: CountComments, ExcludedMethods.
11
+ # ExcludedMethods: refine
12
+ Metrics/BlockLength:
13
+ Max: 32
14
+
15
+ # Offense count: 6
16
+ # Cop supports --auto-correct.
17
+ # Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
18
+ # URISchemes: http, https
19
+ Metrics/LineLength:
20
+ Max: 176
@@ -1,5 +1,22 @@
1
- sudo: false
1
+ ---
2
2
  language: ruby
3
+ sudo: false
4
+ cache: bundler
5
+ before_install:
6
+ - gem install bundler
7
+ script:
8
+ - bundle exec rubocop
9
+ - bundle exec rake test
3
10
  rvm:
4
- - 2.4.1
5
- before_install: gem install bundler -v 1.16.0.pre.2
11
+ - 2.3.8
12
+ - 2.4.5
13
+ - 2.5.3
14
+ - 2.6.3
15
+ - ruby-head
16
+ - jruby-head
17
+ - rbx-3
18
+ matrix:
19
+ allow_failures:
20
+ - rvm: ruby-head
21
+ - rvm: jruby-head
22
+ - rvm: rbx-3
@@ -1,60 +1,58 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sha256_seal (0.1.4)
4
+ sha256_seal (0.1.5)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
9
  ast (2.4.0)
10
- aw (0.1.1)
11
- defi (1.1.3)
12
- docile (1.3.1)
13
- fix (0.17.1)
14
- aw (~> 0.1.1)
15
- defi (~> 1.1.3)
16
- spectus (~> 3.0.6)
17
- jaro_winkler (1.5.1)
18
- json (2.1.0)
19
- matchi (1.0.2)
20
- parallel (1.12.1)
21
- parser (2.5.1.2)
10
+ aw (0.1.6)
11
+ defi (1.1.5)
12
+ docile (1.3.2)
13
+ fix (0.18.2)
14
+ aw (~> 0.1.6)
15
+ defi (~> 1.1.5)
16
+ spectus (~> 3.0.9)
17
+ jaro_winkler (1.5.3)
18
+ json (2.2.0)
19
+ matchi (1.0.4)
20
+ parallel (1.18.0)
21
+ parser (2.6.5.0)
22
22
  ast (~> 2.4.0)
23
- powerpack (0.1.2)
24
23
  rainbow (3.0.0)
25
- rake (12.3.1)
26
- rubocop (0.58.2)
24
+ rake (13.0.0)
25
+ rubocop (0.75.0)
27
26
  jaro_winkler (~> 1.5.1)
28
27
  parallel (~> 1.10)
29
- parser (>= 2.5, != 2.5.1.1)
30
- powerpack (~> 0.1)
28
+ parser (>= 2.6)
31
29
  rainbow (>= 2.2.2, < 4.0)
32
30
  ruby-progressbar (~> 1.7)
33
- unicode-display_width (~> 1.0, >= 1.0.1)
34
- ruby-progressbar (1.10.0)
35
- simplecov (0.16.1)
31
+ unicode-display_width (>= 1.4.0, < 1.7)
32
+ ruby-progressbar (1.10.1)
33
+ simplecov (0.17.1)
36
34
  docile (~> 1.1)
37
35
  json (>= 1.8, < 3)
38
36
  simplecov-html (~> 0.10.0)
39
37
  simplecov-html (0.10.2)
40
- spectus (3.0.6)
41
- aw (~> 0.1.1)
42
- defi (~> 1.1.1)
43
- matchi (~> 1.0.2)
44
- unicode-display_width (1.4.0)
45
- yard (0.9.16)
38
+ spectus (3.0.9)
39
+ aw (~> 0.1.6)
40
+ defi (~> 1.1.5)
41
+ matchi (~> 1.0.4)
42
+ unicode-display_width (1.6.0)
43
+ yard (0.9.20)
46
44
 
47
45
  PLATFORMS
48
46
  ruby
49
47
 
50
48
  DEPENDENCIES
51
- bundler (~> 1.16)
52
- fix (~> 0.17)
53
- rake (~> 12.3)
54
- rubocop (~> 0.58)
49
+ bundler (~> 2.0)
50
+ fix (~> 0.18)
51
+ rake (~> 13.0)
52
+ rubocop (~> 0.75)
55
53
  sha256_seal!
56
- simplecov (~> 0.16)
54
+ simplecov (~> 0.17)
57
55
  yard (~> 0.9)
58
56
 
59
57
  BUNDLED WITH
60
- 1.16.1
58
+ 2.0.2
data/README.md CHANGED
@@ -1,4 +1,6 @@
1
- # Sha256Seal
1
+ # Sha256 Seal 🔏
2
+
3
+ A tiny library to sign documents, and check their integrity.
2
4
 
3
5
  ## Installation
4
6
 
@@ -25,21 +27,97 @@ Sign values and verify signatures of values.
25
27
  In the context of a Web application, CSRF tokens could be embedded in URLs.
26
28
 
27
29
  ```ruby
28
- secret = 'secret'
30
+ SECRET = 'secret'
31
+
29
32
 
30
- value = '/~bob/.__SIGNATURE_HERE__/documents/'
31
- field = '__SIGNATURE_HERE__'
33
+ document_string = '/.__SIGNATURE_HERE__/accounts/42?editable=false'
34
+ signature_field = '__SIGNATURE_HERE__'
32
35
 
33
- builder = Sha256Seal::Builder.new(value, secret, field)
34
- builder.signed_value # => "/~bob/.8aa1d38b5c16d077d5ac1360c8a6f0248419ff5a3e6dca28a3233894ddcdf3c4/documents/"
36
+ builder = Sha256Seal::Builder.new(document_string, SECRET, signature_field)
35
37
  builder.signed_value? # => false
38
+ builder.signed_value # => "/.a31c3936f236684a8ebc51dcfef168ce124450d71ae1ec404552ec9e0090a8db/accounts/42?editable=false"
39
+
36
40
 
37
- value = '/~bob/.8aa1d38b5c16d077d5ac1360c8a6f0248419ff5a3e6dca28a3233894ddcdf3c4/documents/'
38
- field = '8aa1d38b5c16d077d5ac1360c8a6f0248419ff5a3e6dca28a3233894ddcdf3c4'
41
+ document_string = '/.a31c3936f236684a8ebc51dcfef168ce124450d71ae1ec404552ec9e0090a8db/accounts/42?editable=false'
42
+ signature_field = 'a31c3936f236684a8ebc51dcfef168ce124450d71ae1ec404552ec9e0090a8db'
39
43
 
40
- builder = Sha256Seal::Builder.new(value, secret, field)
41
- builder.signed_value # => "/~bob/.8aa1d38b5c16d077d5ac1360c8a6f0248419ff5a3e6dca28a3233894ddcdf3c4/documents/"
44
+ builder = Sha256Seal::Builder.new(document_string, SECRET, signature_field)
42
45
  builder.signed_value? # => true
46
+ builder.signed_value # => "/.a31c3936f236684a8ebc51dcfef168ce124450d71ae1ec404552ec9e0090a8db/accounts/42?editable=false"
47
+
48
+
49
+ document_string = '/.a31c3936f236684a8ebc51dcfef168ce124450d71ae1ec404552ec9e0090a8db/accounts/42?editable=true'
50
+ signature_field = 'a31c3936f236684a8ebc51dcfef168ce124450d71ae1ec404552ec9e0090a8db'
51
+
52
+ builder = Sha256Seal::Builder.new(document_string, SECRET, signature_field)
53
+ builder.signed_value? # => false
54
+ builder.signed_value # => "/.babd3a90b6bc2a4c0c7536a0c4804e5430a5a6df27d223c0f0102edb231de590/accounts/42?editable=true"
55
+ ```
56
+
57
+ ### Rails integration example
58
+
59
+ Environment variable:
60
+
61
+ ```txt
62
+ CSRF_SECRET_KEY=secret
63
+ ```
64
+
65
+ Route:
66
+
67
+ ```ruby
68
+ # config/routes.rb
69
+ Rails.application.routes.draw do
70
+ scope module: :verified_requests, path: '.:csrf', as: 'verified_request' do
71
+ get '/accounts/:id', to: 'accounts#show', as: 'account'
72
+ end
73
+ end
74
+ ```
75
+
76
+ Controller:
77
+
78
+ ```ruby
79
+ # app/controllers/verified_requests/base_controller.rb
80
+ class VerifiedRequests::BaseController < ApplicationController
81
+ # @see https://api.rubyonrails.org/classes/ActionController/RequestForgeryProtection.html#method-i-verified_request-3F
82
+ def verified_request?
83
+ secret = ENV.fetch('CSRF_SECRET_KEY')
84
+ document_string = request.original_url.force_encoding('utf-8')
85
+ signature_field = request.path_parameters.fetch(:csrf)
86
+
87
+ builder = Sha256Seal::Builder.new(document_string, secret, signature_field)
88
+ builder.signed_value? || Rails.env.test?
89
+ end
90
+
91
+ def signed_url(route_method, **options)
92
+ url_route_method = "#{route_method}_url".to_sym
93
+ incorrect_csrf = '__CSRF_SECRET_KEY__'
94
+ url_route_string = public_send(url_route_method, csrf: incorrect_csrf, **options)
95
+
96
+ replace_incorrect_csrf_by_correct_csrf(url_route_string, incorrect_csrf: incorrect_csrf)
97
+ end
98
+ helper_method :signed_url
99
+
100
+ def replace_incorrect_csrf_by_correct_csrf(value, incorrect_csrf:)
101
+ secret = ENV.fetch('CSRF_SECRET_KEY')
102
+ field = incorrect_csrf
103
+ builder = Sha256Seal::Builder.new(value, secret, field)
104
+ value = builder.signed_value
105
+ field = builder.send(:signature)
106
+ builder = Sha256Seal::Builder.new(value, secret, field)
107
+
108
+ builder.signed_value
109
+ end
110
+ end
111
+ ```
112
+
113
+ View:
114
+
115
+ ```ruby
116
+ # app/views/verified_requests/accounts/show.html.erb
117
+
118
+ <%
119
+ signed_url(:verified_request_account, id: 'bob', admin: true) # => "http://0.0.0.0:5000/.405d7c8f14389c9ae7f1d97ff66699093bf2d89d13b4f4280a35d62f9e616259/accounts/bob?admin=true"
120
+ %>
43
121
  ```
44
122
 
45
123
  ## Contributing
@@ -1 +1 @@
1
- 0.1.4
1
+ 0.1.5
@@ -15,7 +15,9 @@ module Sha256Seal
15
15
 
16
16
  i = @value.scan(@field).length
17
17
 
18
- raise ArgumentError, "#{i} #{@field.inspect} occurrences instead of 1." unless i.equal?(1)
18
+ return if i.equal?(1)
19
+
20
+ raise ::ArgumentError, "#{i} #{@field.inspect} occurrences instead of 1."
19
21
  end
20
22
 
21
23
  def signed_value
@@ -29,7 +31,7 @@ module Sha256Seal
29
31
  private
30
32
 
31
33
  def signature
32
- Digest::SHA256.hexdigest(salt_value)
34
+ ::Digest::SHA256.hexdigest(salt_value)
33
35
  end
34
36
 
35
37
  def salt_value
@@ -18,10 +18,10 @@ Gem::Specification.new do |spec|
18
18
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
19
  spec.require_paths = ['lib']
20
20
 
21
- spec.add_development_dependency 'bundler', '~> 1.16'
22
- spec.add_development_dependency 'fix', '~> 0.17'
23
- spec.add_development_dependency 'rake', '~> 12.3'
24
- spec.add_development_dependency 'rubocop', '~> 0.58'
25
- spec.add_development_dependency 'simplecov', '~> 0.16'
21
+ spec.add_development_dependency 'bundler', '~> 2.0'
22
+ spec.add_development_dependency 'fix', '~> 0.18'
23
+ spec.add_development_dependency 'rake', '~> 13.0'
24
+ spec.add_development_dependency 'rubocop', '~> 0.75'
25
+ spec.add_development_dependency 'simplecov', '~> 0.17'
26
26
  spec.add_development_dependency 'yard', '~> 0.9'
27
27
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sha256_seal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cyril Kato
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-08-25 00:00:00.000000000 Z
11
+ date: 2019-10-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,70 +16,70 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.16'
19
+ version: '2.0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.16'
26
+ version: '2.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: fix
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0.17'
33
+ version: '0.18'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0.17'
40
+ version: '0.18'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '12.3'
47
+ version: '13.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '12.3'
54
+ version: '13.0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rubocop
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0.58'
61
+ version: '0.75'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0.58'
68
+ version: '0.75'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: simplecov
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '0.16'
75
+ version: '0.17'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '0.16'
82
+ version: '0.17'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: yard
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -103,6 +103,7 @@ extra_rdoc_files: []
103
103
  files:
104
104
  - ".gitignore"
105
105
  - ".rubocop.yml"
106
+ - ".rubocop_todo.yml"
106
107
  - ".travis.yml"
107
108
  - Gemfile
108
109
  - Gemfile.lock
@@ -132,8 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
132
133
  - !ruby/object:Gem::Version
133
134
  version: '0'
134
135
  requirements: []
135
- rubyforge_project:
136
- rubygems_version: 2.7.3
136
+ rubygems_version: 3.0.3
137
137
  signing_key:
138
138
  specification_version: 4
139
139
  summary: Seal device with SHA-256 hash function.