signed_params 0.1.0 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 891a56fac43ec97d7619f289ffc82d565d0d363615b2273329592e29d1152108
4
- data.tar.gz: cb4b69abffc6625eee315f5575e916d326d833a6b1749eff5dab4af1da8598ec
3
+ metadata.gz: 7bb815e0b8cc4495a7f7714cbe16cf48ec4ec0704a43f78e87b262abf6df6434
4
+ data.tar.gz: 284ba299d0d2fb5215f821c9428d2df7f9e88006b603cd00877ebd463be09b15
5
5
  SHA512:
6
- metadata.gz: 9557c06ef1af6fc3ff0e253b44d13f4a5a12857cfb25e2d138477f58d24d6aa576c8a10816a010771f3a738593dc49aff9a98be8b99b5bd1e992bc72ad37afd3
7
- data.tar.gz: 912c6259c604c4c3d33403720ed74b0fad1769ba5f76283ddc163d7bcc7aee84711e555e5fe8d04a94f580035988d54a85f8dee5270690ab1a36f80d96db6c7c
6
+ metadata.gz: 3c46582d4a3e0aee5929930684677f7fc1a560d52a63937b30b7c1cf3899f2dcdeba2dd480a162f751329e3865e4d01e9fc0a785a4995c9158811913a18bca3d
7
+ data.tar.gz: 2a7752b203a38ef492eac4a44177a79138f4b909e801ff055db587867033ea4c24f2c34fecf1b77c5fb80bfeae772ce3003415322c197e062cd0b9ad47e5a14a
data/README.md CHANGED
@@ -6,13 +6,8 @@ A lightweight library for encoding/decoding Rails request parameters.
6
6
 
7
7
  Battle-tested at [Hansa](https://hansahq.com). Developed at [Primevise](https://primevise.com).
8
8
 
9
- <a href="https://rubygems.org/gems/signed_params">
10
- <img alt="signed_params GEM Version" src="https://img.shields.io/gem/v/signed_params?color=10b981&include_prereleases&logo=ruby&logoColor=f43f5e">
11
- </a>
12
-
13
- <a href="https://rubygems.org/gems/signed_params">
14
- <img alt="signed_params GEM Downloads" src="https://img.shields.io/gem/dt/signed_params?color=10b981&include_prereleases&logo=ruby&logoColor=f43f5e">
15
- </a>
9
+ <a href="https://rubygems.org/gems/signed_params"><img alt="signed_params GEM Version" src="https://img.shields.io/gem/v/signed_params?color=10b981&include_prereleases&logo=ruby&logoColor=f43f5e"></a>
10
+ <a href="https://rubygems.org/gems/signed_params"><img alt="signed_params GEM Downloads" src="https://img.shields.io/gem/dt/signed_params?color=10b981&include_prereleases&logo=ruby&logoColor=f43f5e"></a>
16
11
 
17
12
  ---
18
13
 
@@ -22,63 +17,48 @@ Battle-tested at [Hansa](https://hansahq.com). Developed at [Primevise](https://
22
17
 
23
18
  Simply add the gem to your Gemfile by running the following command
24
19
 
25
- ```bash
26
- $ bundle add signed_params
27
20
  ```
28
-
29
- #### Add to application
30
-
31
- After you have the gem installed, you include the functionality in `app/controllers/application_controller.rb`:
32
-
33
- ```ruby
34
- class ApplicationController < ActionController::Base
35
- include SignedParams::Concern
36
- end
21
+ bundle add signed_params
37
22
  ```
38
23
 
39
- > [!TIP]
40
- > 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.
41
-
42
24
  ---
43
25
 
44
26
  ## Usage
45
27
 
46
- 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.
28
+ The signed paramaters can be accesed via `params.signed`. It mirrors the behavior of Rails' [signed cookies](https://api.rubyonrails.org/classes/ActionDispatch/Cookies.html).
29
+
30
+ Similarly, setting a signed parameter can be done with the `params.sign` method.
47
31
 
48
32
  #### Example
49
33
 
50
34
  ```ruby
51
35
  class RecordsController < ApplicationController
52
- has_signed_params :record_ids, only: :index
53
-
54
36
  def index
55
- # The record_ids param is automatically decoded
56
- @records = Record.find(params[:record_ids])
37
+
38
+ # Using `params.signed` will return `nil` if the parameter is tampered
39
+ record_ids = params.signed[:record_ids]
40
+
41
+ # Using `params.signed.fetch` will raise `ActionController::Parameters::InvalidSignature` if the parameter is tampered
42
+ record_ids = params.signed.fetch(:record_ids)
43
+
44
+ @records = Record.find(record_ids)
57
45
  end
58
46
 
59
47
  def new_public_link
60
48
  record_ids = Record.last(8).pluck(:id)
61
- encoded_record_ids = sign_params(record_ids)
62
- # Your controller action logic that generates shareable public links
49
+ redirect_to records_path(params.sign(record_ids:))
63
50
  end
64
51
  end
65
52
  ```
66
53
 
54
+ > [!TIP]
55
+ > You can use all sorts of datatypes when signing parameters. Strings, integers, arrays, objects - they all just work.
56
+
67
57
  > [!CAUTION]
68
58
  > Avoid exposing sensitive data while using `signed_params`. Your application should still implement proper authentication and authorization.
69
59
 
70
60
  ---
71
61
 
72
- ## Configuration
73
-
74
- `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.
75
-
76
- ```ruby
77
- SignedParams.configure do |config|
78
- config.verifier_secret = ENV["SIGNED_PARAMS_ENCODING_SECRET"] || "my-strong-and-private-signing-secret"
79
- end
80
- ```
81
-
82
62
  ## License
83
63
 
84
64
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ class SignedParams::Railtie < Rails::Railtie
2
+ initializer "parameters.signed.set_verifier" do |app|
3
+ ActionController::Parameters.include ActionController::Parameters::Signed::Integration
4
+ ActionController::Parameters.verifier = app.message_verifier :signed_parameters
5
+ end
6
+ end
@@ -1,3 +1,3 @@
1
1
  module SignedParams
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/signed_params.rb CHANGED
@@ -1,15 +1,29 @@
1
- require "signed_params/concern"
2
- require "signed_params/configuration"
3
1
  require "signed_params/version"
2
+ require "action_controller/metal/strong_parameters"
4
3
 
5
- module SignedParams
6
- class << self
7
- def configuration
8
- @configuration ||= Configuration.new
9
- end
4
+ class ActionController::Parameters::Signed < Data.define(:verifier, :params)
5
+ ActionController::Parameters::InvalidSignature = Class.new StandardError
10
6
 
11
- def configure
12
- yield configuration
13
- end
7
+ def [](key)
8
+ verifier.verified(params[key])
14
9
  end
10
+
11
+ def fetch(key)
12
+ verifier.verify(params[key])
13
+ rescue ActiveSupport::MessageVerifier::InvalidSignature
14
+ raise ActionController::Parameters::InvalidSignature
15
+ end
16
+ end
17
+
18
+ module ActionController::Parameters::Signed::Integration
19
+ def self.included(parameters)
20
+ parameters.mattr_accessor :verifier
21
+ end
22
+
23
+ def sign(**params)
24
+ params.transform_values { verifier.generate _1 }
25
+ end
26
+ def signed = @signed ||= ActionController::Parameters::Signed.new(verifier, self)
15
27
  end
28
+
29
+ require_relative "signed_params/railtie" if defined?(Rails::Railtie)
metadata CHANGED
@@ -1,16 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: signed_params
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Elvinas Predkelis
8
8
  bindir: bin
9
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
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: actionpack
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '6.1'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '6.1'
26
+ description: A lightweight library for encoding/decoding Rails request parameters.
14
27
  email:
15
28
  - elvinas@primevise.com
16
29
  executables: []
@@ -20,8 +33,7 @@ files:
20
33
  - LICENCE
21
34
  - README.md
22
35
  - lib/signed_params.rb
23
- - lib/signed_params/concern.rb
24
- - lib/signed_params/configuration.rb
36
+ - lib/signed_params/railtie.rb
25
37
  - lib/signed_params/version.rb
26
38
  homepage: https://github.com/elvinaspredkelis/signed_params
27
39
  licenses:
@@ -44,8 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
44
56
  - !ruby/object:Gem::Version
45
57
  version: '0'
46
58
  requirements: []
47
- rubygems_version: 3.6.2
59
+ rubygems_version: 3.6.7
48
60
  specification_version: 4
49
- summary: A Rails concern that provides automatic encoding/decoding of signed parameters
50
- to prevent tampering
61
+ summary: A lightweight library for encoding/decoding Rails request parameters.
51
62
  test_files: []
@@ -1,38 +0,0 @@
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
@@ -1,21 +0,0 @@
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