signed_params 0.0.1 → 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: 8c20a32fb7717fdc82f737a65b377486beffbc9faa4852d36563c9c4828a9f3c
4
- data.tar.gz: d64e43dde0ed9bc8294dc1b4d44b3157fba803ca1a83c2576c1172a0c4f30bbe
3
+ metadata.gz: 7bb815e0b8cc4495a7f7714cbe16cf48ec4ec0704a43f78e87b262abf6df6434
4
+ data.tar.gz: 284ba299d0d2fb5215f821c9428d2df7f9e88006b603cd00877ebd463be09b15
5
5
  SHA512:
6
- metadata.gz: 830bc743e8226ae34d62686bb305274c35a0a4abe4f399de5076cfc55e563a5df889efae64d924694a853e8cad94e6a4370c429c41ba2e6c660bc00a558160b1
7
- data.tar.gz: 3a026ec3ee4eb1103e0a29eed1989a4e7f2637e39599ae3d675ef2965b59a2b7db36c6cd15980fa9096046d91de0cc1ce5f1b347a030624ae758a8e3077b7b16
6
+ metadata.gz: 3c46582d4a3e0aee5929930684677f7fc1a560d52a63937b30b7c1cf3899f2dcdeba2dd480a162f751329e3865e4d01e9fc0a785a4995c9158811913a18bca3d
7
+ data.tar.gz: 2a7752b203a38ef492eac4a44177a79138f4b909e801ff055db587867033ea4c24f2c34fecf1b77c5fb80bfeae772ce3003415322c197e062cd0b9ad47e5a14a
data/README.md CHANGED
@@ -1,10 +1,13 @@
1
1
  # Signed Params
2
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.
3
+ A lightweight library for encoding/decoding Rails request parameters.
4
4
 
5
- `signed_params` are great for generating sharable links and/or mitigating web scrapers.
5
+ `signed_params` are protected against tampering and safe to share with the internet. Great for generating sharable links and/or mitigating web scrapers.
6
6
 
7
- Battle tested at [Hansa](https://hansahq.com). Developed at [Primevise](https://primevise.com).
7
+ Battle-tested at [Hansa](https://hansahq.com). Developed at [Primevise](https://primevise.com).
8
+
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>
8
11
 
9
12
  ---
10
13
 
@@ -14,61 +17,47 @@ Battle tested at [Hansa](https://hansahq.com). Developed at [Primevise](https://
14
17
 
15
18
  Simply add the gem to your Gemfile by running the following command
16
19
 
17
- ```bash
18
- $ bundle add signed_params
19
20
  ```
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
21
+ bundle add signed_params
31
22
  ```
32
23
 
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
24
  ---
37
25
 
38
26
  ## Usage
39
27
 
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.
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.
41
31
 
42
32
  #### Example
43
33
 
44
34
  ```ruby
45
35
  class RecordsController < ApplicationController
46
- has_signed_params :record_ids, only: :index
47
-
48
36
  def index
49
- # The record_ids param is automatically decoded
50
- @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)
51
45
  end
52
46
 
53
47
  def new_public_link
54
48
  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
49
+ redirect_to records_path(params.sign(record_ids:))
57
50
  end
58
51
  end
59
52
  ```
60
53
 
61
- ---
62
-
63
- ## Configuration
54
+ > [!TIP]
55
+ > You can use all sorts of datatypes when signing parameters. Strings, integers, arrays, objects - they all just work.
64
56
 
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.
57
+ > [!CAUTION]
58
+ > Avoid exposing sensitive data while using `signed_params`. Your application should still implement proper authentication and authorization.
66
59
 
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
- ```
60
+ ---
72
61
 
73
62
  ## License
74
63
 
@@ -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.0.1"
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.0.1
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