Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core,schemas): check email verification status in me api #6507

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
32 changes: 16 additions & 16 deletions packages/console/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,23 +116,23 @@ function Providers() {
<AppThemeProvider>
<Helmet titleTemplate={`%s - ${mainTitle}`} defaultTitle={mainTitle} />
<Toast />
<ErrorBoundary>
<LogtoErrorBoundary>
{/**
* If it's not Cloud (OSS), render the tenant app container directly since only default tenant is available;
* if it's Cloud, render the tenant app container only when a tenant ID is available (in a tenant context).
*/}
{!isCloud || currentTenantId ? (
<AppDataProvider>
<AppConfirmModalProvider>
<AppConfirmModalProvider>
<ErrorBoundary>
<LogtoErrorBoundary>
{/**
* If it's not Cloud (OSS), render the tenant app container directly since only default tenant is available;
* if it's Cloud, render the tenant app container only when a tenant ID is available (in a tenant context).
*/}
{!isCloud || currentTenantId ? (
<AppDataProvider>
<AppRoutes />
</AppConfirmModalProvider>
</AppDataProvider>
) : (
<CloudAppRoutes />
)}
</LogtoErrorBoundary>
</ErrorBoundary>
</AppDataProvider>
) : (
<CloudAppRoutes />
)}
</LogtoErrorBoundary>
</ErrorBoundary>
</AppConfirmModalProvider>
</AppThemeProvider>
</LogtoProvider>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function VerificationCodeModal() {
}

try {
await api.post(`me/verification-codes/verify`, { json: { verificationCode, email, action } });
await api.post(`me/verification-codes/verify`, { json: { verificationCode, email } });

if (action === 'changeEmail') {
await api.patch('me', { json: { primaryEmail: email } });
Expand Down
19 changes: 16 additions & 3 deletions packages/core/src/libraries/verification-status.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { generateStandardId } from '@logto/shared';
import { type Nullable } from '@silverhand/essentials';

import RequestError from '#src/errors/RequestError/index.js';
import type Queries from '#src/tenants/Queries.js';
Expand All @@ -13,25 +14,37 @@
deleteVerificationStatusesByUserId,
} = queries.verificationStatuses;

const createVerificationStatus = async (userId: string) => {
const createVerificationStatus = async (userId: string, identifier: Nullable<string>) => {
// Remove existing verification statuses for current user.
await deleteVerificationStatusesByUserId(userId);

return insertVerificationStatus({
id: generateStandardId(),
userId,
verifiedIdentifier: identifier,

Check warning on line 24 in packages/core/src/libraries/verification-status.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/libraries/verification-status.ts#L24

Added line #L24 was not covered by tests
});
};

const checkVerificationStatus = async (userId: string): Promise<void> => {
const checkVerificationStatus = async (
userId: string,
identifier: Nullable<string>
): Promise<void> => {

Check warning on line 31 in packages/core/src/libraries/verification-status.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/libraries/verification-status.ts#L29-L31

Added lines #L29 - L31 were not covered by tests
const verificationStatus = await findVerificationStatusByUserId(userId);

assertThat(verificationStatus, 'session.verification_session_not_found');

// The user verification status is considered valid if the user is verified within 10 minutes.
const isValid = Date.now() - verificationStatus.createdAt < verificationTimeout;
assertThat(isValid, new RequestError({ code: 'session.verification_failed', status: 422 }));

assertThat(
verificationStatus.verifiedIdentifier === identifier,
new RequestError({ code: 'session.verification_failed', status: 422 })
);

Check warning on line 43 in packages/core/src/libraries/verification-status.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/libraries/verification-status.ts#L39-L43

Added lines #L39 - L43 were not covered by tests
};

return { createVerificationStatus, checkVerificationStatus };
return {
createVerificationStatus,
checkVerificationStatus,
};
};
3 changes: 1 addition & 2 deletions packages/core/src/queries/verification-status.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { VerificationStatus } from '@logto/schemas';
import { VerificationStatuses } from '@logto/schemas';
import { type VerificationStatus, VerificationStatuses } from '@logto/schemas';
import type { CommonQueryMethods } from '@silverhand/slonik';
import { sql } from '@silverhand/slonik';

Expand Down
18 changes: 11 additions & 7 deletions packages/core/src/routes-me/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
'/',
koaGuard({
body: object({
username: string().regex(usernameRegEx),
primaryEmail: string().regex(emailRegEx),
username: string().regex(usernameRegEx), // OSS only
primaryEmail: string().regex(emailRegEx), // Cloud only
name: string().or(literal('')).nullable(),
avatar: string().url().or(literal('')).nullable(),
}).partial(),
Expand All @@ -57,6 +57,12 @@
const user = await findUserById(userId);
assertThat(!user.isSuspended, new RequestError({ code: 'user.suspended', status: 401 }));

const { primaryEmail } = body;
if (primaryEmail) {
// Check if user has verified email within 10 minutes.
await checkVerificationStatus(userId, primaryEmail);
}

Check warning on line 65 in packages/core/src/routes-me/user.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/routes-me/user.ts#L60-L65

Added lines #L60 - L65 were not covered by tests
await checkIdentifierCollision(body, userId);

const updatedUser = await updateUserById(userId, body);
Expand Down Expand Up @@ -113,7 +119,7 @@

await verifyUserPassword(user, password);

await createVerificationStatus(userId);
await createVerificationStatus(userId, null);

Check warning on line 122 in packages/core/src/routes-me/user.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/routes-me/user.ts#L122

Added line #L122 was not covered by tests

ctx.status = 204;

Expand All @@ -128,13 +134,11 @@
const { id: userId } = ctx.auth;
const { password } = ctx.guard.body;

const { isSuspended, passwordEncrypted: oldPasswordEncrypted } = await findUserById(userId);
const { isSuspended } = await findUserById(userId);

Check warning on line 137 in packages/core/src/routes-me/user.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/routes-me/user.ts#L137

Added line #L137 was not covered by tests

assertThat(!isSuspended, new RequestError({ code: 'user.suspended', status: 401 }));

if (oldPasswordEncrypted) {
await checkVerificationStatus(userId);
}
await checkVerificationStatus(userId, null);

Check warning on line 141 in packages/core/src/routes-me/user.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/routes-me/user.ts#L141

Added line #L141 was not covered by tests

const { passwordEncrypted, passwordEncryptionMethod } = await encryptUserPassword(password);
await updateUserById(userId, { passwordEncrypted, passwordEncryptionMethod });
Expand Down
22 changes: 10 additions & 12 deletions packages/core/src/routes-me/verification-code.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { TemplateType } from '@logto/connector-kit';
import { emailRegEx } from '@logto/core-kit';
import { literal, object, string, union } from 'zod';
import { object, string } from 'zod';

import RequestError from '#src/errors/RequestError/index.js';
import koaGuard from '#src/middleware/koa-guard.js';
import type { RouterInitArgs } from '#src/routes/types.js';
import assertThat from '#src/utils/assert-that.js';

import RequestError from '../errors/RequestError/index.js';
import assertThat from '../utils/assert-that.js';

import type { AuthedMeRouter } from './types.js';

Expand Down Expand Up @@ -44,21 +45,18 @@
body: object({
email: string().regex(emailRegEx),
verificationCode: string().min(1),
action: union([literal('changeEmail'), literal('changePassword')]),
}),
}),
async (ctx, next) => {
const { id: userId } = ctx.auth;
const { verificationCode, action, ...identifier } = ctx.guard.body;
await verifyPasscode(undefined, codeType, verificationCode, identifier);
const { verificationCode, ...identifier } = ctx.guard.body;

Check warning on line 52 in packages/core/src/routes-me/verification-code.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/routes-me/verification-code.ts#L52

Added line #L52 was not covered by tests

if (action === 'changePassword') {
// Store password verification status
const user = await findUserById(userId);
assertThat(!user.isSuspended, new RequestError({ code: 'user.suspended', status: 401 }));
const user = await findUserById(userId);
assertThat(!user.isSuspended, new RequestError({ code: 'user.suspended', status: 401 }));

await verifyPasscode(undefined, codeType, verificationCode, identifier);

Check warning on line 57 in packages/core/src/routes-me/verification-code.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/routes-me/verification-code.ts#L54-L57

Added lines #L54 - L57 were not covered by tests

await createVerificationStatus(userId);
}
await createVerificationStatus(userId, identifier.email);

Check warning on line 59 in packages/core/src/routes-me/verification-code.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/routes-me/verification-code.ts#L59

Added line #L59 was not covered by tests

ctx.status = 204;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { sql } from '@silverhand/slonik';

import type { AlterationScript } from '../lib/types/alteration.js';

const alteration: AlterationScript = {
up: async (pool) => {
await pool.query(sql`
alter table verification_statuses add column verified_identifier varchar(255);
`);
},
down: async (pool) => {
await pool.query(sql`
alter table verification_statuses drop column verified_identifier;
`);
},
};

export default alteration;
1 change: 1 addition & 0 deletions packages/schemas/tables/verification_statuses.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ create table verification_statuses (
user_id varchar(21) not null
references users (id) on update cascade on delete cascade,
created_at timestamptz not null default(now()),
verified_identifier varchar(255),
primary key (id)
);

Expand Down
Loading