669 Commits
Author SHA1 Message Date
Trong Huu Nguyen a8399a8f8f test: merge split test files into their package test files
The redirect and provider fetch tests lived in files of their own for no
reason other than how they were added.
2026-08-10 12:36:09 +02:00
Trong Huu Nguyen c399a781b6 fix(openid/provider): remove every alg=none key from the JWKS
RemoveKey shifts later keys into the removed index, so a forward loop skipped
the key after each removal. Two adjacent alg=none keys left the second one in
the set handed to signature verification.
2026-08-10 12:35:25 +02:00
Trong Huu Nguyen 4d67da8e61 test(crypto): generate JWK fixtures for any signature algorithm
Client assertion signing is algorithm-agnostic, but the fixtures only
produced RS256 keys, so nothing proved it. Covers RSA, ECDSA and Ed25519,
including both RFC 9864 identifiers.

Also drops a kty assignment that jwx ignores and that would have mislabelled
non-RSA keys had it taken effect.
2026-08-10 12:34:22 +02:00
Trong Huu Nguyen 6cf48cd668 feat(openid/config): validate client assertion alg against provider metadata
Fail at startup rather than on the first token request when the identity
provider does not accept the client assertion algorithm. The check is
skipped when the provider omits the field, which is optional in Discovery.
2026-08-10 12:33:16 +02:00
Trong Huu Nguyen 2c708d554c feat(openid/config): require and expose the client JWK algorithm
Both the assertion signer and the provider validation derived the algorithm
from the key and had to handle a missing "alg" that NewClientConfig already
rejects. Validate it once at construction and keep the result.
2026-08-10 12:32:04 +02:00
Trong Huu Nguyen f3f76fccc8 refactor(config)!: rename id-token-signing-alg to jwks-fallback-alg
The flag never described id_token verification. It assigns an algorithm to
provider JWKS keys that omit "alg", which is a compatibility shim for
providers such as Azure and ID-porten.

BREAKING CHANGE: openid.id-token-signing-alg is now openid.jwks-fallback-alg.
A config file with the old key fails to start. The old environment variable
is ignored and the value falls back to RS256.
2026-08-10 12:30:25 +02:00
Trong Huu Nguyen b71e1b0b0f refactor(handler/sso/server): remove redundant logout overrides
These are no longer necessary after d2e01b2e.
2026-08-10 09:58:05 +02:00
Trong Huu Nguyen f2567ccdfe feat(mock): enforce unique jti and lifetime for client assertions 2026-08-06 13:22:10 +02:00
Trong Huu Nguyen 47c10f5a9b fix(openid/client): mint new client assertions for each PAR retry 2026-08-06 13:12:15 +02:00
Trong Huu Nguyen b870e8ec7b fix(openid/client): cap client assertion lifetime to 5 seconds 2026-08-06 12:57:36 +02:00
Trong Huu Nguyen aa74b67a33 refactor: remove unreachable functions
Both were reported by deadcode: retry.WithBase was never used, and
PrometheusMiddleware.Initialize is a leftover from the chi-prometheus
code this middleware was adapted from.
2026-07-28 12:52:45 +02:00
Trong Huu Nguyen cb14d1624f fix(metrics): record on the collector that is actually registered
When an equal collector was already registered, the caller kept using
its own instance, whose observations are never scraped. RegisterCollector
now returns the registered collector so callers can adopt it.
2026-07-28 12:52:45 +02:00
Trong Huu Nguyen 070a4127ae test: cover the hardened provider fetch and client redirect policy
Verified that the redirect test fails without the CheckRedirect policy:
without it the redirect is followed and the target is reached.
2026-07-28 12:52:45 +02:00
Trong Huu Nguyen b99a00479d fix: annotate reviewed gosec findings
Each finding was reviewed individually; the annotations record why the
flagged code is safe rather than suppressing the rules globally.

- G101: viper configuration keys, not credentials
- G117: the marshalled value is the plaintext input to the encryption
  that immediately follows, or a token endpoint response that is
  required to carry tokens
- G118: the background deletion must outlive the request, so the
  request context deliberately is not used
- G124: cookie attributes are validated in config.Cookie.Validate
- G710: the redirect targets are relative by construction, validated by
  the url validators, or read from the provider's metadata document
2026-07-28 12:52:34 +02:00
Trong Huu Nguyen 9600b8ffda fix(mock): set ReadHeaderTimeout on the relying party server
The bare http.Server replaces the one httptest provides, dropping its
timeouts along with it.
2026-07-28 11:43:54 +02:00
Trong Huu Nguyen 29c7de357e fix(handler): scope the background session deletion error
The retry callback assigned to the enclosing if-scoped err instead of
declaring its own. Nothing read that variable after the handler
returned, so behaviour is unchanged.
2026-07-28 11:43:54 +02:00
Trong Huu Nguyen 4b56c77ddb fix(openid/client): refuse redirects for credential-bearing requests
Requests to the token and pushed authorization endpoints carry the
client secret or a signed client assertion. Following a redirect would
forward those credentials to a host the provider never advertised.
2026-07-28 11:43:53 +02:00
Trong Huu Nguyen 4d1d591448 fix(openid): bound and validate the provider metadata fetch
The metadata document was fetched with http.Get, which has no deadline,
so a hung connection blocked startup indefinitely. The response status
was never checked either, leaving an error page to surface as a JSON
decoding error.

The fetch now runs with a 10s deadline derived from the context passed
in from main, goes through the shared transport so it is traced and
pooled, and rejects any non-200 response.
2026-07-28 11:43:48 +02:00
Trong Huu Nguyen ea20e96f87 fix(metrics): log collector registration failures
Registration errors were discarded, leaving the collector silently
absent from the metrics endpoint while the vectors still recorded.
Duplicate registrations remain ignored; they are expected when several
instances are constructed in the same process.
2026-07-28 09:05:04 +02:00
Trong Huu Nguyen c56625d842 fix: check or explicitly ignore returned errors
Satisfies errcheck. Errors that carry no actionable information are
ignored explicitly: writes to an already-committed response, and closing
a fully read response body or file.

Test fixture setup asserts with require.NoError instead, since a failure
there means the fixture itself is broken.
2026-07-28 09:04:59 +02:00
Trong Huu Nguyen c6e711cd85 refactor: drop redundant embedded field selectors
Satisfies staticcheck QF1008.
2026-07-28 09:04:42 +02:00
Trong Huu Nguyen 704f455291 refactor(session): replace -1 sentinels with named constant
Introduce SecondsNotApplicable for *_in_seconds fields whose feature is
disabled (no inactivity timeout, auto-refresh off), replacing scattered
magic -1 values and the post-construction mutation in Verbose(). JSON
output is unchanged.
2026-06-10 16:58:52 +02:00
Trong Huu Nguyen 08cf4260e7 refactor(session): clarify token refresh scheduling
Consolidate the refresh schedule into a single NextRefresh function (leeway +
inactivity half-life, floored at the cooldown). ShouldRefresh now defers to it,
so the explicit cooldown gate is redundant and removed. Add doc comments naming
the mechanisms. No behavior change.
2026-06-10 16:58:41 +02:00
Trong Huu Nguyen 8fbd4656db fix(session): refresh tokens 30s before expiry instead of 5m
Refreshes are activity-based and synchronous-on-expiry, so the proactive
leeway only hides refresh latency and provides retry headroom; it is not
what prevents serving expired tokens. Shrink it from 5m to 30s to align
with identity provider guidance against refreshing long before expiry,
while keeping decent end-user UX.
2026-06-10 16:57:54 +02:00
Trong Huu Nguyen 9f3d10fcaf feat(openid/client): set nbf claim for client assertions 2026-06-09 10:00:22 +02:00
Trong Huu Nguyen a8c5ad891f style: go fmt 2026-05-26 13:27:55 +02:00
Trong Huu Nguyen 66d2a14e47 fix(handler/error): respect log level parameter in respondError
The previous if/else only checked for WarnLevel, causing any other
level (e.g. InfoLevel in 24fb9b1) to fall through to Errorf. Use Logf to
dispatch on the actual level passed by callers.
2026-05-11 15:28:39 +02:00
Trong Huu Nguyen 24fb9b1a1d refactor(handler/error): reduce log severity for spammy warnings 2026-05-08 07:52:14 +02:00
Trong Huu Nguyen 66d734358d refactor: use errgroup for coordinated server lifecycle
Move metrics and probe listeners from main.go into server.go and
manage all servers with errgroup. This replaces log.Fatalf goroutines
with proper error propagation and ties all server lifetimes together
so a failure in any listener triggers graceful shutdown of the others.
2026-05-08 07:49:17 +02:00
Trong Huu Nguyen b1e3732ec3 feat: add optional pprof debug endpoints on probe server
Enabled via --pprof-enabled flag. Exposes /debug/pprof/ routes on the
probe bind address for runtime profiling.
2026-05-07 16:01:13 +02:00
Trong Huu Nguyen 1939d18ba8 fix: add server timeouts to prevent goroutine/memory leak from idle keep-alive connections
Without IdleTimeout, clients holding keep-alive connections open indefinitely
caused server-side goroutines (and their ~16KB of buffers) to accumulate
linearly until OOM.
2026-05-07 16:01:11 +02:00
Trong Huu Nguyen c02a932d72 feat(openid): support domain_hint for authorization grant 2026-03-03 09:38:24 +01:00
Trong Huu Nguyen 02842773d3 style: go fix 2026-03-03 08:35:11 +01:00
Trong Huu Nguyen 466d0132e9 feat(openid): support logout_hint for RP-initiated logouts 2026-01-16 09:18:30 +01:00
Trong Huu Nguyen 5febe7c766 feat: remove support for legacy cookie 2025-12-03 13:54:11 +01:00
Trong Huu Nguyen da69847027 feat(openid): add opt-in toggle for typ header in accordance with RFC7523bis
Some providers require that the `typ` header has a value exactly equal
to `client-authentication+jwt` in accordance with changes introduced by
RFC7523bis.

This commit allows for opting in to setting the `typ` header with this new value.

The default behaviour is to use the previous de facto standard value, `JWT`.
Once the changes in RFC7523bis lands in the affected standards and
identity providers start supporting the new `typ` header (Entra ID being
notable for not supporting this as of this commit), we will default to
use `client-authentication+jwt`.
2025-08-26 08:29:27 +02:00
Trong Huu Nguyen 75ff84ba28 refactor: suppress some noisy startup logs 2025-07-09 13:33:40 +02:00
Trong Huu Nguyen 2e3da2bb64 feat: add separate health probe listener
This adds an optional health probe listener, mostly for use in rutime
environments where you want separate public and private listeners.

The existing /oauth2/ping endpoint on the main listener is kept for
backwards compatibility.
2025-07-08 11:26:22 +02:00
Trong Huu Nguyen a820dc5b9b perf(session/redis): disable caller tracing
This drops the calling function, filepath and line number attributes
for redisotel tracing. The underlying implementation here calls
runtime.Callers, which are more expensive than we'd like.
2025-07-01 14:03:04 +02:00
Trong Huu Nguyen 4bf3b1bdd4 refactor: move string generator to crypto package 2025-06-16 09:55:44 +02:00
Trong Huu Nguyen 4e1c8e68f8 feat(openid): retry front-channel logouts 2025-06-16 09:55:44 +02:00
Trong Huu Nguyen a156c11ace refactor(session): use SetSpanAttributes on refresh 2025-06-16 09:55:43 +02:00
Trong Huu Nguyen 1a138c66a3 refactor: move retry package 2025-06-16 09:55:42 +02:00
Trong Huu Nguyen b3c2c72155 feat(openid): only set max_age parameter for prompt=login
We generally don't want to instruct the identity provider
to attempt full reauthentication when switching accounts
with `prompt=select_account`.
2025-06-11 13:28:52 +02:00
Trong Huu Nguyen 052d310280 fix(openid): require expires_in for token responses
While RFC 6749 specify this field as recommended:

> If omitted, the authorization server SHOULD provide the
> expiration time via other means or document the default value.

and equivalently the OIDC Core spec specifies the same field as optional,
we will explicitly enforce that these fields are returned from the AS.

This isn't a breaking change as the existing session refresh logic implicitly
depends on this field and its value.

While there are probably some providers that omit the `expires_in` field
or sets it to zero with the intent of returning access tokens that do not
expire, we assume these are relatively rare. We might revisit this
at some point in the future, should our assumptions be wrong.
2025-06-11 13:07:18 +02:00
Trong Huu Nguyen bf2f97f400 feat: set more session and token-related span attributes 2025-06-10 13:51:15 +02:00
Trong Huu Nguyen 9bb5ac9210 fix(openid/client): also accept acr and locale params when no defaults are configured 2025-05-23 09:00:45 +02:00
Trong Huu Nguyen b9963b19f9 refactor(openid): clean up id_token validation tests 2025-05-23 08:59:42 +02:00
Trong Huu NguyenandThomas Krampl c5ec362e60 feat(session): update id_token in session if returned from refresh grant
Co-authored-by: Thomas Krampl <thomas.siegfried.krampl@nav.no>
2025-05-22 15:52:15 +02:00
Trong Huu NguyenandThomas Krampl 192cd86022 feat: use id_token instead of access_token for forward-auth headers
Co-authored-by: Thomas Krampl <thomas.siegfried.krampl@nav.no>
2025-05-22 11:09:59 +02:00