-
-
Notifications
You must be signed in to change notification settings - Fork 461
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core,schemas): add denyAccess api context to custom jwt
add denyAccess api context to custom jwt
- Loading branch information
Showing
7 changed files
with
175 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,18 @@ | ||
import { | ||
type CustomJwtErrorBody, | ||
CustomJwtErrorCode, | ||
LogtoJwtTokenKeyType, | ||
jwtCustomizerUserContextGuard, | ||
userInfoSelectFields, | ||
type CustomJwtFetcher, | ||
type JwtCustomizerType, | ||
type JwtCustomizerUserContext, | ||
type LogtoJwtTokenKey, | ||
type JwtCustomizerApiContext, | ||
type CustomJwtScriptPayload, | ||
} from '@logto/schemas'; | ||
import { type ConsoleLog } from '@logto/shared'; | ||
import { assert, deduplicate, pick, pickState } from '@silverhand/essentials'; | ||
import { assert, conditional, deduplicate, pick, pickState } from '@silverhand/essentials'; | ||
import deepmerge from 'deepmerge'; | ||
import { ZodError, z } from 'zod'; | ||
|
||
|
@@ -28,19 +32,43 @@ import { | |
|
||
import { type CloudConnectionLibrary } from './cloud-connection.js'; | ||
|
||
const apiContext: JwtCustomizerApiContext = Object.freeze({ | ||
denyAccess: (message?: string) => { | ||
const error: CustomJwtErrorBody = { | ||
code: CustomJwtErrorCode.AccessDenied, | ||
message: message ?? 'Access denied', | ||
}; | ||
|
||
throw new LocalVmError(error, 403); | ||
}, | ||
}); | ||
|
||
export class JwtCustomizerLibrary { | ||
// Convert errors to WithTyped client response error to share the error handling logic. | ||
static async runScriptInLocalVm(data: CustomJwtFetcher) { | ||
try { | ||
const payload = | ||
data.tokenType === LogtoJwtTokenKeyType.AccessToken | ||
// @ts-expect-error -- remove this when the dev feature is ready | ||
const payload: CustomJwtScriptPayload = { | ||
...(data.tokenType === LogtoJwtTokenKeyType.AccessToken | ||
? pick(data, 'token', 'context', 'environmentVariables') | ||
: pick(data, 'token', 'environmentVariables'); | ||
: pick(data, 'token', 'environmentVariables')), | ||
...conditional( | ||
// TODO: @simeng remove this when the dev feature is ready | ||
Check warning on line 56 in packages/core/src/libraries/jwt-customizer.ts GitHub Actions / ESLint Report Analysispackages/core/src/libraries/jwt-customizer.ts#L56
|
||
EnvSet.values.isDevFeaturesEnabled && { | ||
api: apiContext, | ||
} | ||
), | ||
}; | ||
|
||
const result = await runScriptFunctionInLocalVm(data.script, 'getCustomJwtClaims', payload); | ||
|
||
// If the `result` is not a record, we cannot merge it to the existing token payload. | ||
return z.record(z.unknown()).parse(result); | ||
} catch (error: unknown) { | ||
if (error instanceof LocalVmError) { | ||
throw error; | ||
} | ||
|
||
// Assuming we only use zod for request body validation | ||
if (error instanceof ZodError) { | ||
const { errors } = error; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters