rails_api_keys 0.2.0 → 0.3.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 +4 -4
- data/CHANGELOG.md +11 -1
- data/README.md +31 -4
- data/app/models/rails_api_keys/api_key.rb +21 -4
- data/db/migrate/20261125120000_add_allowed_ips_to_rails_api_keys_api_keys.rb +7 -0
- data/lib/generators/rails_api_keys/install/templates/create_rails_api_keys_api_keys.rb.tt +1 -0
- data/lib/rails_api_keys/authentication.rb +21 -5
- data/lib/rails_api_keys/ip_allowlist.rb +70 -0
- data/lib/rails_api_keys/owner.rb +7 -2
- data/lib/rails_api_keys/version.rb +1 -1
- data/lib/rails_api_keys.rb +1 -0
- metadata +7 -5
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c45e1c9af06f5623758b70a9b538d6b4d4b00bc866306e7c32d00cc80d3f61ec
|
|
4
|
+
data.tar.gz: 83fdf46bdd6c33ccb772c119cea2e2ff2d4a385e4d02a668b41fd5c39878a134
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: afbb1d31a939d90b11b3d8b7ef9c6653d2059a7c73eef84d1541ccbe7271a47209574edcbf0974799926996b4d1dc02b23f121685555b421b675afb5f365d919
|
|
7
|
+
data.tar.gz: d7388c243e87098b920df8fafea2cbad71345f24759b00c302dc0837ebe611b51c063baed435afb615c9504b293645b524195b3c58389122b2afe92c1ddfc414
|
data/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.3.0] - 2026-11-25
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Optional `allowed_ips` column (`json`, default `[]`) on `rails_api_keys_api_keys` supporting single IPv4/IPv6 addresses and CIDR ranges via `IPAddr`
|
|
15
|
+
- `allowed_ips:` optional keyword argument on `RailsApiKeys::ApiKey.generate_for!` and `owner.create_api_key!`
|
|
16
|
+
- `RailsApiKeys::ApiKey#allows_ip?` and IP allowlist enforcement in `authenticate_api_key!` (renders `403 Forbidden` without updating `last_used_at` when a request comes from a disallowed IP)
|
|
17
|
+
- `touch_last_used:` optional keyword argument on `RailsApiKeys::ApiKey.authenticate`
|
|
18
|
+
|
|
10
19
|
## [0.2.0] - 2026-08-10
|
|
11
20
|
|
|
12
21
|
### Added
|
|
@@ -32,6 +41,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
32
41
|
- `RailsApiKeys.configure` — optional `owner_class`, `token_prefix`, and `owner_active`
|
|
33
42
|
- Install generator (`rails_api_keys:install`) for migration and initializer
|
|
34
43
|
|
|
35
|
-
[Unreleased]: https://github.com/rubyroidlabs/rails_api_keys/compare/v0.
|
|
44
|
+
[Unreleased]: https://github.com/rubyroidlabs/rails_api_keys/compare/v0.3.0...HEAD
|
|
45
|
+
[0.3.0]: https://github.com/rubyroidlabs/rails_api_keys/compare/v0.2.0...v0.3.0
|
|
36
46
|
[0.2.0]: https://github.com/rubyroidlabs/rails_api_keys/compare/v0.1.0...v0.2.0
|
|
37
47
|
[0.1.0]: https://github.com/rubyroidlabs/rails_api_keys/releases/tag/v0.1.0
|
data/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# rails_api_keys
|
|
2
2
|
|
|
3
3
|
[](https://github.com/rubyroidlabs/rails_api_keys/actions/workflows/ci.yml)
|
|
4
|
+
[](https://badge.fury.io/rb/rails_api_keys)
|
|
4
5
|
|
|
5
6
|
Reusable Rails engine for personal API keys: create (reveal once), hash at rest, revoke, and authenticate via `Authorization: Bearer <token>`.
|
|
6
7
|
|
|
@@ -44,7 +45,21 @@ class Company < ApplicationRecord
|
|
|
44
45
|
end
|
|
45
46
|
```
|
|
46
47
|
|
|
47
|
-
That declares `has_many :api_keys` and allows the model to own keys. Build your own controllers/UI to create keys (show the raw token once), list them, and revoke.
|
|
48
|
+
That declares `has_many :api_keys` and allows the model to own keys. Build your own controllers/UI to create keys (show the raw token once), list them, and revoke — or use the optional companion gem below.
|
|
49
|
+
|
|
50
|
+
## Optional UI
|
|
51
|
+
|
|
52
|
+
For a ready-made HTML/JSON management UI (create, reveal once + copy, list active keys, revoke), install the companion gem [`rails_api_keys-ui`](https://github.com/rubyroidlabs/rails_api_keys-ui):
|
|
53
|
+
|
|
54
|
+

|
|
55
|
+
|
|
56
|
+
```ruby
|
|
57
|
+
# Gemfile
|
|
58
|
+
gem "rails_api_keys"
|
|
59
|
+
gem "rails_api_keys-ui"
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
See that repo for install generators, route helpers, embeddable panel, and configuration.
|
|
48
63
|
|
|
49
64
|
## Configuration
|
|
50
65
|
|
|
@@ -68,10 +83,16 @@ end
|
|
|
68
83
|
## Usage
|
|
69
84
|
|
|
70
85
|
```ruby
|
|
71
|
-
key, raw = current_user.create_api_key!(
|
|
86
|
+
key, raw = current_user.create_api_key!(
|
|
87
|
+
name: "Zapier",
|
|
88
|
+
permission: :read,
|
|
89
|
+
allowed_ips: [ "203.0.113.10", "10.0.0.0/24" ] # optional; empty allows any IP
|
|
90
|
+
)
|
|
72
91
|
# Show `raw` once — it cannot be recovered later.
|
|
73
92
|
|
|
74
93
|
RailsApiKeys::ApiKey.authenticate(raw) # => key or nil
|
|
94
|
+
key.allows_ip?("203.0.113.10") # => true
|
|
95
|
+
key.update!(allowed_ips: [ "10.0.0.0/24" ])
|
|
75
96
|
key.revoke!
|
|
76
97
|
```
|
|
77
98
|
|
|
@@ -91,7 +112,7 @@ Clients send:
|
|
|
91
112
|
Authorization: Bearer <raw_token>
|
|
92
113
|
```
|
|
93
114
|
|
|
94
|
-
`authenticate_api_key!` rejects missing/invalid tokens with 401, then enforces permission from the HTTP method: GET/HEAD require read; other methods require write (403 if the
|
|
115
|
+
`authenticate_api_key!` rejects missing/invalid tokens with 401, then checks `allowed_ips` against `request.remote_ip` and enforces permission from the HTTP method: GET/HEAD require read; other methods require write (403 if the IP or permission is disallowed, without updating `last_used_at`).
|
|
95
116
|
|
|
96
117
|
## Permissions
|
|
97
118
|
|
|
@@ -100,7 +121,7 @@ Authorization: Bearer <raw_token>
|
|
|
100
121
|
| `read` | allowed | forbidden |
|
|
101
122
|
| `read_write` | allowed | allowed |
|
|
102
123
|
|
|
103
|
-
Permissions are immutable after create. Revoke with `revoke!` (sets `revoked_at`).
|
|
124
|
+
Permissions are immutable after create. `allowed_ips` accepts single IPv4/IPv6 addresses or CIDR ranges (or a comma/newline-separated string), defaults to `[]` (unrestricted), and can be updated after create. Revoke with `revoke!` (sets `revoked_at`).
|
|
104
125
|
|
|
105
126
|
## Security notes
|
|
106
127
|
|
|
@@ -118,6 +139,12 @@ bin/rubocop
|
|
|
118
139
|
|
|
119
140
|
See [CHANGELOG.md](./CHANGELOG.md) for release notes. Agent-oriented notes live in [`AGENTS.md`](./AGENTS.md).
|
|
120
141
|
|
|
142
|
+
## Contributing
|
|
143
|
+
|
|
144
|
+
Bug reports and pull requests are welcome. See [CONTRIBUTING.md](./CONTRIBUTING.md) and the [Code of Conduct](./CODE_OF_CONDUCT.md).
|
|
145
|
+
|
|
146
|
+
To report a security vulnerability privately, see [SECURITY.md](./SECURITY.md).
|
|
147
|
+
|
|
121
148
|
## License
|
|
122
149
|
|
|
123
150
|
This project is licensed under the [MIT License](./LICENSE).
|
|
@@ -23,32 +23,35 @@ module RailsApiKeys
|
|
|
23
23
|
validates :token_digest, uniqueness: true
|
|
24
24
|
validate :owner_declares_has_api_keys
|
|
25
25
|
validate :permission_immutable, on: :update
|
|
26
|
+
validate :allowed_ips_must_be_valid
|
|
26
27
|
|
|
27
28
|
scope :active, -> { where(revoked_at: nil) }
|
|
28
29
|
|
|
29
30
|
class << self
|
|
30
31
|
# Creates a key and returns +[record, raw_token]+. Show +raw_token+ once.
|
|
31
|
-
def generate_for!(owner:, name:, permission:)
|
|
32
|
+
def generate_for!(owner:, name:, permission:, allowed_ips: [])
|
|
32
33
|
raw_token = build_raw_token
|
|
33
34
|
record = create!(
|
|
34
35
|
owner: owner,
|
|
35
36
|
name: name,
|
|
36
37
|
permission: permission,
|
|
38
|
+
allowed_ips: allowed_ips,
|
|
37
39
|
token_digest: digest(raw_token),
|
|
38
40
|
token_display_prefix: display_prefix_for(raw_token)
|
|
39
41
|
)
|
|
40
42
|
[ record, raw_token ]
|
|
41
43
|
end
|
|
42
44
|
|
|
43
|
-
# Returns an active key for +raw_token+, or +nil+. Touches +last_used_at
|
|
44
|
-
|
|
45
|
+
# Returns an active key for +raw_token+, or +nil+. Touches +last_used_at+
|
|
46
|
+
# unless +touch_last_used: false+.
|
|
47
|
+
def authenticate(raw_token, touch_last_used: true)
|
|
45
48
|
return if raw_token.blank?
|
|
46
49
|
|
|
47
50
|
key = active.find_by(token_digest: digest(raw_token))
|
|
48
51
|
return unless key
|
|
49
52
|
return unless RailsApiKeys.configuration.owner_active?(key.owner)
|
|
50
53
|
|
|
51
|
-
key.touch_last_used!
|
|
54
|
+
key.touch_last_used! if touch_last_used
|
|
52
55
|
key
|
|
53
56
|
end
|
|
54
57
|
|
|
@@ -65,6 +68,10 @@ module RailsApiKeys
|
|
|
65
68
|
end
|
|
66
69
|
end
|
|
67
70
|
|
|
71
|
+
def allowed_ips=(value)
|
|
72
|
+
super(IpAllowlist.new(value).entries)
|
|
73
|
+
end
|
|
74
|
+
|
|
68
75
|
# Soft-revokes the key so {.authenticate} no longer accepts it.
|
|
69
76
|
def revoke!
|
|
70
77
|
update!(revoked_at: Time.current)
|
|
@@ -86,12 +93,22 @@ module RailsApiKeys
|
|
|
86
93
|
read_write?
|
|
87
94
|
end
|
|
88
95
|
|
|
96
|
+
def allows_ip?(ip)
|
|
97
|
+
IpAllowlist.new(allowed_ips).allows?(ip)
|
|
98
|
+
end
|
|
99
|
+
|
|
89
100
|
def touch_last_used!
|
|
90
101
|
update_column(:last_used_at, Time.current)
|
|
91
102
|
end
|
|
92
103
|
|
|
93
104
|
private
|
|
94
105
|
|
|
106
|
+
def allowed_ips_must_be_valid
|
|
107
|
+
IpAllowlist.new(allowed_ips).invalid_entries.each do |entry|
|
|
108
|
+
errors.add(:allowed_ips, "contains invalid IP address or CIDR: #{entry}")
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
95
112
|
def owner_declares_has_api_keys
|
|
96
113
|
return if owner.blank?
|
|
97
114
|
return if RailsApiKeys.owner_class?(owner.class)
|
|
@@ -7,6 +7,7 @@ class CreateRailsApiKeysApiKeys < ActiveRecord::Migration[<%= ActiveRecord::Migr
|
|
|
7
7
|
t.string :token_digest, null: false
|
|
8
8
|
t.string :token_display_prefix, null: false
|
|
9
9
|
t.string :permission, null: false, default: "read"
|
|
10
|
+
t.json :allowed_ips, null: false, default: []
|
|
10
11
|
t.references :owner, polymorphic: true, null: false, index: true
|
|
11
12
|
t.datetime :revoked_at
|
|
12
13
|
t.datetime :last_used_at
|
|
@@ -20,11 +20,11 @@ module RailsApiKeys
|
|
|
20
20
|
|
|
21
21
|
private
|
|
22
22
|
|
|
23
|
-
# Authenticates the Bearer token and enforces method-based permission.
|
|
24
|
-
# Renders 401 on auth failure, 403 when the
|
|
23
|
+
# Authenticates the Bearer token and enforces IP allowlist and method-based permission.
|
|
24
|
+
# Renders 401 on auth failure, 403 when the IP or permission is disallowed.
|
|
25
25
|
def authenticate_api_key!
|
|
26
26
|
token = bearer_token
|
|
27
|
-
key = RailsApiKeys::ApiKey.authenticate(token)
|
|
27
|
+
key = RailsApiKeys::ApiKey.authenticate(token, touch_last_used: false)
|
|
28
28
|
|
|
29
29
|
unless key
|
|
30
30
|
render_api_unauthorized
|
|
@@ -33,7 +33,19 @@ module RailsApiKeys
|
|
|
33
33
|
|
|
34
34
|
@current_api_key = key
|
|
35
35
|
@current_api_owner = key.owner
|
|
36
|
-
|
|
36
|
+
|
|
37
|
+
return false unless enforce_api_ip!
|
|
38
|
+
return false unless enforce_api_permission!
|
|
39
|
+
|
|
40
|
+
current_api_key.touch_last_used!
|
|
41
|
+
true
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def enforce_api_ip!
|
|
45
|
+
return true if current_api_key.allows_ip?(request.remote_ip)
|
|
46
|
+
|
|
47
|
+
render_api_forbidden
|
|
48
|
+
false
|
|
37
49
|
end
|
|
38
50
|
|
|
39
51
|
def enforce_api_permission!
|
|
@@ -46,7 +58,7 @@ module RailsApiKeys
|
|
|
46
58
|
|
|
47
59
|
return true if allowed
|
|
48
60
|
|
|
49
|
-
|
|
61
|
+
render_api_forbidden
|
|
50
62
|
false
|
|
51
63
|
end
|
|
52
64
|
|
|
@@ -60,5 +72,9 @@ module RailsApiKeys
|
|
|
60
72
|
def render_api_unauthorized
|
|
61
73
|
render json: { error: "Unauthorized" }, status: :unauthorized
|
|
62
74
|
end
|
|
75
|
+
|
|
76
|
+
def render_api_forbidden
|
|
77
|
+
render json: { error: "Forbidden" }, status: :forbidden
|
|
78
|
+
end
|
|
63
79
|
end
|
|
64
80
|
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "ipaddr"
|
|
4
|
+
|
|
5
|
+
module RailsApiKeys
|
|
6
|
+
# Normalizes, validates, and matches IPv4/IPv6 addresses and CIDR ranges.
|
|
7
|
+
class IpAllowlist
|
|
8
|
+
attr_reader :entries
|
|
9
|
+
|
|
10
|
+
def initialize(raw_entries)
|
|
11
|
+
@entries = normalize(raw_entries)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def invalid_entries
|
|
15
|
+
entries.reject { |entry| valid_entry?(entry) }
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def valid?
|
|
19
|
+
invalid_entries.empty?
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def allows?(ip)
|
|
23
|
+
return true if entries.empty?
|
|
24
|
+
return false if ip.blank?
|
|
25
|
+
|
|
26
|
+
candidate = parse_candidate(ip)
|
|
27
|
+
return false unless candidate
|
|
28
|
+
|
|
29
|
+
entries.any? { |entry| matches_entry?(entry, candidate) }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def normalize(value)
|
|
35
|
+
raw =
|
|
36
|
+
case value
|
|
37
|
+
when nil
|
|
38
|
+
[]
|
|
39
|
+
when String
|
|
40
|
+
value.split(/[\r\n,]+/)
|
|
41
|
+
when Array
|
|
42
|
+
value
|
|
43
|
+
else
|
|
44
|
+
[ value ]
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
raw.filter_map { |entry| entry.to_s.strip.presence }.uniq
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def valid_entry?(entry)
|
|
51
|
+
IPAddr.new(entry.to_s)
|
|
52
|
+
true
|
|
53
|
+
rescue IPAddr::InvalidAddressError, IPAddr::InvalidPrefixError
|
|
54
|
+
false
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def parse_candidate(ip)
|
|
58
|
+
candidate = IPAddr.new(ip.to_s)
|
|
59
|
+
candidate.ipv4_mapped? ? candidate.native : candidate
|
|
60
|
+
rescue IPAddr::InvalidAddressError, IPAddr::InvalidPrefixError
|
|
61
|
+
nil
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def matches_entry?(entry, candidate)
|
|
65
|
+
IPAddr.new(entry).include?(candidate)
|
|
66
|
+
rescue IPAddr::InvalidAddressError, IPAddr::InvalidPrefixError
|
|
67
|
+
false
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
data/lib/rails_api_keys/owner.rb
CHANGED
|
@@ -17,8 +17,13 @@ module RailsApiKeys
|
|
|
17
17
|
RailsApiKeys.register_owner_class!(self)
|
|
18
18
|
|
|
19
19
|
# Creates a key for this owner and returns +[record, raw_token]+.
|
|
20
|
-
define_method(:create_api_key!) do |name:, permission
|
|
21
|
-
RailsApiKeys::ApiKey.generate_for!(
|
|
20
|
+
define_method(:create_api_key!) do |name:, permission:, allowed_ips: []|
|
|
21
|
+
RailsApiKeys::ApiKey.generate_for!(
|
|
22
|
+
owner: self,
|
|
23
|
+
name: name,
|
|
24
|
+
permission: permission,
|
|
25
|
+
allowed_ips: allowed_ips
|
|
26
|
+
)
|
|
22
27
|
end
|
|
23
28
|
end
|
|
24
29
|
end
|
data/lib/rails_api_keys.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rails_api_keys
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Pavel Pershko
|
|
@@ -43,6 +43,7 @@ files:
|
|
|
43
43
|
- app/models/rails_api_keys/application_record.rb
|
|
44
44
|
- config/routes.rb
|
|
45
45
|
- db/migrate/20260810120000_create_rails_api_keys_api_keys.rb
|
|
46
|
+
- db/migrate/20261125120000_add_allowed_ips_to_rails_api_keys_api_keys.rb
|
|
46
47
|
- lib/generators/rails_api_keys/install/install_generator.rb
|
|
47
48
|
- lib/generators/rails_api_keys/install/templates/create_rails_api_keys_api_keys.rb.tt
|
|
48
49
|
- lib/generators/rails_api_keys/install/templates/initializer.rb.tt
|
|
@@ -50,6 +51,7 @@ files:
|
|
|
50
51
|
- lib/rails_api_keys/authentication.rb
|
|
51
52
|
- lib/rails_api_keys/configuration.rb
|
|
52
53
|
- lib/rails_api_keys/engine.rb
|
|
54
|
+
- lib/rails_api_keys/ip_allowlist.rb
|
|
53
55
|
- lib/rails_api_keys/owner.rb
|
|
54
56
|
- lib/rails_api_keys/version.rb
|
|
55
57
|
homepage: https://github.com/rubyroidlabs/rails_api_keys
|
|
@@ -57,10 +59,10 @@ licenses:
|
|
|
57
59
|
- MIT
|
|
58
60
|
metadata:
|
|
59
61
|
homepage_uri: https://github.com/rubyroidlabs/rails_api_keys
|
|
60
|
-
source_code_uri: https://github.com/rubyroidlabs/rails_api_keys/tree/v0.
|
|
61
|
-
changelog_uri: https://github.com/rubyroidlabs/rails_api_keys/blob/v0.
|
|
62
|
+
source_code_uri: https://github.com/rubyroidlabs/rails_api_keys/tree/v0.3.0
|
|
63
|
+
changelog_uri: https://github.com/rubyroidlabs/rails_api_keys/blob/v0.3.0/CHANGELOG.md
|
|
62
64
|
bug_tracker_uri: https://github.com/rubyroidlabs/rails_api_keys/issues
|
|
63
|
-
documentation_uri: https://www.rubydoc.info/gems/rails_api_keys/0.
|
|
65
|
+
documentation_uri: https://www.rubydoc.info/gems/rails_api_keys/0.3.0
|
|
64
66
|
rubygems_mfa_required: 'true'
|
|
65
67
|
allowed_push_host: https://rubygems.org
|
|
66
68
|
rdoc_options: []
|
|
@@ -77,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
77
79
|
- !ruby/object:Gem::Version
|
|
78
80
|
version: '0'
|
|
79
81
|
requirements: []
|
|
80
|
-
rubygems_version: 4.0.
|
|
82
|
+
rubygems_version: 4.0.20
|
|
81
83
|
specification_version: 4
|
|
82
84
|
summary: Reusable Rails engine for personal API key authentication.
|
|
83
85
|
test_files: []
|