Skip to content

Commit

Permalink
feat(core): add hasPassword field to custom JWT user context
Browse files Browse the repository at this point in the history
  • Loading branch information
darcyYe committed Jun 24, 2024
1 parent a43434c commit 0423c00
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
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: 'foo@logto.io',
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 @@ -87,6 +87,7 @@ export class JwtCustomizerLibrary {
await this.queries.organizations.relations.users.getOrganizationsByUserId(userId);
const userContext = {
...pick(user, ...userInfoSelectFields),
hasPassword: Boolean(user.passwordEncrypted),
ssoIdentities: fullSsoIdentities.map(pickState('issuer', 'identityId', 'detail')),
mfaVerificationFactors: deduplicate(user.mfaVerifications.map(({ type }) => type)),
roles: roles.map((role) => {
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 & {
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

0 comments on commit 0423c00

Please sign in to comment.