Skip to content

Commit b4273f9

Browse files
Update README.md
1 parent 7670e75 commit b4273f9

File tree

1 file changed

+18
-38
lines changed

1 file changed

+18
-38
lines changed

README.md

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,8 @@ A lightweight library for encoding/decoding Rails request parameters.
66

77
Battle-tested at [Hansa](https://hansahq.com). Developed at [Primevise](https://primevise.com).
88

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>
1611

1712
---
1813

@@ -22,63 +17,48 @@ Battle-tested at [Hansa](https://hansahq.com). Developed at [Primevise](https://
2217

2318
Simply add the gem to your Gemfile by running the following command
2419

25-
```bash
26-
$ bundle add signed_params
2720
```
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
3722
```
3823

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-
4224
---
4325

4426
## Usage
4527

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.
4731

4832
#### Example
4933

5034
```ruby
5135
class RecordsController < ApplicationController
52-
has_signed_params :record_ids, only: :index
53-
5436
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)
5745
end
5846

5947
def new_public_link
6048
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:))
6350
end
6451
end
6552
```
6653

54+
> [!TIP]
55+
> You can use all sorts of datatypes when signing parameters. Strings, integers, arrays, objects - they all just work.
56+
6757
> [!CAUTION]
6858
> Avoid exposing sensitive data while using `signed_params`. Your application should still implement proper authentication and authorization.
6959
7060
---
7161

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-
8262
## License
8363

8464
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

0 commit comments

Comments
 (0)