Skip to content

fix(sso): test the connection with the configured redirect URI - #440

Open
SebastianOehm wants to merge 2 commits into
admonstrator:mainfrom
SebastianOehm:fix-438-sso-test-redirect-uri
Open

SebastianOehm wants to merge 2 commits into
admonstrator:mainfrom
SebastianOehm:fix-438-sso-test-redirect-uri

Conversation

@SebastianOehm

Copy link
Copy Markdown

Fixes #438

Problem

"Test connection" on Settings → Single sign-on checks the client by redeeming a made-up authorization code at the token endpoint. The request sent redirect_uri = <issuer origin>/auth/oidc/callback. The issuer almost always runs on a different host than Zettelrobbe, so that URI is usually not the registered one. Authentik compares the redirect URI with its strict list and answers a mismatch with invalid_client, so in the reporter's setup the test always failed and told the user to check the client ID, secret and auth method, even though the actual login (which uses resolveRedirectUri()) worked.

Changes

  • services/oidcService.js: testConnection() takes a redirectUri and sends it in the probe instead of the issuer origin. The invalid_client message keeps its hint about client ID, secret and auth method, and now adds that some providers answer the same way when the redirect URI is not registered, naming the URI that was sent.
  • routes/settingsConnections.js: POST /api/settings/oidc/test accepts an optional redirectUri (400 "The redirect URI must start with https:// or http://." if it is not an absolute http(s) URL). When it is empty or missing, the endpoint uses resolveRedirectUri(req), the same value a login uses: OIDC_REDIRECT_URI, otherwise the callback derived from the request. Swagger JSDoc updated. This route is the only caller of testConnection().
  • public/js/modules/settings-sso.js: the test button sends the Redirect URI field like the other fields it tests (unsaved values), or the detected URI when the field is empty. The page therefore always sends a value; the server-side fallback matters for direct API calls.
  • OPENAPI/openapi.json: regenerated.
  • tests/test-settings-oidc.js: the fake provider records redirect_uri and can enforce a strict redirect list (invalid_client on mismatch, like Authentik). Four new test cases (typed URI, with surrounding spaces, sent to a strict provider; stored OIDC_REDIRECT_URI used when none is typed; request-derived URI used when none is stored, never the issuer origin; unregistered URI named in the error) plus a malformed redirectUri added to the existing 400 input test. All five checks fail without the fix.

The login flow itself is unchanged; only the Test connection button and its endpoint behave differently, and the test still saves nothing.

Testing

  • node scripts/run-tests.js --all: 155 test files passed, 8 skipped (need a running server or poppler), 0 failed; the same result as on main (no test files added).
  • test-settings-oidc: 28 checks passed; with the old service and route, 5 fail.
  • ESLint and Prettier on the changed files, npm run typecheck, regen-openapi.js result committed.
  • End to end with the Docker image and a fake provider that checks redirect URIs strictly, using the issuer layout from the issue (.../application/o/zettelrobbe/). An empty Redirect URI field (detected URI) and the registered URI typed in both report success, with SSO switched off and on. An unregistered URI fails and the message names it. An image built from main sends http://<issuer host>/auth/oidc/callback and fails, which reproduces the report. Not tested against a real Authentik instance.

Background
The single sign-on "Test connection" (POST /api/settings/oidc/test) checks
the client by redeeming a made-up authorization code. It sent
redirect_uri = <issuer origin>/auth/oidc/callback, a URI that is never the
registered one because the provider runs on another host. Authentik
compares the redirect URI with its strict list before it looks at the code
and answers a mismatch with invalid_client, so the test always failed and
blamed the client ID, secret and auth method, while the real login (which
uses resolveRedirectUri()) worked.

Changes
- services/oidcService.js: testConnection() takes redirectUri and sends it
  in the probe instead of the issuer origin. The invalid_client message
  adds that some providers answer the same way for an unregistered
  redirect URI and names the URI that was sent.
- routes/settingsConnections.js: the test endpoint accepts an optional
  redirectUri (400 when it is not an absolute http(s) URL); empty falls
  back to resolveRedirectUri(req), i.e. OIDC_REDIRECT_URI, else the
  callback derived from the request (honours TRUST_PROXY). Swagger updated.
- public/js/modules/settings-sso.js: the test sends the Redirect URI field
  like every other field it tests, or the detected URI when it is empty.
- OPENAPI/openapi.json: regenerated.
- tests/test-settings-oidc.js: the fake provider records redirect_uri and
  can enforce a strict redirect list (invalid_client on mismatch, like
  Authentik). New cases: typed URI sent to a strict provider, stored
  OIDC_REDIRECT_URI used when none is typed, derived URI used when none is
  stored (never the issuer origin), unregistered URI named in the error,
  malformed URI refused with 400. Five of them fail without the fix.

Testing
- node scripts/run-tests.js --all: 155 passed, 8 skipped (need a server
  or poppler), 0 failed; same as before the change.
- test-settings-oidc: 28 passed; with the old service and route 5 fail.
- npx eslint and prettier --check on the changed files, npm run typecheck,
  regen-openapi.js committed.
- End to end with the Docker image and a strict fake provider (issuer
  .../application/o/zettelrobbe/): empty Redirect URI field (detected
  URI), the registered URI typed, SSO switched off and on all report
  success; an unregistered URI fails and the message names it. The image
  built from main sends http://<issuer host>/auth/oidc/callback and fails.

Impact
Settings page connection test only. The login flow is unchanged. API
callers that omit redirectUri now get the URI a login would use.

Upstream Status
Not applicable; single sign-on is specific to this fork.

Fixes admonstrator#438
Resolve the conflict in the invalid_client message of
oidcService.testConnection(): keep main's new wording ("token
authentication") and append this branch's redirect URI hint.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: SSO "Test connection" uses the issuer origin as redirect_uri and fails with invalid_client on Authentik

1 participant