diff --git a/.changeset/types-0-41.md b/.changeset/types-0-41.md new file mode 100644 index 0000000..16bbd7e --- /dev/null +++ b/.changeset/types-0-41.md @@ -0,0 +1,5 @@ +--- +"seamless-cli": patch +--- + +Depends on `@seamless-auth/types` 0.41.0. diff --git a/.changeset/verify-step-up-after-sign-in.md b/.changeset/verify-step-up-after-sign-in.md new file mode 100644 index 0000000..df44f45 --- /dev/null +++ b/.changeset/verify-step-up-after-sign-in.md @@ -0,0 +1,5 @@ +--- +"seamless-cli": patch +--- + +`seamless verify` holds the API to the step-up and registration contract from seamless-auth-api v0.24. A step-up only accepts a factor enrolled before the session signed in, so the step-up spec and the owner admin helper sign in again after enrolling a factor and step that session up. Registering an address that already has a verified account is answered as an unclaimed sign-up, so the owner signs in through `/login` once the account exists, and the owner admin spec checks that the owner holds the admin role either way. Both work against earlier API releases too. diff --git a/package-lock.json b/package-lock.json index 0040101..f16f2a2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,17 +1,17 @@ { "name": "seamless-cli", - "version": "0.29.0", + "version": "0.30.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "seamless-cli", - "version": "0.29.0", + "version": "0.30.3", "license": "Apache-2.0", "dependencies": { "@clack/prompts": "^1.0.1", "@napi-rs/keyring": "^1.3.0", - "@seamless-auth/types": "^0.40.0", + "@seamless-auth/types": "^0.41.0", "adm-zip": "^0.6.1", "jose": "^6.2.15", "kleur": "^4.1.5", @@ -1431,9 +1431,9 @@ "peer": true }, "node_modules/@seamless-auth/types": { - "version": "0.40.0", - "resolved": "https://registry.npmjs.org/@seamless-auth/types/-/types-0.40.0.tgz", - "integrity": "sha512-NpFVCWE0VSNrBK9M4yBxsloyQgGHXP6B8/QmDPxsFz48/qV6jcDqdUpuV4PAJslG9B9X+4BcnTSQzI+iPSRNsA==", + "version": "0.41.0", + "resolved": "https://registry.npmjs.org/@seamless-auth/types/-/types-0.41.0.tgz", + "integrity": "sha512-1/hETcuRN0FsdTNrHEwAWLENKTIIQuvIQ+cAyKLppWor4ovIkP9mPuF0f52CMFnjyWCvBavmgmJZWeaQyQsZgA==", "license": "Apache-2.0", "dependencies": { "zod": "^4.5.0" diff --git a/package.json b/package.json index 3304fd0..550997b 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,7 @@ "dependencies": { "@clack/prompts": "^1.0.1", "@napi-rs/keyring": "^1.3.0", - "@seamless-auth/types": "^0.40.0", + "@seamless-auth/types": "^0.41.0", "adm-zip": "^0.6.1", "jose": "^6.2.15", "kleur": "^4.1.5", diff --git a/verify/harness/api/ownerAdmin.spec.ts b/verify/harness/api/ownerAdmin.spec.ts index 5a72fc1..d1857ef 100644 --- a/verify/harness/api/ownerAdmin.spec.ts +++ b/verify/harness/api/ownerAdmin.spec.ts @@ -1,24 +1,17 @@ import { OWNER_EMAIL } from '../lib/env'; import { expect, test } from '../lib/fixtures'; -import { registerEmail, requestEmailOtp, verifyEmailOtp } from '../lib/flows'; +import { ownerSession } from '../lib/flows'; test.describe('owner admin (api)', () => { - // The owner address is fixed in the stack's OWNER_EMAIL before boot, so this - // spec registers that exact email rather than the actor fixture's random one. - test('registering the owner email grants the admin role at signup', async ({ actor }) => { - const ephemeral = await registerEmail(actor.ctx, OWNER_EMAIL); - const code = await requestEmailOtp(actor.ctx, ephemeral); - - const res = await verifyEmailOtp(actor.ctx, ephemeral, code); - expect(res.ok(), `verify-email-otp -> ${res.status()} ${await res.text()}`).toBeTruthy(); - - const body = await res.json(); - expect(body.token, 'session access token').toBeTruthy(); - - const roles = (body.roles ?? []) as string[]; + // The owner address is fixed in the stack's OWNER_EMAIL before boot, so this spec + // uses that exact email rather than the actor fixture's random one. Another spec may + // have registered it already, so it signs in when the account exists. + test('the owner email is granted the admin role', async ({ actor }) => { + const { token, roles } = await ownerSession(actor.ctx, OWNER_EMAIL); + expect(token, 'session access token').toBeTruthy(); expect( roles.includes('admin'), - `owner signup grants the admin role (got ${JSON.stringify(roles)})`, + `the owner holds the admin role (got ${JSON.stringify(roles)})`, ).toBeTruthy(); }); }); diff --git a/verify/harness/api/stepUp.spec.ts b/verify/harness/api/stepUp.spec.ts index ba02fb2..a6069bd 100644 --- a/verify/harness/api/stepUp.spec.ts +++ b/verify/harness/api/stepUp.spec.ts @@ -1,22 +1,27 @@ import { expect, test } from '../lib/fixtures'; -import { registerAndVerifyEmail } from '../lib/flows'; +import { login, registerAndVerifyEmail, verifyLoginEmailOtp } from '../lib/flows'; import { totp } from '../lib/totp'; test.describe('step-up (api)', () => { test('TOTP MFA freshens the session step-up status', async ({ actor }) => { - const { token } = await registerAndVerifyEmail(actor.ctx, actor.email); - const auth = { Authorization: `Bearer ${token}` }; + const registered = await registerAndVerifyEmail(actor.ctx, actor.email); + const enrolling = { Authorization: `Bearer ${registered.token}` }; // Enroll TOTP (enrollment alone must not satisfy step-up). - const start = await actor.ctx.post('/totp/enroll/start', { headers: auth, data: {} }); + const start = await actor.ctx.post('/totp/enroll/start', { headers: enrolling, data: {} }); expect(start.ok(), `enroll/start -> ${start.status()}`).toBeTruthy(); const { secret } = await start.json(); const enroll = await actor.ctx.post('/totp/enroll/verify', { - headers: auth, + headers: enrolling, data: { code: totp(secret) }, }); expect(enroll.ok(), `enroll/verify -> ${enroll.status()}`).toBeTruthy(); + // A step-up only accepts a factor enrolled before the session signed in, so the + // TOTP proves the holder of the next session, not the one that added it. + const token = await verifyLoginEmailOtp(actor.ctx, await login(actor.ctx, actor.email)); + const auth = { Authorization: `Bearer ${token}` }; + const before = await actor.ctx.get('/step-up/status', { headers: auth }); expect(before.ok()).toBeTruthy(); expect((await before.json()).fresh, 'step-up is not fresh before MFA').toBe(false); diff --git a/verify/harness/lib/cutoverFlows.ts b/verify/harness/lib/cutoverFlows.ts index 5578ab4..1572309 100644 --- a/verify/harness/lib/cutoverFlows.ts +++ b/verify/harness/lib/cutoverFlows.ts @@ -4,9 +4,9 @@ import { APIRequestContext, expect } from '@playwright/test'; import { MOCK_OIDC_ISSUER } from '../mock-oidc'; import { MOCK_OIDC_PORT, OWNER_EMAIL } from './env'; -import { registerEmail, requestEmailOtp, verifyEmailOtp } from './flows'; +import { ownerAccessToken } from './flows'; import { SoftwareAuthenticator } from './softwareAuthenticator'; -import { registerPasskey, stepUpOwner } from './stepUp'; +import { registerPasskey } from './stepUp'; // Helpers for the migration cutover path (fells-code/seamless-auth-api#337): a legacy // OIDC provider that links imported users by their directory id, prompts them into @@ -18,19 +18,9 @@ export function bearer(token: string) { return { Authorization: `Bearer ${token}` }; } -/** - * An admin access token, stepped up for admin writes. Only OWNER_EMAIL is granted admin at signup, so this signs - * in as the owner; registering an address that already has an account continues - * that account, so it works whether or not the owner spec has run first. - */ +/** An admin access token, stepped up for admin writes. Only OWNER_EMAIL is admin at signup. */ export async function ownerAdminToken(ctx: APIRequestContext): Promise { - const ephemeral = await registerEmail(ctx, OWNER_EMAIL); - const code = await requestEmailOtp(ctx, ephemeral); - const res = await verifyEmailOtp(ctx, ephemeral, code); - expect(res.ok(), `owner sign-in -> ${res.status()} ${await res.text()}`).toBeTruthy(); - const { token } = (await res.json()) as { token: string }; - await stepUpOwner(ctx, token); - return token; + return ownerAccessToken(ctx, OWNER_EMAIL); } /** Adds an OIDC provider that verifies the mock's ID tokens and links on `oid`. */ diff --git a/verify/harness/lib/flows.ts b/verify/harness/lib/flows.ts index 680e2d7..ebe9aec 100644 --- a/verify/harness/lib/flows.ts +++ b/verify/harness/lib/flows.ts @@ -104,6 +104,22 @@ export async function login(ctx: APIRequestContext, identifier: string): Promise return body.token as string; } +/** Send a login email OTP over the ephemeral token and verify it; returns the access token. */ +export async function verifyLoginEmailOtp(ctx: APIRequestContext, ephemeral: string): Promise { + const sent = await ctx.get('/otp/generate-login-email-otp', { + headers: { ...bearer(ephemeral), ...EXTERNAL_DELIVERY }, + }); + expect(sent.ok(), `generate-login-email-otp -> ${sent.status()}`).toBeTruthy(); + const code = (await sent.json())?.delivery?.token; + expect(code, 'external delivery returns the login OTP code').toBeTruthy(); + const res = await ctx.post('/otp/verify-login-email-otp', { + headers: bearer(ephemeral), + data: { verificationToken: String(code) }, + }); + expect(res.ok(), `verify-login-email-otp -> ${res.status()} ${await res.text()}`).toBeTruthy(); + return (await res.json()).token as string; +} + export interface MagicLink { /** Lookup handle from the query string. On its own it signs nobody in. */ token: string; @@ -206,14 +222,19 @@ export async function oauthLogin( return { token: body.token, refreshToken: body.refreshToken, sub: body.sub, email: body.email }; } +export interface OwnerSession { + token: string; + roles: string[]; +} + /** - * An access token for the tenant owner (OWNER_EMAIL, admin at signup), stepped up - * for the admin API. Signs the owner in when the account exists (the api layer - * usually registered it earlier in the run) and registers it otherwise. An unknown - * identifier gets a decoy OTP send that delivers nothing, which is how the two - * cases are told apart. + * Signs the tenant owner (OWNER_EMAIL, admin at signup) in, registering the account the + * first time. Registering an address that already has a verified account is answered as + * an unclaimed sign-up and sends nothing, so an existing owner goes through /login. An + * unknown identifier gets a decoy login OTP send that delivers nothing, which is how the + * two cases are told apart. */ -export async function ownerAccessToken(ctx: APIRequestContext, ownerEmail: string): Promise { +export async function ownerSession(ctx: APIRequestContext, ownerEmail: string): Promise { const ephemeral = await login(ctx, ownerEmail); const sent = await ctx.get('/otp/generate-login-email-otp', { headers: { ...bearer(ephemeral), ...EXTERNAL_DELIVERY }, @@ -227,14 +248,22 @@ export async function ownerAccessToken(ctx: APIRequestContext, ownerEmail: strin data: { verificationToken: String(code) }, }); expect(res.ok(), `verify-login-email-otp -> ${res.status()} ${await res.text()}`).toBeTruthy(); - const { token } = (await res.json()) as { token: string }; - await stepUpOwner(ctx, token); - return token; + const body = await res.json(); + return { token: body.token, roles: body.roles ?? [] }; } - const { token } = await registerAndVerifyEmail(ctx, ownerEmail); - await stepUpOwner(ctx, token); - return token; + const registered = await registerEmail(ctx, ownerEmail); + const registeredCode = await requestEmailOtp(ctx, registered); + const res = await verifyEmailOtp(ctx, registered, registeredCode); + expect(res.ok(), `verify-email-otp -> ${res.status()} ${await res.text()}`).toBeTruthy(); + const body = await res.json(); + return { token: body.token, roles: body.roles ?? [] }; +} + +/** An access token for the tenant owner, stepped up for the admin API. */ +export async function ownerAccessToken(ctx: APIRequestContext, ownerEmail: string): Promise { + const signIn = async () => (await ownerSession(ctx, ownerEmail)).token; + return stepUpOwner(ctx, await signIn(), signIn); } /** Replace a user's roles through the admin API. */ diff --git a/verify/harness/lib/stepUp.ts b/verify/harness/lib/stepUp.ts index 1fc3a14..23342ee 100644 --- a/verify/harness/lib/stepUp.ts +++ b/verify/harness/lib/stepUp.ts @@ -44,12 +44,18 @@ export async function registerPasskey( /** * Freshens the owner session's step-up, which admin writes require. Uses one passkey - * derived from OWNER_EMAIL, enrolled the first time it is needed. + * derived from OWNER_EMAIL, enrolled the first time it is needed. A step-up only accepts + * a factor enrolled before the session signed in, so enrolling it calls `signIn` for a + * new session and steps that one up. Returns the access token that was stepped up. */ -export async function stepUpOwner(ctx: APIRequestContext, accessToken: string): Promise { - const begin = async () => { +export async function stepUpOwner( + ctx: APIRequestContext, + accessToken: string, + signIn: () => Promise, +): Promise { + const begin = async (token: string) => { const res = await ctx.post('/step-up/webauthn/start', { - headers: bearer(accessToken), + headers: bearer(token), data: {}, }); if (res.status() === 401) return null; @@ -61,15 +67,17 @@ export async function stepUpOwner(ctx: APIRequestContext, accessToken: string): return enrolled ? options : null; }; - let options = await begin(); + let token = accessToken; + let options = await begin(token); if (!options) { - await registerPasskey(ctx, accessToken, ownerAuthenticator); - options = await begin(); + await registerPasskey(ctx, token, ownerAuthenticator); + token = await signIn(); + options = await begin(token); } expect(options, 'step-up offers the owner passkey').toBeTruthy(); const finish = await ctx.post('/step-up/webauthn/finish', { - headers: bearer(accessToken), + headers: bearer(token), data: { assertionResponse: ownerAuthenticator.authenticate({ challenge: options.challenge, @@ -79,4 +87,5 @@ export async function stepUpOwner(ctx: APIRequestContext, accessToken: string): }, }); expect(finish.ok(), `step-up finish -> ${finish.status()} ${await finish.text()}`).toBeTruthy(); + return token; }