Skip to content

Commit 44e9cda

Browse files
authored
test: OIDC social-login setPassword step-up fallback E2E (#90) (#93)
* test: add OIDC social-login setPassword step-up fallback E2E (#90) Follow-up to #75. Adds the browser E2E that proves an OIDC (Keycloak) account with no passkey has POST /user/setPassword governed by allowInitialPasswordSetWithoutStepUp (403 when false, succeeds when true) rather than a permanent 401 step-up, and that a freshly logged-in OIDC user can enroll a first passkey (FACTOR_AUTHORIZATION_CODE). - New chromium-step-up-oidc Playwright project (grep @step-up-oidc), excluded from the default/step-up projects. - globalSetup/globalTeardown start a dev-mode Keycloak container (realm mounted, no external DB) when KEYCLOAK_E2E is set; reuse an already running provider, and fail fast rather than destroy a live one. - step-up-oidc.spec.ts: real Keycloak redirect login, both fallback branches (selected per app boot via STEP_UP_OIDC_ALLOW_INITIAL and a SPRING_APPLICATION_JSON override), and first-passkey enrollment. - New playwright-tests-oidc CI job mirroring playwright-tests, with a Keycloak provider (via globalSetup) and a Mailpit service so the passkey-registration notification stays local instead of hitting SES. - Docs: TESTING.md and AUTHENTICATION.md describe the new project, run commands, and the local-DB reset caveat. Claude-Session: https://claude.ai/code/session_01EJY7pA4NvY9vJt6CfDBVWt * ci: run OIDC step-up E2E job only on main pushes (#90) The playwright-tests-oidc job is heavier than the others (two app boots plus a Keycloak container), so gate it to pushes on main rather than running it on every pull request. Claude-Session: https://claude.ai/code/session_01EJY7pA4NvY9vJt6CfDBVWt * test: extract shared WebAuthn E2E helpers and harden Keycloak startup Addresses the two low-severity review findings on PR #93 (#90). - Extract setupVirtualAuthenticator, addVirtualAuthenticator, and getCredentialIds into playwright/src/utils/webauthn.ts and export them from utils/index.ts. The step-up-oidc, step-up-flow, and mfa-flow specs now import these instead of each carrying a local copy (mfa-flow previously inlined the authenticator setup). - startKeycloak now removes the container it started if waitForMetadata times out. Playwright's separate globalTeardown is not guaranteed to run when globalSetup throws, so a readiness timeout could otherwise leave the container and ownership marker behind, which the next run's "already running but not answering" guard would reject until a manual docker rm -f. Claude-Session: https://claude.ai/code/session_01EJY7pA4NvY9vJt6CfDBVWt
1 parent 25bec70 commit 44e9cda

13 files changed

Lines changed: 589 additions & 70 deletions

File tree

‎.github/workflows/tests.yml‎

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,116 @@ jobs:
121121
with:
122122
name: playwright-report
123123
path: playwright/reports/
124+
125+
playwright-tests-oidc:
126+
name: Playwright OIDC Step-Up Tests
127+
# Heavier than the other jobs (two app boots plus a Keycloak container), so it runs only on pushes
128+
# to main, not on every pull request. The `push` trigger is already scoped to main; the ref check
129+
# keeps it correct if more push branches are added later.
130+
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
131+
runs-on: ubuntu-latest
132+
timeout-minutes: 30
133+
services:
134+
mariadb:
135+
image: mariadb:12.2
136+
env:
137+
MARIADB_DATABASE: springuser
138+
MARIADB_USER: springuser
139+
MARIADB_PASSWORD: springuser
140+
MARIADB_ROOT_PASSWORD: rootpassword
141+
ports:
142+
- 3306:3306
143+
options: >-
144+
--health-cmd="healthcheck.sh --connect --innodb_initialized"
145+
--health-interval=10s
146+
--health-timeout=5s
147+
--health-retries=5
148+
mailpit:
149+
# The first-passkey-enrollment test registers a passkey, and user.webauthn.notifyOnRegistration
150+
# is on by default, so the app sends a notification email. Without a relay it would attempt a real
151+
# outbound SMTP connection to the base config's SES host on every run; catch it in Mailpit instead
152+
# (the mail env below points spring.mail here). The test does not assert the mail; this just keeps
153+
# the send local.
154+
image: axllent/mailpit:v1.30.7
155+
env:
156+
MP_SMTP_AUTH_ACCEPT_ANY: "1"
157+
MP_SMTP_AUTH_ALLOW_INSECURE: "1"
158+
ports:
159+
- 1025:1025
160+
- 8025:8025
161+
options: >-
162+
--health-cmd="/mailpit readyz"
163+
--health-interval=10s
164+
--health-timeout=5s
165+
--health-retries=5
166+
env:
167+
# The MariaDB service container replaces Spring Boot's Docker Compose integration.
168+
SPRING_DOCKER_COMPOSE_ENABLED: "false"
169+
# Point mail at the Mailpit service container so passkey-registration notifications stay local
170+
# (mirrors docker-compose-keycloak.yml's SPRING_MAIL_* for the same plain-SMTP, no-auth setup).
171+
SPRING_MAIL_HOST: localhost
172+
SPRING_MAIL_PORT: "1025"
173+
SPRING_MAIL_PROPERTIES_MAIL_SMTP_AUTH: "false"
174+
SPRING_MAIL_PROPERTIES_MAIL_SMTP_STARTTLS_ENABLE: "false"
175+
SPRING_MAIL_PROPERTIES_MAIL_SMTP_STARTTLS_REQUIRED: "false"
176+
# Tell Playwright's globalSetup to start a Keycloak provider (via docker) for this run.
177+
KEYCLOAK_E2E: "1"
178+
# OIDC client + provider config for the app under test. The app runs on the runner host (started by
179+
# Playwright's webServer), not inside a compose network, so every provider URI points at the host's
180+
# published Keycloak port 8180 rather than the compose-network keycloak:8080. Client id/secret match
181+
# keycloak/realm/realm-export.json (dev-only credentials committed to the repo).
182+
DS_SPRING_USER_KEYCLOAK_CLIENT_ID: ds-spring-user-framework-demo
183+
DS_SPRING_USER_KEYCLOAK_CLIENT_SECRET: FTp1j7sGvc4g3MFdghEX4n7RPhbu86PQ
184+
DS_SPRING_USER_KEYCLOAK_PROVIDER_AUTHORIZATION_URI: http://localhost:8180/realms/demo/protocol/openid-connect/auth
185+
DS_SPRING_USER_KEYCLOAK_PROVIDER_TOKEN_URI: http://localhost:8180/realms/demo/protocol/openid-connect/token
186+
DS_SPRING_USER_KEYCLOAK_PROVIDER_USER_INFO_URI: http://localhost:8180/realms/demo/protocol/openid-connect/userinfo
187+
DS_SPRING_USER_KEYCLOAK_PROVIDER_JWK_SET_URI: http://localhost:8180/realms/demo/protocol/openid-connect/certs
188+
steps:
189+
- uses: actions/checkout@v4
190+
191+
- uses: actions/setup-java@v4
192+
with:
193+
distribution: temurin
194+
java-version: 21
195+
196+
- uses: gradle/actions/setup-gradle@v4
197+
198+
- name: Build application
199+
run: ./gradlew assemble
200+
201+
- uses: actions/setup-node@v4
202+
with:
203+
node-version: 20
204+
cache: npm
205+
cache-dependency-path: playwright/package-lock.json
206+
207+
- name: Install Playwright dependencies
208+
working-directory: playwright
209+
run: |
210+
npm ci
211+
npx playwright install --with-deps chromium
212+
213+
# Two boots: the allowInitialPasswordSetWithoutStepUp flag is boot-time config, so each branch of
214+
# the OIDC fallback needs its own app start. globalSetup brings Keycloak up for each run.
215+
- name: Run E2E (OIDC step-up, initial password allowed)
216+
working-directory: playwright
217+
env:
218+
APP_PROFILES: docker-keycloak,playwright-test,step-up
219+
STEP_UP_OIDC_ALLOW_INITIAL: "true"
220+
run: npx playwright test --project=chromium-step-up-oidc
221+
222+
- name: Run E2E (OIDC step-up, initial password denied)
223+
working-directory: playwright
224+
env:
225+
APP_PROFILES: docker-keycloak,playwright-test,step-up
226+
STEP_UP_OIDC_ALLOW_INITIAL: "false"
227+
# Override playwright-test's flag (true) so the denial branch is exercised.
228+
SPRING_APPLICATION_JSON: '{"user":{"security":{"allowInitialPasswordSetWithoutStepUp":false}}}'
229+
run: npx playwright test --project=chromium-step-up-oidc
230+
231+
- name: Upload Playwright report
232+
if: failure()
233+
uses: actions/upload-artifact@v4
234+
with:
235+
name: playwright-report-oidc
236+
path: playwright/reports/

‎docs/AUTHENTICATION.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ browser (ceremony then retry succeeds) and the negative path (absent `WEBAUTHN`
125125
runs no ceremony). Run it with
126126
`APP_PROFILES=local,playwright-test,step-up npx playwright test --project=chromium-step-up`.
127127

128+
The social-login fallback is covered by the `chromium-step-up-oidc` project
129+
([`step-up-oidc.spec.ts`](../playwright/tests/step-up/step-up-oidc.spec.ts)): after a real Keycloak OIDC
130+
login, `setPassword` on the passkey-less account is governed by `allowInitialPasswordSetWithoutStepUp`
131+
(`403` when false, success when true) rather than a permanent `401`, and a freshly logged-in OIDC user can
132+
enroll a first passkey. It needs a Keycloak provider and runs the app on
133+
`docker-keycloak,playwright-test,step-up`; see [TESTING.md](TESTING.md) for the run command.
134+
128135
## MFA
129136

130137
The `mfa` profile turns on `user.mfa.enabled` (`application-mfa.yml:20`), `false` in the base config

‎docs/TESTING.md‎

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ disables verification/reset emails (tests fetch tokens via the Test API instead)
100100
"set initial password" flow works without a `StepUpService` bean.
101101

102102
The `chromium`, `firefox`, `webkit`, `Mobile Chrome`, and `Mobile Safari` projects skip specs
103-
tagged `@mfa-enabled` and `@step-up-enabled` (`grepInvert`); separate Chromium-only projects run
104-
those, each against a server started with the matching add-on profile. Both use the CDP virtual
105-
authenticator, so they are Chromium-only.
103+
tagged `@mfa-enabled`, `@step-up-enabled`, and `@step-up-oidc` (`grepInvert`); separate Chromium-only
104+
projects run those, each against a server started with the matching add-on profile. They use the CDP
105+
virtual authenticator (and, for OIDC, a Keycloak provider), so they are Chromium-only.
106106

107107
```bash
108108
# MFA flow (@mfa-enabled)
@@ -123,8 +123,40 @@ The step-up run adds `step-up-e2e` (`application-step-up-e2e.yml`), a test-only
123123
`bootRun` starts Mailpit automatically alongside MariaDB. The realistic demo values stay in
124124
`application-step-up.yml` (`ttlSeconds: 120`). The step-up specs run serially
125125
(`test.describe.configure({ mode: 'serial' })`) because concurrent account registration deadlocks in
126-
MariaDB (framework issue devondragon/SpringUserFramework#368). One acceptance case is not covered here:
127-
social-login (OIDC) `setPassword` fallback, which needs the Keycloak stack (tracked separately).
126+
MariaDB (framework issue devondragon/SpringUserFramework#368).
127+
128+
The social-login (OIDC) `setPassword` fallback is covered separately by the `chromium-step-up-oidc`
129+
project ([`step-up-oidc.spec.ts`](../playwright/tests/step-up/step-up-oidc.spec.ts)), because it needs a
130+
real OpenID provider. `globalSetup` starts a dev-mode Keycloak (`quay.io/keycloak/keycloak:25.0.6
131+
start-dev --import-realm`, the `keycloak/realm` export mounted, no external database) whenever
132+
`KEYCLOAK_E2E` is set, and `globalTeardown` removes it; an already-running Keycloak is reused and left
133+
alone. The app runs on the host on `docker-keycloak,playwright-test,step-up`, with the
134+
`DS_SPRING_USER_KEYCLOAK_*` provider URIs pointed at the host's published Keycloak port (`localhost:8180`)
135+
rather than the compose-network `keycloak:8080`. The `allowInitialPasswordSetWithoutStepUp` flag is
136+
boot-time config, so each branch is its own app boot, selected by `STEP_UP_OIDC_ALLOW_INITIAL`:
137+
138+
```bash
139+
# setPassword succeeds (flag true, from playwright-test)
140+
KEYCLOAK_E2E=1 STEP_UP_OIDC_ALLOW_INITIAL=true \
141+
DS_SPRING_USER_KEYCLOAK_CLIENT_ID=ds-spring-user-framework-demo \
142+
DS_SPRING_USER_KEYCLOAK_CLIENT_SECRET=FTp1j7sGvc4g3MFdghEX4n7RPhbu86PQ \
143+
DS_SPRING_USER_KEYCLOAK_PROVIDER_AUTHORIZATION_URI=http://localhost:8180/realms/demo/protocol/openid-connect/auth \
144+
DS_SPRING_USER_KEYCLOAK_PROVIDER_TOKEN_URI=http://localhost:8180/realms/demo/protocol/openid-connect/token \
145+
DS_SPRING_USER_KEYCLOAK_PROVIDER_USER_INFO_URI=http://localhost:8180/realms/demo/protocol/openid-connect/userinfo \
146+
DS_SPRING_USER_KEYCLOAK_PROVIDER_JWK_SET_URI=http://localhost:8180/realms/demo/protocol/openid-connect/certs \
147+
APP_PROFILES=docker-keycloak,playwright-test,step-up \
148+
npx playwright test --project=chromium-step-up-oidc
149+
150+
# setPassword denied with 403, not a permanent 401 (flag false)
151+
# ...same env, but: STEP_UP_OIDC_ALLOW_INITIAL=false and
152+
# SPRING_APPLICATION_JSON='{"user":{"security":{"allowInitialPasswordSetWithoutStepUp":false}}}'
153+
```
154+
155+
The spec logs in as the realm's single seeded user (`demo@example.com`) and resets the local
156+
KEYCLOAK-provisioned account before each test so re-runs stay deterministic. Run locally, `bootRun` uses
157+
your normal development database (`compose.dev.yaml`, port 3306), so the reset deletes any local account
158+
at that address: do not keep a real account you care about under `demo@example.com` in your dev database.
159+
In CI the database is a throwaway service container, so nothing persists.
128160

129161
**Test API**:
130162
[`TestDataController`](../src/main/java/com/digitalsanctuary/spring/demo/test/api/TestDataController.java)
@@ -138,6 +170,11 @@ disables CSRF for `/api/test/**` and restricts it to requests from `127.0.0.1`,
138170

139171
[`.github/workflows/tests.yml`](../.github/workflows/tests.yml) runs on pull requests and pushes
140172
to `main`: **`unit-tests`** runs `./gradlew test` on Java 21. **`playwright-tests`** builds the
141-
app, starts a `mariadb:12.2` service container, installs Playwright, then runs E2E twice: once
173+
app, starts a `mariadb:12.2` service container, installs Playwright, then runs E2E three times: once
142174
with `APP_PROFILES=playwright-test` against `chromium` (MFA off), once with
143-
`APP_PROFILES=playwright-test,mfa` against `chromium-mfa` (MFA on).
175+
`APP_PROFILES=playwright-test,mfa` against `chromium-mfa` (MFA on), and once with
176+
`APP_PROFILES=local,playwright-test,step-up,step-up-e2e` against `chromium-step-up`. **`playwright-tests-oidc`**
177+
covers the OIDC `setPassword` fallback: it starts the same MariaDB service (plus a Mailpit service), and
178+
`globalSetup` brings up a dev-mode Keycloak on the runner, then runs `chromium-step-up-oidc` twice (once per
179+
`allowInitialPasswordSetWithoutStepUp` branch). Because it is heavier (two app boots plus a Keycloak
180+
container), it runs only on pushes to `main`, not on every pull request.

‎playwright/global-setup.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { startKeycloak } from './src/utils/keycloak';
2+
3+
/**
4+
* Global setup, run once before the suite. Only the chromium-step-up-oidc project needs an external
5+
* OpenID provider, so this brings Keycloak up only when KEYCLOAK_E2E is set (the OIDC run command and
6+
* CI job set it). Every other project skips this and relies solely on the `webServer` block.
7+
*/
8+
async function globalSetup(): Promise<void> {
9+
if (!process.env.KEYCLOAK_E2E) return;
10+
await startKeycloak();
11+
}
12+
13+
export default globalSetup;

‎playwright/global-teardown.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { stopKeycloak } from './src/utils/keycloak';
2+
3+
/**
4+
* Global teardown, run once after the suite. Removes the Keycloak container, but only the one this run
5+
* started (stopKeycloak no-ops when the provider was reused rather than started here).
6+
*/
7+
async function globalTeardown(): Promise<void> {
8+
if (!process.env.KEYCLOAK_E2E) return;
9+
await stopKeycloak();
10+
}
11+
12+
export default globalTeardown;

‎playwright/playwright.config.ts‎

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ export default defineConfig({
2020
/* Unique output directories for this project */
2121
outputDir: path.join(__dirname, 'test-results', PROJECT_ID),
2222

23+
/* Bring up a Keycloak OpenID provider for the chromium-step-up-oidc project. Both hooks no-op unless
24+
* KEYCLOAK_E2E is set, so every other project runs without Docker. See src/utils/keycloak.ts. */
25+
globalSetup: path.join(__dirname, 'global-setup.ts'),
26+
globalTeardown: path.join(__dirname, 'global-teardown.ts'),
27+
2328
/* Run tests in files in parallel */
2429
fullyParallel: true,
2530

@@ -93,32 +98,32 @@ export default defineConfig({
9398
projects: [
9499
{
95100
name: 'chromium',
96-
grepInvert: /@mfa-enabled|@step-up-enabled/,
101+
grepInvert: /@mfa-enabled|@step-up-enabled|@step-up-oidc/,
97102
use: { ...devices['Desktop Chrome'] },
98103
},
99104

100105
{
101106
name: 'firefox',
102-
grepInvert: /@mfa-enabled|@step-up-enabled/,
107+
grepInvert: /@mfa-enabled|@step-up-enabled|@step-up-oidc/,
103108
use: { ...devices['Desktop Firefox'] },
104109
},
105110

106111
{
107112
name: 'webkit',
108-
grepInvert: /@mfa-enabled|@step-up-enabled/,
113+
grepInvert: /@mfa-enabled|@step-up-enabled|@step-up-oidc/,
109114
use: { ...devices['Desktop Safari'] },
110115
},
111116

112117
/* Test against mobile viewports */
113118
{
114119
name: 'Mobile Chrome',
115-
grepInvert: /@mfa-enabled|@step-up-enabled/,
120+
grepInvert: /@mfa-enabled|@step-up-enabled|@step-up-oidc/,
116121
use: { ...devices['Pixel 5'] },
117122
},
118123

119124
{
120125
name: 'Mobile Safari',
121-
grepInvert: /@mfa-enabled|@step-up-enabled/,
126+
grepInvert: /@mfa-enabled|@step-up-enabled|@step-up-oidc/,
122127
use: { ...devices['iPhone 12'] },
123128
},
124129

@@ -138,6 +143,29 @@ export default defineConfig({
138143
grep: /@step-up-enabled/,
139144
use: { ...devices['Desktop Chrome'] },
140145
},
146+
147+
/* Step-up OIDC fallback (issue #90): Chromium only, needs both a Keycloak OpenID provider and the
148+
* app on the docker-keycloak,playwright-test,step-up profiles. globalSetup starts Keycloak when
149+
* KEYCLOAK_E2E is set. The two allowInitialPasswordSetWithoutStepUp branches are separate app boots,
150+
* selected by STEP_UP_OIDC_ALLOW_INITIAL (and the matching SPRING_APPLICATION_JSON override):
151+
* # setPassword succeeds (flag true, from playwright-test):
152+
* KEYCLOAK_E2E=1 STEP_UP_OIDC_ALLOW_INITIAL=true \
153+
* DS_SPRING_USER_KEYCLOAK_CLIENT_ID=ds-spring-user-framework-demo \
154+
* DS_SPRING_USER_KEYCLOAK_CLIENT_SECRET=FTp1j7sGvc4g3MFdghEX4n7RPhbu86PQ \
155+
* DS_SPRING_USER_KEYCLOAK_PROVIDER_AUTHORIZATION_URI=http://localhost:8180/realms/demo/protocol/openid-connect/auth \
156+
* DS_SPRING_USER_KEYCLOAK_PROVIDER_TOKEN_URI=http://localhost:8180/realms/demo/protocol/openid-connect/token \
157+
* DS_SPRING_USER_KEYCLOAK_PROVIDER_USER_INFO_URI=http://localhost:8180/realms/demo/protocol/openid-connect/userinfo \
158+
* DS_SPRING_USER_KEYCLOAK_PROVIDER_JWK_SET_URI=http://localhost:8180/realms/demo/protocol/openid-connect/certs \
159+
* APP_PROFILES=docker-keycloak,playwright-test,step-up \
160+
* npx playwright test --project=chromium-step-up-oidc
161+
* # setPassword denied with 403, not a permanent 401 (flag false):
162+
* ... STEP_UP_OIDC_ALLOW_INITIAL=false \
163+
* SPRING_APPLICATION_JSON='{"user":{"security":{"allowInitialPasswordSetWithoutStepUp":false}}}' ... */
164+
{
165+
name: 'chromium-step-up-oidc',
166+
grep: /@step-up-oidc/,
167+
use: { ...devices['Desktop Chrome'] },
168+
},
141169
],
142170

143171
/* Run your local dev server before starting the tests */

‎playwright/src/utils/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ export {
1414
type UnlockUserResponse,
1515
type HealthResponse,
1616
} from './test-api-client';
17+
18+
export { setupVirtualAuthenticator, addVirtualAuthenticator, getCredentialIds } from './webauthn';

0 commit comments

Comments
 (0)