MCPcopy Create free account
hub / github.com/effect-app/libs / verifyToken

Function verifyToken

packages/infra/src/internal/auth.ts:202–231  ·  view source on GitHub ↗
(resolveConfig: Effect.Effect<ResolvedConfig, InvalidRequestError | InvalidTokenError>)

Source from the content-addressed store, hash-verified

200
201const verifyToken =
202 (resolveConfig: Effect.Effect<ResolvedConfig, InvalidRequestError | InvalidTokenError>) => (token: string) =>
203 resolveConfig.pipe(
204 Effect.flatMap((config) => {
205 const options = {
206 clockTolerance: config.clockTolerance,
207 ...(config.tokenSigningAlg ? { algorithms: [config.tokenSigningAlg] } : {}),
208 ...(config.audience !== undefined ? { audience: config.audience } : {}),
209 ...(config.issuer !== undefined ? { issuer: config.issuer } : {}),
210 ...(config.maxTokenAge !== undefined ? { maxTokenAge: config.maxTokenAge } : {})
211 }
212 const verified = config.keyType === "jwks"
213 ? Effect.tryPromise({
214 try: () => jwtVerify(token, config.key, options).then(({ protectedHeader }) => ({ protectedHeader })),
215 catch: (error) => new InvalidTokenError(getErrorMessage(error))
216 })
217 : Effect.tryPromise({
218 try: () => jwtVerify(token, config.key, options).then(({ protectedHeader }) => ({ protectedHeader })),
219 catch: (error) => new InvalidTokenError(getErrorMessage(error))
220 })
221
222 return verified.pipe(
223 Effect.flatMap(({ protectedHeader }) => {
224 const typ = protectedHeader.typ?.toLowerCase().replace(/^application\//, "")
225 return config.strict && typ !== "at+jwt"
226 ? Effect.fail(new InvalidTokenError("Unexpected 'typ' value"))
227 : Effect.void
228 })
229 )
230 })
231 )
232
233export const checkJWTI = (config: Config) => {
234 const resolveConfig = makeResolveConfig(config)

Callers 1

checkJWTIFunction · 0.85

Calls 4

getErrorMessageFunction · 0.85
pipeMethod · 0.65
replaceMethod · 0.45
failMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…