Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/types-0-41.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"seamless-cli": patch
---

Depends on `@seamless-auth/types` 0.41.0.
5 changes: 5 additions & 0 deletions .changeset/verify-step-up-after-sign-in.md
Original file line number Diff line number Diff line change
@@ -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.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
23 changes: 8 additions & 15 deletions verify/harness/api/ownerAdmin.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
15 changes: 10 additions & 5 deletions verify/harness/api/stepUp.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
18 changes: 4 additions & 14 deletions verify/harness/lib/cutoverFlows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<string> {
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`. */
Expand Down
53 changes: 41 additions & 12 deletions verify/harness/lib/flows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
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;
Expand Down Expand Up @@ -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<string> {
export async function ownerSession(ctx: APIRequestContext, ownerEmail: string): Promise<OwnerSession> {
const ephemeral = await login(ctx, ownerEmail);
const sent = await ctx.get('/otp/generate-login-email-otp', {
headers: { ...bearer(ephemeral), ...EXTERNAL_DELIVERY },
Expand All @@ -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<string> {
const signIn = async () => (await ownerSession(ctx, ownerEmail)).token;
return stepUpOwner(ctx, await signIn(), signIn);
}

/** Replace a user's roles through the admin API. */
Expand Down
25 changes: 17 additions & 8 deletions verify/harness/lib/stepUp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
const begin = async () => {
export async function stepUpOwner(
ctx: APIRequestContext,
accessToken: string,
signIn: () => Promise<string>,
): Promise<string> {
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;
Expand All @@ -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,
Expand All @@ -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;
}