standard_id 0.65.0 → 0.66.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: 1364fd2d53a2dd7ce6d7c811234b94c9ddf8a7ce311de86103afd22cb3f3e625
4
- data.tar.gz: fa95d8bcfae362f247dca2b54f10be29112a1ee9030bd4bbe4a363e3da525885
3
+ metadata.gz: ce9554a4ecea8b55429ff64af874edbb1fd9220d0cfaf866834b4c2acda20482
4
+ data.tar.gz: cfe2fe8aee4b26f46111a27afec5b6c892cfed6602b306ff74c1ab2c1424adc7
5
5
  SHA512:
6
- metadata.gz: 90773578c0e8d4324f50bf934ece396cf213ff2cd3ffa0a2b1d0c50d592120637ef2c19719989d8aa9f3881a9315f55d740ef488736291ce581102f77987aa18
7
- data.tar.gz: e78a6d7b8d4ad10b5298bc750fceb5a3d30bc4fba4d587fc43f5eebb6abca9a7f760d2024c0ce642d2e5372f214dfe3b86b50292f91434d39126d7181c492cc0
6
+ metadata.gz: 6e70a3bf0dc94040dbe10b1cbe90480de7b90e549f5b86311fe29e9b81798193a03745c1d2789bcd923eef74a549ba483f0ecca676aebc8f070644f60008d5b6
7
+ data.tar.gz: 886cfc5f1f805f1716142eb945b0f0fcbb13c34859f9ce882076ea31c014bfeb65bcdcfb636103192f89572cf95d7ea7ec7c82bf6ab289da5b01c1d0b00dd2e4
data/CHANGELOG.md CHANGED
@@ -7,6 +7,30 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.66.0] - 2026-11-25
11
+
12
+ ### Added
13
+
14
+ - **Per-endpoint accepted audiences** (`config.oauth.endpoint_audiences`). The gem's own bearer
15
+ endpoints, `userinfo` (`GET /userinfo`), `sessions` (`GET /sessions`, `DELETE /sessions/:id`)
16
+ and `step_up` (`POST /step_up`, `POST /step_up/verify`), can each be limited to a subset of
17
+ the accepted audiences, e.g. `{ userinfo: %w[companion_kit admin_kit], ... }`, so a
18
+ per-service or MCP token the API accepts elsewhere cannot read the account, list or revoke its
19
+ sessions, or step up. A token with none of the endpoint's audiences gets the
20
+ `StandardId::AudienceVerification` response, `403 insufficient_scope`, and the refusal publishes
21
+ `oauth.audience.mismatch` (already auditable) with `token_audiences`, `required_audiences` and
22
+ `endpoint`. An array `aud` passes when any entry matches, and the matched audience's
23
+ `audience_profile_types` binding applies.
24
+ Opt-in: an endpoint left out (or given `[]`) accepts any accepted audience, as before. An
25
+ unknown key raises `StandardId::ConfigurationError` at boot. In production with more than one
26
+ accepted audience, boot logs a warning naming the endpoints left open.
27
+ `StandardId::Oauth::Audiences.endpoint(name)` returns an endpoint's list.
28
+
29
+ ### Changed
30
+
31
+ - `GET /userinfo` verifies the access token in a `before_action` rather than inside the action.
32
+ Responses are unchanged.
33
+
10
34
  ## [0.65.0] - 2026-11-25
11
35
 
12
36
  ### Added
data/README.md CHANGED
@@ -464,6 +464,38 @@ so neither list applies to browser sessions. `JwtService.decode` and
464
464
  `StandardId::Oauth::Audiences.mintable` / `.accepted` return the effective
465
465
  lists.
466
466
 
467
+ #### Per-endpoint audiences
468
+
469
+ The accepted list is the whole API's. The gem's own bearer endpoints, which
470
+ return or change the account's data, can be narrowed further with
471
+ `config.oauth.endpoint_audiences`, so a per-service or MCP token your API must
472
+ accept elsewhere cannot read the account or end its sessions:
473
+
474
+ ```ruby
475
+ StandardId.configure do |config|
476
+ config.oauth.accepted_audiences = %w[companion_kit admin_kit mcp]
477
+
478
+ config.oauth.endpoint_audiences = {
479
+ userinfo: %w[companion_kit admin_kit], # GET /userinfo
480
+ sessions: %w[companion_kit admin_kit], # GET /sessions, DELETE /sessions/:id
481
+ step_up: %w[companion_kit admin_kit] # POST /step_up, POST /step_up/verify
482
+ }
483
+ end
484
+ ```
485
+
486
+ A token whose `aud` holds none of the endpoint's audiences gets
487
+ `403 insufficient_scope`, the response `StandardId::AudienceVerification`
488
+ gives everywhere, and publishes `oauth.audience.mismatch` (auditable) with
489
+ `token_audiences`, `required_audiences` and `endpoint`; an array `aud` passes
490
+ when any entry matches. A token outside
491
+ the accepted list is still a `401 invalid_token` at decode time. The matched
492
+ audience's `audience_profile_types` binding is enforced as well, as on any
493
+ `AudienceVerification` controller. An endpoint left out (or given `[]`) accepts
494
+ any accepted audience, as before, so nothing changes until you set it; an
495
+ unknown key raises `StandardId::ConfigurationError` at boot. In production, with
496
+ more than one accepted audience, boot logs a warning naming the endpoints left
497
+ open (`step_up` only while step-up is enabled).
498
+
467
499
  ### Custom Token Claims
468
500
 
469
501
  You can add additional JWT claims for any token issued through the OAuth token endpoint by mapping scopes to claim names and providing callbacks to resolve each claim. Scopes listed in `oauth.scope_claims` are evaluated against the requested token scopes; when a scope matches, every claim listed for that scope is resolved via the callable defined in `oauth.claim_resolvers`.
@@ -34,6 +34,20 @@ module StandardId
34
34
  # A host hook refused the request with its own response.
35
35
  rescue_from StandardId::RequestRefused, with: :handle_request_refused
36
36
 
37
+ # Narrows the bearer audiences this endpoint accepts to
38
+ # `c.oauth.endpoint_audiences[endpoint]` when the host set it
39
+ # (StandardId::Oauth::Audiences.endpoint): a token with none of them is
40
+ # refused by StandardId::AudienceVerification, 403 insufficient_scope.
41
+ # Unset, any accepted audience passes, as before. Declare it after
42
+ # `before_action :verify_access_token!`, which the check runs behind.
43
+ def self.endpoint_audiences(endpoint)
44
+ include StandardId::AudienceVerification
45
+
46
+ verify_audience -> { StandardId::Oauth::Audiences.endpoint(endpoint) }
47
+ skip_before_action :verify_audience!, if: -> { StandardId::Oauth::Audiences.endpoint(endpoint).empty? }
48
+ rescue_from StandardId::InvalidAudienceError, with: ->(error) { reject_endpoint_audience(error, endpoint) }
49
+ end
50
+
37
51
  protected
38
52
 
39
53
  def validate_content_type!
@@ -93,6 +107,32 @@ module StandardId
93
107
  }, status: error.http_status
94
108
  end
95
109
 
110
+ # AudienceVerification publishes oauth.audience.mismatch only when a
111
+ # profile binding fails; a token refused here for its audience alone is
112
+ # recorded too, then answered as AudienceVerification answers it. A
113
+ # failing subscriber is logged, as there, and does not change the 403.
114
+ def reject_endpoint_audience(error, endpoint)
115
+ publish_endpoint_audience_mismatch(error, endpoint)
116
+ handle_invalid_audience(error)
117
+ end
118
+
119
+ def publish_endpoint_audience_mismatch(error, endpoint)
120
+ StandardId::Events.publish(
121
+ StandardId::Events::OAUTH_AUDIENCE_MISMATCH,
122
+ audience: nil,
123
+ token_audiences: error.actual,
124
+ required_audiences: error.required,
125
+ expected_profile_types: [],
126
+ actual_profile_type: nil,
127
+ endpoint: endpoint.to_s,
128
+ account: current_account
129
+ )
130
+ rescue StandardError => e
131
+ StandardId.config.logger&.warn(
132
+ "[StandardId] failed to emit oauth.audience.mismatch event: #{e.class}: #{e.message}"
133
+ )
134
+ end
135
+
96
136
  def handle_request_refused(error)
97
137
  error.headers.each { |name, value| response.set_header(name.to_s, value.to_s) }
98
138
  render json: error.body, status: error.status
@@ -5,6 +5,7 @@ module StandardId
5
5
 
6
6
  skip_before_action :validate_content_type!
7
7
  before_action :verify_access_token!
8
+ endpoint_audiences :sessions
8
9
 
9
10
  def index
10
11
  sessions = current_account.sessions.active.order(created_at: :desc)
@@ -16,7 +16,8 @@ module StandardId
16
16
  # token is issued: the client keeps its own, and tokens refreshed from it
17
17
  # carry the original sign-in's auth_time. For an audience bound to a
18
18
  # profile type, set c.step_up.token_profile_resolver when an account may
19
- # hold several such profiles. Routed only while c.step_up.enabled is true.
19
+ # hold several such profiles. c.oauth.endpoint_audiences[:step_up] narrows
20
+ # which tokens may step up. Routed only while c.step_up.enabled is true.
20
21
  class StepUpController < BaseController
21
22
  authenticated_controller
22
23
 
@@ -27,6 +28,7 @@ module StandardId
27
28
  store: StandardId::RateLimitHandling::RATE_LIMIT_STORE
28
29
 
29
30
  before_action :verify_access_token!
31
+ endpoint_audiences :step_up
30
32
  # A token whose account config.account_scope no longer finds is no
31
33
  # longer anyone's to step up.
32
34
  before_action -> { render_bearer_unauthorized! if current_account.nil? }
@@ -4,9 +4,10 @@ module StandardId
4
4
  authenticated_controller
5
5
 
6
6
  skip_before_action :validate_content_type!
7
+ before_action :verify_access_token!
8
+ endpoint_audiences :userinfo
7
9
 
8
10
  def show
9
- verify_access_token!
10
11
  account = current_account
11
12
  raise StandardId::NotAuthenticatedError unless account
12
13
 
@@ -367,6 +367,16 @@ StandardId.configure do |c|
367
367
  # for other services lists them as mintable but not accepted:
368
368
  # c.oauth.mintable_audiences = %w[web harness inference]
369
369
  # c.oauth.accepted_audiences = %w[web]
370
+ #
371
+ # Narrow the gem's own bearer endpoints (userinfo, sessions, step_up) to the
372
+ # end-user clients' audiences; a token for any other accepted audience gets
373
+ # 403. An endpoint left out accepts any accepted audience.
374
+ # Default: {}
375
+ # c.oauth.endpoint_audiences = {
376
+ # userinfo: %w[web mobile],
377
+ # sessions: %w[web mobile],
378
+ # step_up: %w[web mobile]
379
+ # }
370
380
 
371
381
  # Scope-to-claims mapping — when a scope is granted, every listed claim is
372
382
  # resolved via claim_resolvers below and embedded in the access token.
@@ -660,6 +660,25 @@ StandardId::ConfigSchema.define do
660
660
  # neither list applies to it.
661
661
  field :accepted_audiences, type: :array, default: nil
662
662
 
663
+ # Audiences each of the gem's own bearer endpoints accepts, on top of the
664
+ # accepted list: a token with none of them gets 403 insufficient_scope
665
+ # (StandardId::AudienceVerification). Keys are the endpoints
666
+ # (StandardId::Oauth::Audiences::ENDPOINTS):
667
+ #
668
+ # :userinfo GET /userinfo
669
+ # :sessions GET /sessions, DELETE /sessions/:id
670
+ # :step_up POST /step_up, POST /step_up/verify
671
+ #
672
+ # c.oauth.endpoint_audiences = {
673
+ # userinfo: %w[companion_kit admin_kit],
674
+ # sessions: %w[companion_kit admin_kit],
675
+ # step_up: %w[companion_kit admin_kit]
676
+ # }
677
+ #
678
+ # An endpoint left out (or given an empty list) accepts any accepted
679
+ # audience, as before. An unknown key raises ConfigurationError at boot.
680
+ field :endpoint_audiences, type: :hash, default: -> { {} }
681
+
663
682
  # Audience → profile type binding (first-class audience modeling).
664
683
  #
665
684
  # Maps each configured audience string to the profile type (or types)
@@ -96,10 +96,12 @@ module StandardId
96
96
  StandardId::Config::CallableValidator.validate!
97
97
  StandardId::Config::ScopeClaimsValidator.validate!
98
98
  StandardId::ScopeConfig.validate_all!
99
+ StandardId::Oauth::Audiences.validate_endpoint_audiences!
99
100
 
100
101
  StandardId::Engine.verify_host_cookie_encryption!(app)
101
102
  StandardId::MigrationCheck.verify_at_boot!
102
103
  StandardId::Engine.warn_if_allowed_audiences_empty_in_production!
104
+ StandardId::Engine.warn_if_endpoint_audiences_unset_in_production!
103
105
  end
104
106
 
105
107
  # Defensive check: StandardId's Web::SessionManager stores session tokens
@@ -176,6 +178,28 @@ module StandardId
176
178
  )
177
179
  end
178
180
 
181
+ # Logs a production-only warning naming the gem's bearer endpoints
182
+ # (`userinfo`, `sessions`, `step_up`) that `oauth.endpoint_audiences` leaves
183
+ # open while the API accepts more than one audience: a token for any of
184
+ # them (an MCP or per-service token, say) can then read the account and
185
+ # its sessions. Silent with one accepted audience, where there is nothing
186
+ # to narrow, and with none, which the accepted-audiences warning covers.
187
+ # `step_up` counts only while step-up is enabled (its routes exist).
188
+ def self.warn_if_endpoint_audiences_unset_in_production!
189
+ return unless StandardId::AppEnvironment.production?
190
+ return unless StandardId::Oauth::Audiences.accepted.size > 1
191
+
192
+ open = StandardId::Oauth::Audiences.unrestricted_endpoints
193
+ open -= [:step_up] unless StandardId::StepUp.enabled?
194
+ return if open.empty?
195
+
196
+ Rails.logger.warn(
197
+ "StandardId: config.oauth.endpoint_audiences does not restrict #{open.join(', ')} in production — " \
198
+ "they accept a bearer for any accepted audience (#{StandardId::Oauth::Audiences.accepted.join(', ')}). " \
199
+ "List the audiences of the end-user clients that call them."
200
+ )
201
+ end
202
+
179
203
  # Warn when the issuer configuration is missing or when `verify_issuer`
180
204
  # is explicitly `false` with an issuer set (migration flag left behind).
181
205
  # Extracted to a module method so specs can exercise it without booting
@@ -19,7 +19,14 @@ module StandardId
19
19
  #
20
20
  # An empty effective list means no restriction on that side, as an empty
21
21
  # allowed_audiences always has.
22
+ #
23
+ # On top of the accepted list, `oauth.endpoint_audiences` narrows what each
24
+ # of the gem's own bearer endpoints accepts (see .endpoint).
22
25
  module Audiences
26
+ # The gem's bearer endpoints that return or change the account's data,
27
+ # by their `oauth.endpoint_audiences` key.
28
+ ENDPOINTS = %i[userinfo sessions step_up].freeze
29
+
23
30
  module_function
24
31
 
25
32
  # @return [Array<String>]
@@ -32,6 +39,38 @@ module StandardId
32
39
  resolve(StandardId.config.oauth.accepted_audiences)
33
40
  end
34
41
 
42
+ # The audiences `oauth.endpoint_audiences` lets +endpoint+ accept. Empty
43
+ # when the host set none, which means any accepted audience.
44
+ #
45
+ # @param endpoint [Symbol] one of ENDPOINTS
46
+ # @return [Array<String>]
47
+ def endpoint(endpoint)
48
+ map = StandardId.config.oauth.endpoint_audiences || {}
49
+ list = map.key?(endpoint.to_sym) ? map[endpoint.to_sym] : map[endpoint.to_s]
50
+ Array(list).map(&:to_s).reject(&:blank?)
51
+ end
52
+
53
+ # ENDPOINTS that `oauth.endpoint_audiences` leaves open.
54
+ #
55
+ # @return [Array<Symbol>]
56
+ def unrestricted_endpoints
57
+ ENDPOINTS.select { |name| endpoint(name).empty? }
58
+ end
59
+
60
+ # A mistyped key would leave its endpoint open without a word, so refuse
61
+ # it at boot.
62
+ #
63
+ # @raise [StandardId::ConfigurationError]
64
+ def validate_endpoint_audiences!
65
+ map = StandardId.config.oauth.endpoint_audiences || {}
66
+ unknown = map.keys.map(&:to_s) - ENDPOINTS.map(&:to_s)
67
+ return if unknown.empty?
68
+
69
+ raise StandardId::ConfigurationError,
70
+ "config.oauth.endpoint_audiences has unknown endpoint(s) #{unknown.join(', ')}; " \
71
+ "expected any of #{ENDPOINTS.join(', ')}"
72
+ end
73
+
35
74
  def resolve(override)
36
75
  list = override.nil? ? StandardId.config.oauth.allowed_audiences : override
37
76
  Array(list).map(&:to_s)
@@ -1,3 +1,3 @@
1
1
  module StandardId
2
- VERSION = "0.65.0"
2
+ VERSION = "0.66.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: standard_id
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.65.0
4
+ version: 0.66.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rarebit One