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

feat(core): add hasPassword field to custom JWT user context #6096

Merged
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
7 changes: 7 additions & 0 deletions .changeset/sharp-cooks-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@logto/console": minor
"@logto/schemas": minor
"@logto/core": minor
---

add `hasPassword` to custom JWT user context
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export const defaultClientCredentialsPayload: ClientCredentialsPayload = {

const defaultUserContext: Partial<JwtCustomizerUserContext> = {
id: '123',
hasPassword: false,
username: 'foo',
primaryEmail: '[email protected]',
primaryPhone: '+1234567890',
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/libraries/jwt-customizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
}
}

constructor(

Check warning on line 62 in packages/core/src/libraries/jwt-customizer.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/core/src/libraries/jwt-customizer.ts#L62

[max-params] Constructor has too many parameters (5). Maximum allowed is 4.
private readonly queries: Queries,
private readonly logtoConfigs: LogtoConfigLibrary,
private readonly cloudConnection: CloudConnectionLibrary,
Expand Down Expand Up @@ -87,6 +87,7 @@
await this.queries.organizations.relations.users.getOrganizationsByUserId(userId);
const userContext = {
...pick(user, ...userInfoSelectFields),
hasPassword: Boolean(user.passwordEncrypted),

Check warning on line 90 in packages/core/src/libraries/jwt-customizer.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/libraries/jwt-customizer.ts#L90

Added line #L90 was not covered by tests
ssoIdentities: fullSsoIdentities.map(pickState('issuer', 'identityId', 'detail')),
mfaVerificationFactors: deduplicate(user.mfaVerifications.map(({ type }) => type)),
roles: roles.map((role) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/integration-tests/src/__mocks__/jwt-customizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ export const accessTokenJwtCustomizerPayload = {
};

export const accessTokenSampleScript = `const getCustomJwtClaims = async ({ token, context, environmentVariables }) => {
return { user_id: context?.user?.id ?? 'unknown' };
}`;
return { user_id: context?.user?.id ?? 'unknown', hasPassword: context?.user?.hasPassword };
};`;

export const clientCredentialsSampleScript = `const getCustomJwtClaims = async ({ token, context, environmentVariables }) => {
return { ...environmentVariables };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ describe('get access token', () => {
testApiScopeNames.join(' ')
);
expect(getAccessTokenPayload(accessToken)).toHaveProperty('user_id', guestUserId);
// The guest user has password.
expect(getAccessTokenPayload(accessToken)).toHaveProperty('hasPassword', true);

await deleteJwtCustomizer('access-token');
});
Expand Down
39 changes: 31 additions & 8 deletions packages/schemas/src/types/logto-config/jwt-customizer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { jsonObjectGuard } from '@logto/connector-kit';
import { z } from 'zod';
import { type ZodType, z } from 'zod';

import { Organizations, Roles, UserSsoIdentities } from '../../db-entries/index.js';
import { mfaFactorsGuard } from '../../foundations/index.js';
import { scopeResponseGuard } from '../scope.js';
import { userInfoGuard } from '../user.js';
import {
Organizations,
type Organization,
type Role,
Roles,
UserSsoIdentities,
type UserSsoIdentity,
} from '../../db-entries/index.js';
import { mfaFactorsGuard, type MfaFactors } from '../../foundations/index.js';
import { scopeResponseGuard, type ScopeResponse } from '../scope.js';
import { userInfoGuard, type UserInfo } from '../user.js';

import { accessTokenPayloadGuard, clientCredentialsPayloadGuard } from './oidc-provider.js';

Expand All @@ -19,7 +26,25 @@ export enum LogtoJwtTokenKeyType {
ClientCredentials = 'client-credentials',
}

export type JwtCustomizerUserContext = UserInfo & {
simeng-li marked this conversation as resolved.
Show resolved Hide resolved
hasPassword: boolean;
ssoIdentities: Array<Pick<UserSsoIdentity, 'issuer' | 'identityId' | 'detail'>>;
mfaVerificationFactors: MfaFactors;
roles: Array<
Pick<Role, 'id' | 'name' | 'description'> & {
scopes: Array<Pick<ScopeResponse, 'id' | 'name' | 'description' | 'resourceId' | 'resource'>>;
}
>;
organizations: Array<Pick<Organization, 'id' | 'name' | 'description'>>;
organizationRoles: Array<{
organizationId: string;
roleId: string;
roleName: string;
}>;
};

export const jwtCustomizerUserContextGuard = userInfoGuard.extend({
hasPassword: z.boolean(),
ssoIdentities: UserSsoIdentities.guard
.pick({ issuer: true, identityId: true, detail: true })
.array(),
Expand All @@ -40,9 +65,7 @@ export const jwtCustomizerUserContextGuard = userInfoGuard.extend({
roleName: z.string(),
})
.array(),
});

export type JwtCustomizerUserContext = z.infer<typeof jwtCustomizerUserContextGuard>;
}) satisfies ZodType<JwtCustomizerUserContext>;

export const accessTokenJwtCustomizerGuard = jwtCustomizerGuard
.extend({
Expand Down
Loading