signed_params 0.0.1
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 +7 -0
- data/LICENCE +21 -0
- data/README.md +75 -0
- data/lib/signed_params/concern.rb +38 -0
- data/lib/signed_params/configuration.rb +21 -0
- data/lib/signed_params/version.rb +3 -0
- data/lib/signed_params.rb +15 -0
- metadata +51 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8c20a32fb7717fdc82f737a65b377486beffbc9faa4852d36563c9c4828a9f3c
|
4
|
+
data.tar.gz: d64e43dde0ed9bc8294dc1b4d44b3157fba803ca1a83c2576c1172a0c4f30bbe
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 830bc743e8226ae34d62686bb305274c35a0a4abe4f399de5076cfc55e563a5df889efae64d924694a853e8cad94e6a4370c429c41ba2e6c660bc00a558160b1
|
7
|
+
data.tar.gz: 3a026ec3ee4eb1103e0a29eed1989a4e7f2637e39599ae3d675ef2965b59a2b7db36c6cd15980fa9096046d91de0cc1ce5f1b347a030624ae758a8e3077b7b16
|
data/LICENCE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 Elvinas Predkelis
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# Signed Params
|
2
|
+
|
3
|
+
A small Rails controller concern that allows encoding and decoding parameters. Such parameters are protected against tampering and safe to share with the internet.
|
4
|
+
|
5
|
+
`signed_params` are great for generating sharable links and/or mitigating web scrapers.
|
6
|
+
|
7
|
+
Battle tested at [Hansa](https://hansahq.com). Developed at [Primevise](https://primevise.com).
|
8
|
+
|
9
|
+
---
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
#### Add gem
|
14
|
+
|
15
|
+
Simply add the gem to your Gemfile by running the following command
|
16
|
+
|
17
|
+
```bash
|
18
|
+
$ bundle add signed_params
|
19
|
+
```
|
20
|
+
|
21
|
+
---
|
22
|
+
|
23
|
+
#### Add it to your application
|
24
|
+
|
25
|
+
After you have the gem installed, you include the functionality in `app/controllers/application_controller.rb`:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
class ApplicationController < ActionController::Base
|
29
|
+
include SignedParams::Concern
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
> [!TIP]
|
34
|
+
> You can also include the concern only in the controllers you seem fit. Adding the concern to the `ApplicationController` is a "forget about it" approach.
|
35
|
+
|
36
|
+
---
|
37
|
+
|
38
|
+
## Usage
|
39
|
+
|
40
|
+
You can encode your parameters with a `sign_param` helper method. Specify which params you want to decode by specifying them in the `has_signed_params` class method.
|
41
|
+
|
42
|
+
#### Example
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
class RecordsController < ApplicationController
|
46
|
+
has_signed_params :record_ids, only: :index
|
47
|
+
|
48
|
+
def index
|
49
|
+
# The record_ids param is automatically decoded
|
50
|
+
@records = Record.find(params[:record_ids])
|
51
|
+
end
|
52
|
+
|
53
|
+
def new_public_link
|
54
|
+
record_ids = Record.last(8).pluck(:id)
|
55
|
+
encoded_record_ids = sign_params(record_ids)
|
56
|
+
# Your controller action logic that generates shareable public links
|
57
|
+
end
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
61
|
+
---
|
62
|
+
|
63
|
+
## Configuration
|
64
|
+
|
65
|
+
`signed_params` uses Rails' [ActiveSupport::MessageVerifier](https://api.rubyonrails.org/classes/ActiveSupport/MessageVerifier.html) under the hood to encode the params. You can adjust the secret used for encoding by adding an initializer.
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
SignedParams.configure do |config|
|
69
|
+
config.verifier_secret = ENV["SIGNED_PARAMS_ENCODING_SECRET"] || "my-strong-and-private-signing-secret"
|
70
|
+
end
|
71
|
+
```
|
72
|
+
|
73
|
+
## License
|
74
|
+
|
75
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module SignedParams
|
2
|
+
module Concern
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
class_attribute :signed_param_keys, default: []
|
7
|
+
helper_method :sign_param
|
8
|
+
end
|
9
|
+
|
10
|
+
class_methods do
|
11
|
+
def has_signed_params(*keys, **attributes)
|
12
|
+
self.signed_param_keys = keys
|
13
|
+
before_action :decode_signed_params
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def sign_param(value)
|
18
|
+
signed_params_verifier.generate(value)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def decode_signed_params
|
24
|
+
return unless signed_param_keys.length
|
25
|
+
signed_param_keys.each do |key|
|
26
|
+
begin
|
27
|
+
params[key] = signed_params_verifier.verify(params[key])
|
28
|
+
rescue ActiveSupport::MessageVerifier::InvalidSignature
|
29
|
+
params[key] = nil
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def signed_params_verifier
|
35
|
+
@signed_params_verifier ||= Rails.application.message_verifier(SignedParams.configuration.verifier_secret)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module SignedParams
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :verifier_secret
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@verifier_secret = :signed_params
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class << self
|
11
|
+
attr_accessor :configuration
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.configuration
|
15
|
+
@configuration ||= Configuration.new
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.configure
|
19
|
+
yield(configuration)
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "signed_params/concern"
|
2
|
+
require "signed_params/configuration"
|
3
|
+
require "signed_params/version"
|
4
|
+
|
5
|
+
module SignedParams
|
6
|
+
class << self
|
7
|
+
def configuration
|
8
|
+
@configuration ||= Configuration.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def configure
|
12
|
+
yield configuration
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: signed_params
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Elvinas Predkelis
|
8
|
+
bindir: bin
|
9
|
+
cert_chain: []
|
10
|
+
date: 2025-05-26 00:00:00.000000000 Z
|
11
|
+
dependencies: []
|
12
|
+
description: A Rails concern that provides automatic encoding/decoding of signed parameters
|
13
|
+
to prevent tampering
|
14
|
+
email:
|
15
|
+
- elvinas@primevise.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- LICENCE
|
21
|
+
- README.md
|
22
|
+
- lib/signed_params.rb
|
23
|
+
- lib/signed_params/concern.rb
|
24
|
+
- lib/signed_params/configuration.rb
|
25
|
+
- lib/signed_params/version.rb
|
26
|
+
homepage: https://github.com/elvinaspredkelis/signed_params
|
27
|
+
licenses:
|
28
|
+
- MIT
|
29
|
+
metadata:
|
30
|
+
homepage_uri: https://github.com/elvinaspredkelis/signed_params
|
31
|
+
source_code_uri: https://github.com/elvinaspredkelis/signed_params
|
32
|
+
changelog_uri: https://github.com/elvinaspredkelis/signed_params/blob/main/CHANGELOG.md
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.0.0
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
requirements: []
|
47
|
+
rubygems_version: 3.6.2
|
48
|
+
specification_version: 4
|
49
|
+
summary: A Rails concern that provides automatic encoding/decoding of signed parameters
|
50
|
+
to prevent tampering
|
51
|
+
test_files: []
|