prefixed_ids 1.2.2 → 1.4.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: 38b6a59341fa7c4d02fdaba7f6eb738cdb5730a9aa6caca89ffb89bfa9dc4740
4
- data.tar.gz: ae7de74cf643796ce5fdafe5e0b7d0db98cf60b761a52a960ad1122c14028b0a
3
+ metadata.gz: b5a5f802c0838ffb2538b43212061d569d0920356aed91e313865799c570d573
4
+ data.tar.gz: 6488ec773ddb3f15dfb48a9bc07ce4d39262feabb89eb8d3a5168e5ffdf52017
5
5
  SHA512:
6
- metadata.gz: 428331d371bbcc87c9d092297b315476ccd9e62548ff59efef59cb7a3574b256247f4dacf6c1edc3c2f72e553a742033493f51587af264707a77ca3d797cea9e
7
- data.tar.gz: af9b8a1f596b9b6d1720e8ec71b267421d6c0df5fd661e0b3b76765d8606bb929e5bf2a2bbe81a9fd853290e4e1a844ca7d3a63ad25001da9d9eb9beb3a98c0b
6
+ metadata.gz: 8b62b64c158bd670e814fbe92b7306a046b9f4e7933f494e5312ddb44f7a068fe9baaa579a11ad45b635aed0ec1dc83bec5b1699471ad550ed5e846a9125e446
7
+ data.tar.gz: a5233cee0e3fb432b1ff4e01bc60f6c33cc670df756bb09e315f26fd5d22c6b7325bc2fde0eb1369be21196d4b23e65850dadf10f0fc2ee927aa636b8067670b
data/README.md CHANGED
@@ -13,6 +13,8 @@ user_12345abcd
13
13
  acct_23lksjdg3
14
14
  ```
15
15
 
16
+ This gem works by hashing the record's original `:id` attribute using [`Hashids`](https://hashids.org/ruby/), which transforms numbers like 347 into a string like yr8. It uses the table's name and an optional additional salt to hash values, returning a string like `tablename_hashedvalue`.
17
+
16
18
  Inspired by [Stripe's prefixed IDs](https://stripe.com/docs/api) in their API.
17
19
 
18
20
  ## 🚀 Installation
@@ -34,13 +36,32 @@ end
34
36
 
35
37
  This will generate a value like `user_1234abcd`.
36
38
 
37
- To query using the prefixed ID, simply you can use either `find` or `find_by_prefixed_id`:
39
+ ##### Prefix ID Param
40
+
41
+ To retrieve the prefix ID, simply call:
42
+
43
+ ```ruby
44
+ User.to_param
45
+ ```
46
+
47
+ If `to_param` override is disabled:
48
+
49
+ ```ruby
50
+ User.prefix_id
51
+ ```
52
+
53
+ ##### Query by Prefixed ID
54
+
55
+ To query using the prefixed ID, you can use either `find`, `find_by_prefix_id`, or `find_by_prefix_id!`:
38
56
 
39
57
  ```ruby
40
58
  User.find("user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe")
41
59
  User.find_by_prefix_id("user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe")
42
60
  ```
43
61
 
62
+ ⚠️ Note that `find` still finds records by the primary key. Eg. `localhost/users/1` still works.
63
+ If you're targeting security issues by masking the ID, make sure to use `find_by_prefix_id` and [add a salt](#salt).
64
+
44
65
  We also override `to_param` by default so it'll be used in URLs automatically.
45
66
 
46
67
  To disable find and to_param overrides, simply pass in the options:
@@ -51,7 +72,26 @@ class User < ApplicationRecord
51
72
  end
52
73
  ```
53
74
 
54
- ### Generic lookup
75
+ ##### Salt
76
+
77
+ A salt is a secret value that makes it impossible to reverse engineer IDs. We recommend adding a salt to make your Prefix IDs unguessable.
78
+
79
+ ###### Global Salt
80
+
81
+ ```ruby
82
+ # config/initializers/prefixed_ids.rb
83
+ PrefixedIds.salt = "salt"
84
+ ```
85
+
86
+ ###### Per Model Salt
87
+
88
+ ```ruby
89
+ class User
90
+ has_prefix_id :user, salt: "usersalt"
91
+ end
92
+ ```
93
+
94
+ ### Generic Lookup By Prefix ID
55
95
 
56
96
  Imagine you have a prefixed ID but you don't know which model it belongs to.
57
97
 
@@ -62,13 +102,13 @@ PrefixedIds.find("acct_2iAnOP0xGDYk6dpe")
62
102
  #=> #<Account>
63
103
  ```
64
104
 
65
- ### Customizing
105
+ ### Customizing Prefix IDs
66
106
 
67
107
  You can customize the prefix, length, and attribute name for PrefixedIds.
68
108
 
69
109
  ```ruby
70
110
  class Account < ApplicationRecord
71
- has_prefix_id :acct, minimum_length: 32, override_find: false, to_param: false
111
+ has_prefix_id :acct, minimum_length: 32, override_find: false, override_param: false, salt: ""
72
112
  end
73
113
  ```
74
114
 
@@ -4,10 +4,10 @@ module PrefixedIds
4
4
 
5
5
  TOKEN = 123
6
6
 
7
- def initialize(model, prefix, minimum_length: PrefixedIds.minimum_length, alphabet: PrefixedIds.alphabet, delimiter: PrefixedIds.delimiter, **options)
7
+ def initialize(model, prefix, salt: PrefixedIds.salt, minimum_length: PrefixedIds.minimum_length, alphabet: PrefixedIds.alphabet, delimiter: PrefixedIds.delimiter, **options)
8
8
  @prefix = prefix.to_s
9
9
  @delimiter = delimiter.to_s
10
- @hashids = Hashids.new(model.table_name, minimum_length, alphabet)
10
+ @hashids = Hashids.new("#{model.table_name}#{salt}", minimum_length, alphabet)
11
11
  end
12
12
 
13
13
  def encode(id)
@@ -1,3 +1,3 @@
1
1
  module PrefixedIds
2
- VERSION = "1.2.2"
2
+ VERSION = "1.4.0"
3
3
  end
data/lib/prefixed_ids.rb CHANGED
@@ -11,6 +11,7 @@ module PrefixedIds
11
11
  mattr_accessor :delimiter, default: "_"
12
12
  mattr_accessor :alphabet, default: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
13
13
  mattr_accessor :minimum_length, default: 24
14
+ mattr_accessor :salt, default: ""
14
15
 
15
16
  mattr_accessor :models, default: {}
16
17
 
@@ -60,6 +61,14 @@ module PrefixedIds
60
61
  def find_by_prefix_id!(id)
61
62
  find_by!(id: _prefix_id.decode(id))
62
63
  end
64
+
65
+ def decode_prefix_id(id)
66
+ _prefix_id.decode(id)
67
+ end
68
+
69
+ def decode_prefix_ids(ids)
70
+ ids.map { |id| decode_prefix_id(id) }
71
+ end
63
72
  end
64
73
 
65
74
  def prefix_id
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prefixed_ids
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Oliver
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-03 00:00:00.000000000 Z
11
+ date: 2023-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -45,7 +45,7 @@ dependencies:
45
45
  - !ruby/object:Gem::Version
46
46
  version: 2.0.0
47
47
  - !ruby/object:Gem::Dependency
48
- name: standardrb
48
+ name: standard
49
49
  requirement: !ruby/object:Gem::Requirement
50
50
  requirements:
51
51
  - - ">="
@@ -86,7 +86,6 @@ files:
86
86
  - lib/prefixed_ids/engine.rb
87
87
  - lib/prefixed_ids/prefix_id.rb
88
88
  - lib/prefixed_ids/version.rb
89
- - lib/tasks/prefixed_ids_tasks.rake
90
89
  homepage: https://github.com/excid3/prefixed_ids
91
90
  licenses:
92
91
  - MIT
@@ -109,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
108
  - !ruby/object:Gem::Version
110
109
  version: '0'
111
110
  requirements: []
112
- rubygems_version: 3.2.3
111
+ rubygems_version: 3.4.7
113
112
  signing_key:
114
113
  specification_version: 4
115
114
  summary: Prefixed IDs generates IDs with friendly prefixes for your models
@@ -1,4 +0,0 @@
1
- # desc "Explaining what the task does"
2
- # task :prefixed_ids do
3
- # # Task goes here
4
- # end