standard_id 0.60.0 → 0.61.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 +34 -0
- data/README.md +43 -5
- data/app/controllers/concerns/standard_id/social_authentication.rb +5 -4
- data/app/forms/standard_id/web/reset_password_confirm_form.rb +1 -1
- data/app/models/standard_id/password_credential.rb +1 -1
- data/lib/generators/standard_id/install/templates/standard_id_reference.rb +11 -0
- data/lib/standard_id/config/callable_validator.rb +26 -3
- data/lib/standard_id/config/schema.rb +26 -0
- data/lib/standard_id/email_password/password_reset.rb +1 -1
- data/lib/standard_id/error_reporting.rb +38 -0
- data/lib/standard_id/events.rb +47 -0
- data/lib/standard_id/health_contributions.rb +27 -0
- data/lib/standard_id/http_client.rb +39 -6
- data/lib/standard_id/version.rb +1 -1
- data/lib/standard_id.rb +2 -0
- metadata +3 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c5b800c9bdd221e81804f561bee356990391ca705f62412542deb424d00fd0e9
|
|
4
|
+
data.tar.gz: deff24e781f7147bca4d0096309f1bcbc37aa6d46295e6bdeff0d47d40805dca
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c4fb45b06cfb55d73e765443943a18a8237f71fdb1093e152870f041c922d4e994bd9d3ff5c4015ada599a3552da9aca829bd7c5ca0fb5bc608e710a92615d7b
|
|
7
|
+
data.tar.gz: 779383c04ce9b1d3548399fae80c08f77ed3f470d4bd9efe2b248cff0455aeb9812353b46a74f859e5e674d7e7264bd36f40b86f340696226b0979f0f1194f98
|
data/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,40 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.61.0] - 2026-11-23
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- **standard_health contributions.** StandardId registers an `ActiveSupport.on_load(:standard_health)`
|
|
15
|
+
hook that offers `StandardId::Checks::Migrations` as the non-critical `standard_id_migrations`
|
|
16
|
+
default check and declares two env vars under `standard_id`: `APP_ENVIRONMENT` (recommended;
|
|
17
|
+
it decides StandardId's production-only guards) and `STANDARD_ID_BYPASS_CODE` (forbidden in
|
|
18
|
+
production). There is no dependency on standard_health: without a standard_health that runs the
|
|
19
|
+
hook, nothing happens. Apps that already `register_check :standard_id_migrations` by hand are
|
|
20
|
+
unaffected (`register_default_checks` skips a registered name); opt out with
|
|
21
|
+
`c.register_default_checks standard_id_migrations: false`.
|
|
22
|
+
- **`config.network_wrapper`**, a callable `->(provider:, &block)` that wraps every outbound
|
|
23
|
+
connection `StandardId::HttpClient` makes (social token, userinfo and JWKS calls), e.g. in a
|
|
24
|
+
circuit breaker: `->(provider:, &b) { StandardCircuit.run(:"oauth_#{provider}", &b) }`. `nil`
|
|
25
|
+
(default) runs the request directly. `HttpClient.post_form`, `.get_with_bearer` and the private
|
|
26
|
+
`start_connection` that plugins call for a JWKS take an optional `provider:` keyword (default
|
|
27
|
+
`:standard_id`), so existing callers keep working, including those that pass
|
|
28
|
+
`post_form`'s form as bare keywords (`post_form(url, id_token: token)`). The boot-time callable check rejects a
|
|
29
|
+
wrapper with an unknown keyword, a positional argument, or (for a lambda) no `&block`.
|
|
30
|
+
- **`StandardId::Events.auditable`**, the frozen list of event patterns worth an audit row, for
|
|
31
|
+
`StandardId::Events.auditable.each { |pattern| config.subscribe_to(pattern) }` in standard_audit
|
|
32
|
+
(one pattern per call, which every standard_audit version accepts). It keeps what the apps
|
|
33
|
+
subscribe to today (authentication, account, social and passwordless events; session created,
|
|
34
|
+
revoked and expired) and adds the password and client-secret lifecycle, refresh-token reuse,
|
|
35
|
+
token revocation, audience mismatches and OAuth client registration changes.
|
|
36
|
+
- **`config.error_reporter`**, an optional callable `(error, context)` for the errors StandardId
|
|
37
|
+
handles and carries on from (an event subscriber that raised after its change committed).
|
|
38
|
+
`context` carries `source: "standard_id"`, `handled: true` and what failed (`event:` or
|
|
39
|
+
`action:`). `nil` (default) keeps `Rails.error.report(error, handled: true, source:
|
|
40
|
+
"standard_id")`. All four report sites go through the new `StandardId::ErrorReporting.report`.
|
|
41
|
+
A reporter that raises is logged and the error falls back to that default report, so the flow
|
|
42
|
+
it was reporting from still carries on.
|
|
43
|
+
|
|
10
44
|
## [0.60.0] - 2026-11-22
|
|
11
45
|
|
|
12
46
|
### Security
|
data/README.md
CHANGED
|
@@ -627,6 +627,23 @@ gets `provider_tokens:` in the `social.before_account` / `before_mint` hooks.
|
|
|
627
627
|
Setting `config.events.publish_provider_tokens = true` restores `event[:tokens]`;
|
|
628
628
|
the subscriber that reads it then has to keep them out of audit rows and logs.
|
|
629
629
|
|
|
630
|
+
**Wrapping provider calls.** Every outbound request StandardId makes for a
|
|
631
|
+
provider (token exchange, userinfo, JWKS) goes through `StandardId::HttpClient`,
|
|
632
|
+
and `config.network_wrapper` wraps each one, given the provider's name. Use it
|
|
633
|
+
for a circuit breaker, so a provider outage fails fast instead of holding
|
|
634
|
+
request threads for the full timeout, or for a tracing span:
|
|
635
|
+
|
|
636
|
+
```ruby
|
|
637
|
+
c.network_wrapper = ->(provider:, &b) { StandardCircuit.run(:"oauth_#{provider}", &b) }
|
|
638
|
+
```
|
|
639
|
+
|
|
640
|
+
It must call the block and return its result. A timeout or refused connection
|
|
641
|
+
raises inside the wrapper, and whatever the wrapper raises (an open circuit,
|
|
642
|
+
say) reaches the provider's caller as is. `provider` is the plugin's name
|
|
643
|
+
(`:google`, `:apple`, ...), or `:standard_id` for a plugin release that does not
|
|
644
|
+
pass one yet. A lambda must take the block as `&block`; the boot check rejects
|
|
645
|
+
one that doesn't, since it would silently skip the request.
|
|
646
|
+
|
|
630
647
|
### Inertia.js Integration
|
|
631
648
|
|
|
632
649
|
StandardId supports [Inertia.js](https://inertiajs.com/) for modern React, Vue, or Svelte frontends. When enabled, web controllers render Inertia components instead of ERB views.
|
|
@@ -1072,6 +1089,21 @@ StandardId::Events.subscribe(/social/) do |event|
|
|
|
1072
1089
|
end
|
|
1073
1090
|
```
|
|
1074
1091
|
|
|
1092
|
+
#### When a subscriber raises after the change has committed
|
|
1093
|
+
|
|
1094
|
+
A subscriber to an event that reports something already written (a completed
|
|
1095
|
+
password reset, a password rehash, a social link) cannot undo it, so StandardId
|
|
1096
|
+
logs its error and reports it instead of failing the request. Reports go to
|
|
1097
|
+
`Rails.error.report(error, handled: true, source: "standard_id")` unless you set
|
|
1098
|
+
`config.error_reporter`, a callable taking `(error, context)`; `context` carries
|
|
1099
|
+
`source:` and `handled:` plus what failed (`event:` or `action:`). If your
|
|
1100
|
+
reporter raises, StandardId logs that and falls back to the `Rails.error.report`
|
|
1101
|
+
call above, so the request still completes:
|
|
1102
|
+
|
|
1103
|
+
```ruby
|
|
1104
|
+
c.error_reporter = ->(error, context) { Sentry.capture_exception(error, extra: context) }
|
|
1105
|
+
```
|
|
1106
|
+
|
|
1075
1107
|
### Audit Logging
|
|
1076
1108
|
|
|
1077
1109
|
For production audit trails, use the [standard_audit](https://github.com/rarebit-one/standard_audit) gem. StandardId and StandardAudit have zero direct references to each other — the host application wires them together.
|
|
@@ -1094,12 +1126,12 @@ rails db:migrate
|
|
|
1094
1126
|
|
|
1095
1127
|
#### Wiring StandardId events to StandardAudit
|
|
1096
1128
|
|
|
1097
|
-
Configure StandardAudit to subscribe to StandardId
|
|
1129
|
+
Configure StandardAudit to subscribe to the events StandardId marks as worth auditing, and map its payload conventions:
|
|
1098
1130
|
|
|
1099
1131
|
```ruby
|
|
1100
1132
|
# config/initializers/standard_audit.rb
|
|
1101
1133
|
StandardAudit.configure do |config|
|
|
1102
|
-
config.subscribe_to
|
|
1134
|
+
StandardId::Events.auditable.each { |pattern| config.subscribe_to(pattern) }
|
|
1103
1135
|
|
|
1104
1136
|
# StandardId uses :account and :current_account rather than :actor/:target.
|
|
1105
1137
|
# Map them so StandardAudit extracts the right records.
|
|
@@ -1118,7 +1150,9 @@ StandardAudit.configure do |config|
|
|
|
1118
1150
|
end
|
|
1119
1151
|
```
|
|
1120
1152
|
|
|
1121
|
-
That's it. Every StandardId
|
|
1153
|
+
That's it. Every security-relevant StandardId event will now be persisted as an audit log entry. No changes are needed inside StandardId itself.
|
|
1154
|
+
|
|
1155
|
+
`StandardId::Events.auditable` is a frozen list of Regexps and full event names: authentication, account, social and passwordless events; session created, revoked and expired; password and client-secret lifecycle; refresh-token reuse, token revocation, audience mismatches and OAuth client registration changes. It leaves out per-request noise (`session.validating`/`validated`) and high-volume token minting (`oauth.token.issued`/`refreshed`). New releases may add to it, so an app that iterates it picks those up. To audit everything instead, subscribe to `/\Astandard_id\./`.
|
|
1122
1156
|
|
|
1123
1157
|
#### Querying audit logs
|
|
1124
1158
|
|
|
@@ -1998,7 +2032,7 @@ module StandardId
|
|
|
1998
2032
|
rescue_to_oauth_error do
|
|
1999
2033
|
raise StandardId::InvalidRequestError, "Missing authorization code" if code.blank?
|
|
2000
2034
|
|
|
2001
|
-
response = HttpClient.post_form(TOKEN_ENDPOINT, { code:, redirect_uri:, ... })
|
|
2035
|
+
response = HttpClient.post_form(TOKEN_ENDPOINT, { code:, redirect_uri:, ... }, provider: :github)
|
|
2002
2036
|
parsed = JSON.parse(response.body)
|
|
2003
2037
|
build_response(fetch_profile(parsed["access_token"]), tokens: extract_tokens(parsed))
|
|
2004
2038
|
end
|
|
@@ -2009,6 +2043,8 @@ module StandardId
|
|
|
2009
2043
|
end
|
|
2010
2044
|
```
|
|
2011
2045
|
|
|
2046
|
+
Pass `provider:` to every `HttpClient` call so the host's `config.network_wrapper` can tell providers apart (see [Social Login Setup](#social-login-setup)). The keyword is new in the release after 0.60.0; a plugin that supports older standard_id releases should check for it, e.g. `HttpClient.method(:post_form).parameters.include?([:key, :provider])`, and leave it out when absent.
|
|
2047
|
+
|
|
2012
2048
|
**Required interface:** `provider_name`, `authorization_url`, `get_user_info`.
|
|
2013
2049
|
|
|
2014
2050
|
**Optional hooks** (all class methods, with safe defaults):
|
|
@@ -2283,12 +2319,14 @@ address with a PostgreSQL advisory lock, so two concurrent starts cannot both is
|
|
|
2283
2319
|
|
|
2284
2320
|
- **At boot** (file-system only, no database): `config.missing_migrations` — `:warn` (default in development/test: logs and prints to stderr), `:raise` (fails boot with `StandardId::ConfigurationError`), or `:ignore` (default in every other environment — it never raises in production unless you opt in).
|
|
2285
2321
|
- **`StandardId::MigrationCheck.pending(check_database: true)`** returns every gem migration that is `:not_installed` or installed but `:not_run` (one `schema_migrations` read).
|
|
2286
|
-
- **Health check** — `StandardId::Checks::Migrations` is [standard_health](https://github.com/rarebit-one/standard_health)-compatible (duck-typed, no dependency).
|
|
2322
|
+
- **Health check** — `StandardId::Checks::Migrations` is [standard_health](https://github.com/rarebit-one/standard_health)-compatible (duck-typed, no dependency). With a standard_health that supports contributions, StandardId offers it through standard_health's load hook, so `c.register_default_checks` registers it, non-critical: a missing migration degrades `/health/ready` without failing it. Opt out with `c.register_default_checks standard_id_migrations: false`. With an older standard_health, register it yourself:
|
|
2287
2323
|
|
|
2288
2324
|
```ruby
|
|
2289
2325
|
c.register_check :standard_id_migrations, StandardId::Checks::Migrations, critical: false
|
|
2290
2326
|
```
|
|
2291
2327
|
|
|
2328
|
+
The same hook declares the env vars StandardId cares about for `/diagnostics/env`: `APP_ENVIRONMENT` (recommended; it decides StandardId's production-only guards) and `STANDARD_ID_BYPASS_CODE` (forbidden in production). An entry your app declares in its own `env_spec` wins. StandardId does not depend on standard_health; without it the hook never runs.
|
|
2329
|
+
|
|
2292
2330
|
Two cases are built in and documented in `StandardId::MigrationCheck`:
|
|
2293
2331
|
|
|
2294
2332
|
- **Superseded** (`SUPERSEDED_BY`): `20260414200000_add_target_created_at_index_to_code_challenges` counts as satisfied when `20260416180511` (whose partial, concurrently built index replaces it) is installed.
|
|
@@ -376,14 +376,15 @@ module StandardId
|
|
|
376
376
|
|
|
377
377
|
# SOCIAL_ACCOUNT_LINKED reports a link that has already committed, so it
|
|
378
378
|
# cannot refuse the login: a subscriber that raises here would fail the
|
|
379
|
-
# callback while the link stays. Its error is logged and reported
|
|
380
|
-
# Rails.error instead of
|
|
381
|
-
# run before anything is
|
|
379
|
+
# callback while the link stays. Its error is logged and reported
|
|
380
|
+
# (StandardId::ErrorReporting; Rails.error by default) instead of
|
|
381
|
+
# propagating. (Guards belong on the events that run before anything is
|
|
382
|
+
# written.)
|
|
382
383
|
def emit_committed_social_link(identifier, provider)
|
|
383
384
|
emit_social_account_linked(identifier.account, provider, identifier)
|
|
384
385
|
rescue StandardError => e
|
|
385
386
|
Rails.logger&.error("[StandardId] SOCIAL_ACCOUNT_LINKED subscriber raised after the link committed: #{e.class}: #{e.message}")
|
|
386
|
-
|
|
387
|
+
StandardId::ErrorReporting.report(e, event: StandardId::Events.namespaced_event_name(StandardId::Events::SOCIAL_ACCOUNT_LINKED))
|
|
387
388
|
end
|
|
388
389
|
|
|
389
390
|
# A social login that recorded a link (and maybe created an account) and
|
|
@@ -56,7 +56,7 @@ module StandardId
|
|
|
56
56
|
account: account
|
|
57
57
|
)
|
|
58
58
|
rescue StandardError => e
|
|
59
|
-
|
|
59
|
+
StandardId::ErrorReporting.report(e, event: StandardId::Events.namespaced_event_name(StandardId::Events::CREDENTIAL_PASSWORD_RESET_COMPLETED))
|
|
60
60
|
logger = StandardId.logger
|
|
61
61
|
return unless logger.respond_to?(:error)
|
|
62
62
|
|
|
@@ -121,7 +121,7 @@ module StandardId
|
|
|
121
121
|
end
|
|
122
122
|
|
|
123
123
|
def report_failure(action, error)
|
|
124
|
-
|
|
124
|
+
StandardId::ErrorReporting.report(error, action: action)
|
|
125
125
|
logger = StandardId.logger
|
|
126
126
|
return unless logger.respond_to?(:error)
|
|
127
127
|
|
|
@@ -597,6 +597,17 @@ StandardId.configure do |c|
|
|
|
597
597
|
# c.sentry_context = ->(account, _session) {
|
|
598
598
|
# { segment: account.primary_profile&.type }
|
|
599
599
|
# }
|
|
600
|
+
|
|
601
|
+
# Wraps every outbound provider request (token exchange, userinfo, JWKS),
|
|
602
|
+
# e.g. in a circuit breaker. Receives provider: (:google, :apple, ...) and
|
|
603
|
+
# the request as a block; must call the block and return its result.
|
|
604
|
+
# Default: nil (the request runs directly)
|
|
605
|
+
# c.network_wrapper = ->(provider:, &b) { StandardCircuit.run(:"oauth_#{provider}", &b) }
|
|
606
|
+
|
|
607
|
+
# Receives errors StandardId handles and carries on from (a subscriber that
|
|
608
|
+
# raised after its change committed). Receives (error, context).
|
|
609
|
+
# Default: nil (Rails.error.report(error, handled: true, source: "standard_id"))
|
|
610
|
+
# c.error_reporter = ->(error, context) { Sentry.capture_exception(error, extra: context) }
|
|
600
611
|
end
|
|
601
612
|
|
|
602
613
|
# Notes:
|
|
@@ -27,6 +27,13 @@ module StandardId
|
|
|
27
27
|
CALLABLE_FIELDS = {
|
|
28
28
|
"profile_resolver" => { signature: "(account, profile_type)", arity: 2, kind: :positional },
|
|
29
29
|
"sentry_context" => { signature: "(account, session)", arity: 2, kind: :positional },
|
|
30
|
+
"error_reporter" => { signature: "(error, context)", arity: 2, kind: :positional },
|
|
31
|
+
"network_wrapper" => {
|
|
32
|
+
signature: "(provider:, &block)",
|
|
33
|
+
kind: :keyword,
|
|
34
|
+
keywords: %i[provider],
|
|
35
|
+
block: true
|
|
36
|
+
},
|
|
30
37
|
"after_account_created" => { signature: "(account, request, context)", arity: 3, kind: :positional },
|
|
31
38
|
"before_sign_in" => { signature: "(account, request, context)", arity: 3, kind: :positional },
|
|
32
39
|
"after_sign_in" => { signature: "(account, request, context)", arity: 3, kind: :positional },
|
|
@@ -142,6 +149,21 @@ module StandardId
|
|
|
142
149
|
when :positional then validate_positional!(path, callable, spec)
|
|
143
150
|
when :keyword then validate_keyword!(path, callable, spec[:keywords], spec[:signature])
|
|
144
151
|
end
|
|
152
|
+
validate_block_param!(path, callable, spec[:signature]) if spec[:block]
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# A wrapper callable (network_wrapper) runs the work it is handed as a
|
|
156
|
+
# block. A Proc can only receive that block through an explicit `&`
|
|
157
|
+
# parameter; without one the block is dropped and the work never runs.
|
|
158
|
+
# (A method or `#call` object can `yield` without declaring it, so only
|
|
159
|
+
# Procs are checked.)
|
|
160
|
+
def validate_block_param!(path, callable, signature)
|
|
161
|
+
return unless callable.is_a?(Proc)
|
|
162
|
+
return if callable.parameters.any? { |type, _| type == :block }
|
|
163
|
+
|
|
164
|
+
raise StandardId::ConfigurationError,
|
|
165
|
+
"StandardId config: `#{path}` must take the wrapped work as a block " \
|
|
166
|
+
"(e.g. `->(provider:, &block) { block.call }`). Expected signature #{signature}."
|
|
145
167
|
end
|
|
146
168
|
|
|
147
169
|
def validate_positional!(path, callable, spec)
|
|
@@ -205,10 +227,11 @@ module StandardId
|
|
|
205
227
|
|
|
206
228
|
# EmailPassword.call_hook passes only the keywords a hook declares, so a
|
|
207
229
|
# lambda with a required positional argument (`->(request) { }`) would
|
|
208
|
-
# raise on its first call. Limited to the email_password hooks
|
|
209
|
-
# are new: older keyword hooks keep accepting
|
|
230
|
+
# raise on its first call. Limited to the email_password hooks and
|
|
231
|
+
# network_wrapper, which are new: older keyword hooks keep accepting
|
|
232
|
+
# what they always did.
|
|
210
233
|
def validate_no_required_positional!(path, callable, params, signature)
|
|
211
|
-
return unless path.start_with?("email_password.") && lambda_callable?(callable)
|
|
234
|
+
return unless (path.start_with?("email_password.") || path == "network_wrapper") && lambda_callable?(callable)
|
|
212
235
|
return unless params.any? { |type, _| type == :req }
|
|
213
236
|
|
|
214
237
|
raise StandardId::ConfigurationError,
|
|
@@ -162,6 +162,32 @@ StandardId::ConfigSchema.define do
|
|
|
162
162
|
# Raise StandardId::LoginMethodDenied.new("message") to refuse with your own.
|
|
163
163
|
# nil (the default) allows every method, i.e. no behaviour change.
|
|
164
164
|
field :login_method_policy, type: :any, default: nil
|
|
165
|
+
|
|
166
|
+
# network_wrapper: wraps every outbound HTTP connection StandardId::HttpClient
|
|
167
|
+
# makes (social-provider token, userinfo and JWKS calls), e.g. in a circuit
|
|
168
|
+
# breaker or a tracing span.
|
|
169
|
+
# Receives: provider: (Symbol — :google, :apple, ...; :standard_id when the
|
|
170
|
+
# caller names none) and a block that performs the request.
|
|
171
|
+
# Must call the block and return its result. Whatever the block raises
|
|
172
|
+
# (timeouts, refused connections) raises inside the wrapper; whatever the
|
|
173
|
+
# wrapper raises reaches the caller unchanged.
|
|
174
|
+
# nil (the default) runs the block directly.
|
|
175
|
+
#
|
|
176
|
+
# c.network_wrapper = ->(provider:, &b) { StandardCircuit.run(:"oauth_#{provider}", &b) }
|
|
177
|
+
field :network_wrapper, type: :any, default: nil
|
|
178
|
+
|
|
179
|
+
# error_reporter: receives errors StandardId handles and carries on from —
|
|
180
|
+
# today, an event subscriber that raised after the change it reports had
|
|
181
|
+
# already committed (a completed password reset, a password rehash, a
|
|
182
|
+
# social link).
|
|
183
|
+
# Receives: (error, context), where context is a Hash with at least
|
|
184
|
+
# source: "standard_id" and handled: true, and may also name
|
|
185
|
+
# what failed (event:, action:).
|
|
186
|
+
# nil (the default) reports to Rails.error, as StandardId always has:
|
|
187
|
+
# `Rails.error.report(error, handled: true, source: "standard_id")`.
|
|
188
|
+
#
|
|
189
|
+
# c.error_reporter = ->(error, context) { Sentry.capture_exception(error, extra: context) }
|
|
190
|
+
field :error_reporter, type: :any, default: nil
|
|
165
191
|
end
|
|
166
192
|
|
|
167
193
|
scope :events do
|
|
@@ -146,7 +146,7 @@ module StandardId
|
|
|
146
146
|
account: account
|
|
147
147
|
)
|
|
148
148
|
rescue StandardError => e
|
|
149
|
-
|
|
149
|
+
StandardId::ErrorReporting.report(e, event: StandardId::Events.namespaced_event_name(StandardId::Events::CREDENTIAL_PASSWORD_RESET_COMPLETED))
|
|
150
150
|
logger = StandardId.logger
|
|
151
151
|
return unless logger.respond_to?(:error)
|
|
152
152
|
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
module StandardId
|
|
2
|
+
# Reports an error StandardId has handled and is carrying on from (see
|
|
3
|
+
# `config.error_reporter`). Every such report goes through here, so a host
|
|
4
|
+
# that routes them elsewhere catches all of them.
|
|
5
|
+
module ErrorReporting
|
|
6
|
+
SOURCE = "standard_id".freeze
|
|
7
|
+
|
|
8
|
+
module_function
|
|
9
|
+
|
|
10
|
+
# Without a configured reporter this is exactly the call the engine has
|
|
11
|
+
# always made, `Rails.error.report(error, handled: true, source:
|
|
12
|
+
# "standard_id")`; `context` goes only to a configured reporter.
|
|
13
|
+
#
|
|
14
|
+
# Every caller is a rescue that carries on, so a configured reporter that
|
|
15
|
+
# raises is logged and the error goes to the default report instead.
|
|
16
|
+
#
|
|
17
|
+
# @param error [Exception]
|
|
18
|
+
# @param context [Hash] extra detail for a configured reporter, e.g. the
|
|
19
|
+
# event whose subscriber raised
|
|
20
|
+
# @return [void]
|
|
21
|
+
def report(error, context = {})
|
|
22
|
+
reporter = StandardId.config.error_reporter
|
|
23
|
+
return report_to_rails(error) unless reporter.respond_to?(:call)
|
|
24
|
+
|
|
25
|
+
begin
|
|
26
|
+
reporter.call(error, context.merge(source: SOURCE, handled: true))
|
|
27
|
+
rescue StandardError => e
|
|
28
|
+
Rails.logger&.error("[StandardId] error_reporter raised #{e.class}: #{e.message}; reporting #{error.class} to Rails.error instead")
|
|
29
|
+
report_to_rails(error)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def report_to_rails(error)
|
|
34
|
+
Rails.error.report(error, handled: true, source: SOURCE)
|
|
35
|
+
end
|
|
36
|
+
private_class_method :report_to_rails
|
|
37
|
+
end
|
|
38
|
+
end
|
data/lib/standard_id/events.rb
CHANGED
|
@@ -6,7 +6,54 @@ module StandardId
|
|
|
6
6
|
# Event namespace prefix for all StandardId events
|
|
7
7
|
NAMESPACE = "standard_id"
|
|
8
8
|
|
|
9
|
+
# Event names worth an audit row, as Regexps and full Strings that both
|
|
10
|
+
# ActiveSupport::Notifications and standard_audit's `subscribe_to` accept.
|
|
11
|
+
# See `.auditable`.
|
|
12
|
+
AUDITABLE = [
|
|
13
|
+
# What the apps subscribed to before this list existed.
|
|
14
|
+
/\Astandard_id\.authentication\./,
|
|
15
|
+
"#{NAMESPACE}.#{SESSION_CREATED}",
|
|
16
|
+
"#{NAMESPACE}.#{SESSION_REVOKED}",
|
|
17
|
+
"#{NAMESPACE}.#{SESSION_EXPIRED}",
|
|
18
|
+
/\Astandard_id\.account\./,
|
|
19
|
+
/\Astandard_id\.social\./,
|
|
20
|
+
/\Astandard_id\.passwordless\./,
|
|
21
|
+
# Changes to how an account or client authenticates. (Not
|
|
22
|
+
# CREDENTIAL_PASSWORD_REHASHED: the password itself is unchanged.)
|
|
23
|
+
"#{NAMESPACE}.#{CREDENTIAL_PASSWORD_CREATED}",
|
|
24
|
+
"#{NAMESPACE}.#{CREDENTIAL_PASSWORD_RESET_INITIATED}",
|
|
25
|
+
"#{NAMESPACE}.#{CREDENTIAL_PASSWORD_RESET_COMPLETED}",
|
|
26
|
+
"#{NAMESPACE}.#{CREDENTIAL_PASSWORD_CHANGED}",
|
|
27
|
+
"#{NAMESPACE}.#{CREDENTIAL_CLIENT_SECRET_CREATED}",
|
|
28
|
+
"#{NAMESPACE}.#{CREDENTIAL_CLIENT_SECRET_ROTATED}",
|
|
29
|
+
"#{NAMESPACE}.#{CREDENTIAL_CLIENT_SECRET_REVOKED}",
|
|
30
|
+
# OAuth: token theft and revocation, a token sent to the wrong resource,
|
|
31
|
+
# and changes to who may request tokens at all.
|
|
32
|
+
"#{NAMESPACE}.#{OAUTH_REFRESH_TOKEN_REUSE_DETECTED}",
|
|
33
|
+
"#{NAMESPACE}.#{OAUTH_TOKEN_REVOKED}",
|
|
34
|
+
"#{NAMESPACE}.#{OAUTH_AUDIENCE_MISMATCH}",
|
|
35
|
+
"#{NAMESPACE}.#{OAUTH_CLIENT_REGISTERED}",
|
|
36
|
+
"#{NAMESPACE}.#{OAUTH_CLIENT_UPDATED}",
|
|
37
|
+
"#{NAMESPACE}.#{OAUTH_CLIENT_DEREGISTERED}"
|
|
38
|
+
].map(&:freeze).freeze
|
|
39
|
+
|
|
9
40
|
class << self
|
|
41
|
+
# The event names a host should persist to its audit log:
|
|
42
|
+
#
|
|
43
|
+
# StandardAudit.configure do |config|
|
|
44
|
+
# StandardId::Events.auditable.each { |pattern| config.subscribe_to(pattern) }
|
|
45
|
+
# end
|
|
46
|
+
#
|
|
47
|
+
# Deliberately left out: per-request noise (session.validating /
|
|
48
|
+
# validated), high-volume token minting (oauth.token.issued,
|
|
49
|
+
# oauth.token.refreshed), and the benign refresh race
|
|
50
|
+
# (oauth.refresh_token.reuse_graced).
|
|
51
|
+
#
|
|
52
|
+
# @return [Array<Regexp, String>] frozen
|
|
53
|
+
def auditable
|
|
54
|
+
AUDITABLE
|
|
55
|
+
end
|
|
56
|
+
|
|
10
57
|
# Publish an event with the given name and payload
|
|
11
58
|
#
|
|
12
59
|
# @param event_name [String, Symbol] The event name (use constants from Definitions)
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# What StandardId offers standard_health, through its load hook. There is no
|
|
2
|
+
# dependency either way: without standard_health (or with a version older than
|
|
3
|
+
# the hook) the block never runs. With it, the block runs once, whichever gem
|
|
4
|
+
# is required first, and receives standard_health's contribution registry,
|
|
5
|
+
# which exposes only `contribute_check` and `contribute_env_vars`.
|
|
6
|
+
#
|
|
7
|
+
# Apps still decide: the check registers through `register_default_checks`
|
|
8
|
+
# (opt out with `standard_id_migrations: false`), and an env var the app
|
|
9
|
+
# declares in its own env_spec wins over the declaration here.
|
|
10
|
+
ActiveSupport.on_load(:standard_health) do |health|
|
|
11
|
+
# Resolved by name when the check runs, so nothing here loads it early.
|
|
12
|
+
health.contribute_check :standard_id_migrations, "StandardId::Checks::Migrations", critical: false
|
|
13
|
+
|
|
14
|
+
# APP_ENVIRONMENT is the one deploy variable StandardId reads itself.
|
|
15
|
+
# STANDARD_ID_BYPASS_CODE it never reads, but its production guard names it
|
|
16
|
+
# and the apps feed c.passwordless.bypass_code from it. Provider credentials (GOOGLE_CLIENT_ID, APPLE_PRIVATE_KEY, ...) are not
|
|
17
|
+
# declared here: which apply depends on the plugins an app registers.
|
|
18
|
+
health.contribute_env_vars "standard_id" do
|
|
19
|
+
recommended :APP_ENVIRONMENT,
|
|
20
|
+
description: "Decides StandardId's production-only guards (the bypass-code refusal, the empty " \
|
|
21
|
+
"allowed_audiences warning, provider_misconfiguration = :raise). Any value other than staging, " \
|
|
22
|
+
"preview, development or test counts as production; unset falls back to Rails.env"
|
|
23
|
+
forbidden :STANDARD_ID_BYPASS_CODE, in: %w[production],
|
|
24
|
+
description: "Conventional source for c.passwordless.bypass_code, a fixed OTP for e2e tests. StandardId " \
|
|
25
|
+
"refuses to verify any code in production while a bypass code is configured"
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -23,19 +23,33 @@ module StandardId
|
|
|
23
23
|
IPAddr.new("fe80::/10")
|
|
24
24
|
].freeze
|
|
25
25
|
|
|
26
|
+
# Provider name passed to `StandardId.config.network_wrapper` when a caller
|
|
27
|
+
# does not name one.
|
|
28
|
+
DEFAULT_PROVIDER = :standard_id
|
|
29
|
+
|
|
26
30
|
class << self
|
|
27
|
-
|
|
31
|
+
# `provider:` names the remote service (`:google`, `:apple`, ...) for
|
|
32
|
+
# `StandardId.config.network_wrapper`, e.g. to keep one circuit per
|
|
33
|
+
# provider. It has no other effect on the request.
|
|
34
|
+
#
|
|
35
|
+
# The form may also come as bare keywords (`post_form(url, id_token: t)`),
|
|
36
|
+
# which is how callers written before `provider:` existed pass it: once
|
|
37
|
+
# the method takes a keyword, Ruby reads a braceless Hash as keywords, so
|
|
38
|
+
# `**form` keeps those calls working. A form field named `provider` must
|
|
39
|
+
# therefore go in an explicit Hash. With no bare keywords, `params` goes
|
|
40
|
+
# to `set_form_data` as given, so an Array of pairs keeps repeated keys.
|
|
41
|
+
def post_form(endpoint, params = nil, provider: DEFAULT_PROVIDER, **form)
|
|
28
42
|
uri, resolved_ip = validate_url!(endpoint)
|
|
29
43
|
request = Net::HTTP::Post.new(uri)
|
|
30
|
-
request.set_form_data(params)
|
|
31
|
-
start_connection(uri, resolved_ip:) { |http| http.request(request) }
|
|
44
|
+
request.set_form_data(form.empty? ? (params || {}) : (params || {}).to_h.merge(form))
|
|
45
|
+
start_connection(uri, resolved_ip:, provider:) { |http| http.request(request) }
|
|
32
46
|
end
|
|
33
47
|
|
|
34
|
-
def get_with_bearer(endpoint, access_token)
|
|
48
|
+
def get_with_bearer(endpoint, access_token, provider: DEFAULT_PROVIDER)
|
|
35
49
|
uri, resolved_ip = validate_url!(endpoint)
|
|
36
50
|
request = Net::HTTP::Get.new(uri)
|
|
37
51
|
request["Authorization"] = "Bearer #{access_token}"
|
|
38
|
-
start_connection(uri, resolved_ip:) { |http| http.request(request) }
|
|
52
|
+
start_connection(uri, resolved_ip:, provider:) { |http| http.request(request) }
|
|
39
53
|
end
|
|
40
54
|
|
|
41
55
|
private
|
|
@@ -59,7 +73,26 @@ module StandardId
|
|
|
59
73
|
[uri, addresses.first]
|
|
60
74
|
end
|
|
61
75
|
|
|
62
|
-
|
|
76
|
+
# Private, but provider plugins call it through `send` to fetch a JWKS
|
|
77
|
+
# with a plain GET, so its signature is part of the plugin contract:
|
|
78
|
+
# `provider:` is optional and new keywords must stay optional.
|
|
79
|
+
def start_connection(uri, resolved_ip: nil, provider: DEFAULT_PROVIDER, &block)
|
|
80
|
+
with_network_wrapper(provider) { open_connection(uri, resolved_ip:, &block) }
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Runs the block through `StandardId.config.network_wrapper`, or
|
|
84
|
+
# directly when none is configured. The wrapper sees the whole
|
|
85
|
+
# connection (connect, request, response), so a timeout or refused
|
|
86
|
+
# connection raises inside it; whatever it raises or returns is the
|
|
87
|
+
# caller's.
|
|
88
|
+
def with_network_wrapper(provider, &block)
|
|
89
|
+
wrapper = StandardId.config.network_wrapper
|
|
90
|
+
return yield unless wrapper.respond_to?(:call)
|
|
91
|
+
|
|
92
|
+
wrapper.call(**StandardId::Utils::CallableParameterFilter.filter(wrapper, { provider: provider&.to_sym }), &block)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def open_connection(uri, resolved_ip: nil, &block)
|
|
63
96
|
http = Net::HTTP.new(uri.host, uri.port)
|
|
64
97
|
http.use_ssl = (uri.scheme == "https")
|
|
65
98
|
http.verify_mode = OpenSSL::SSL::VERIFY_PEER if http.use_ssl?
|
data/lib/standard_id/version.rb
CHANGED
data/lib/standard_id.rb
CHANGED
|
@@ -14,7 +14,9 @@ require "standard_id/association_strict_loading"
|
|
|
14
14
|
require "standard_id/migration_check"
|
|
15
15
|
require "standard_id/migration_helpers"
|
|
16
16
|
require "standard_id/checks/migrations"
|
|
17
|
+
require "standard_id/health_contributions"
|
|
17
18
|
require "standard_id/errors"
|
|
19
|
+
require "standard_id/error_reporting"
|
|
18
20
|
require "standard_id/keyed_digest"
|
|
19
21
|
require "standard_id/events"
|
|
20
22
|
require "standard_id/events/subscribers/base"
|
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.
|
|
4
|
+
version: 0.61.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Rarebit One
|
|
@@ -308,6 +308,7 @@ files:
|
|
|
308
308
|
- lib/standard_id/email_password/token_flow.rb
|
|
309
309
|
- lib/standard_id/email_password/verification.rb
|
|
310
310
|
- lib/standard_id/engine.rb
|
|
311
|
+
- lib/standard_id/error_reporting.rb
|
|
311
312
|
- lib/standard_id/errors.rb
|
|
312
313
|
- lib/standard_id/events.rb
|
|
313
314
|
- lib/standard_id/events/definitions.rb
|
|
@@ -319,6 +320,7 @@ files:
|
|
|
319
320
|
- lib/standard_id/events/subscribers/password_changed_notice_subscriber.rb
|
|
320
321
|
- lib/standard_id/events/subscribers/password_reset_delivery_subscriber.rb
|
|
321
322
|
- lib/standard_id/events/subscribers/passwordless_delivery_subscriber.rb
|
|
323
|
+
- lib/standard_id/health_contributions.rb
|
|
322
324
|
- lib/standard_id/http_client.rb
|
|
323
325
|
- lib/standard_id/instrumentation.rb
|
|
324
326
|
- lib/standard_id/jwt_service.rb
|