diff --git a/packages/nestjs-auth-jwt/src/auth-jwt.strategy.ts b/packages/nestjs-auth-jwt/src/auth-jwt.strategy.ts index 483ea28ee..e5b2812d2 100644 --- a/packages/nestjs-auth-jwt/src/auth-jwt.strategy.ts +++ b/packages/nestjs-auth-jwt/src/auth-jwt.strategy.ts @@ -5,7 +5,11 @@ import { PassportStrategyFactory, VerifyTokenServiceInterface, } from '@concepta/nestjs-authentication'; -import { JwtStrategy, JwtStrategyOptionsInterface } from '@concepta/nestjs-jwt'; +import { + createVerifyAccessTokenCallback, + JwtStrategy, + JwtStrategyOptionsInterface, +} from '@concepta/nestjs-jwt'; import { QueryOptionsInterface } from '@concepta/typeorm-common'; import { @@ -17,7 +21,6 @@ import { import { AuthJwtSettingsInterface } from './interfaces/auth-jwt-settings.interface'; import { AuthJwtUserLookupServiceInterface } from './interfaces/auth-jwt-user-lookup-service.interface'; -import { createVerifyTokenCallback } from './utils/create-verify-token-callback.util'; @Injectable() export class AuthJwtStrategy extends PassportStrategyFactory( @@ -33,7 +36,7 @@ export class AuthJwtStrategy extends PassportStrategyFactory( private userLookupService: AuthJwtUserLookupServiceInterface, ) { const options: Partial = { - verifyToken: createVerifyTokenCallback(verifyTokenService), + verifyToken: createVerifyAccessTokenCallback(verifyTokenService), ...settings, }; diff --git a/packages/nestjs-auth-jwt/src/utils/create-verify-token-callback.util.ts b/packages/nestjs-auth-jwt/src/utils/create-verify-token-callback.util.ts deleted file mode 100644 index 0bdbbe65a..000000000 --- a/packages/nestjs-auth-jwt/src/utils/create-verify-token-callback.util.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { VerifyTokenServiceInterface } from '@concepta/nestjs-authentication'; - -export function createVerifyTokenCallback( - verifyTokenService: VerifyTokenServiceInterface, -) { - return ( - token: string, - done: (error?: Error, decodedToken?: unknown) => void, - ): void => { - verifyTokenService - .accessToken(token) - .then((decodedToken: unknown) => done(undefined, decodedToken)) - .catch((error) => done(error)); - }; -} diff --git a/packages/nestjs-auth-refresh/src/auth-refresh.strategy.spec.ts b/packages/nestjs-auth-refresh/src/auth-refresh.strategy.spec.ts index 756563cf9..bd401cae5 100644 --- a/packages/nestjs-auth-refresh/src/auth-refresh.strategy.spec.ts +++ b/packages/nestjs-auth-refresh/src/auth-refresh.strategy.spec.ts @@ -19,6 +19,8 @@ describe(AuthRefreshStrategy, () => { let authorizationPayloadInterface: AuthorizationPayloadInterface; beforeEach(async () => { + // TODO: configure JWT module to use different access and refresh secrets + settings = mock>(); userLookUpService = mock(); diff --git a/packages/nestjs-auth-refresh/src/auth-refresh.strategy.ts b/packages/nestjs-auth-refresh/src/auth-refresh.strategy.ts index eecdd83ed..e01cd0a50 100644 --- a/packages/nestjs-auth-refresh/src/auth-refresh.strategy.ts +++ b/packages/nestjs-auth-refresh/src/auth-refresh.strategy.ts @@ -3,7 +3,10 @@ import { PassportStrategyFactory, VerifyTokenServiceInterface, } from '@concepta/nestjs-authentication'; -import { JwtStrategy } from '@concepta/nestjs-jwt'; +import { + createVerifyRefreshTokenCallback, + JwtStrategy, +} from '@concepta/nestjs-jwt'; import { AuthorizationPayloadInterface } from '@concepta/ts-common'; import { QueryOptionsInterface } from '@concepta/typeorm-common'; @@ -16,7 +19,6 @@ import { import { AuthRefreshSettingsInterface } from './interfaces/auth-refresh-settings.interface'; import { AuthRefreshUserLookupServiceInterface } from './interfaces/auth-refresh-user-lookup-service.interface'; -import { createVerifyTokenCallback } from './utils/create-verify-token-callback.util'; @Injectable() export class AuthRefreshStrategy extends PassportStrategyFactory( @@ -32,7 +34,7 @@ export class AuthRefreshStrategy extends PassportStrategyFactory( private userLookupService: AuthRefreshUserLookupServiceInterface, ) { const options: Partial = { - verifyToken: createVerifyTokenCallback(verifyTokenService), + verifyToken: createVerifyRefreshTokenCallback(verifyTokenService), ...settings, }; diff --git a/packages/nestjs-auth-refresh/src/utils/create-verify-token-callback.util.ts b/packages/nestjs-auth-refresh/src/utils/create-verify-token-callback.util.ts deleted file mode 100644 index 47543b187..000000000 --- a/packages/nestjs-auth-refresh/src/utils/create-verify-token-callback.util.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { VerifyTokenServiceInterface } from '@concepta/nestjs-authentication'; - -export function createVerifyTokenCallback( - verifyTokenService: VerifyTokenServiceInterface, -) { - return ( - token: string, - done: (error?: Error, decodedToken?: unknown) => void, - ): void => { - verifyTokenService - .accessToken(token) - .then((decodedToken) => done(undefined, decodedToken)) - .catch((error) => done(error)); - }; -} diff --git a/packages/nestjs-jwt/src/index.ts b/packages/nestjs-jwt/src/index.ts index fdce965ba..f7c030135 100644 --- a/packages/nestjs-jwt/src/index.ts +++ b/packages/nestjs-jwt/src/index.ts @@ -1,3 +1,6 @@ +// types +export { JwtTokenType, JwtVerifyTokenCallback } from './jwt.types'; + // interfaces export { JwtStrategyOptionsInterface } from './interfaces/jwt-strategy-options.interface'; export { JwtIssueServiceInterface } from './interfaces/jwt-issue-service.interface'; @@ -12,3 +15,7 @@ export { JwtVerifyService } from './services/jwt-verify.service'; // strategy exports export { ExtractJwt, JwtFromRequestFunction } from 'passport-jwt'; export { JwtStrategy } from './jwt.strategy'; + +// utils +export { createVerifyAccessTokenCallback } from './utils/create-verify-access-token-callback.util'; +export { createVerifyRefreshTokenCallback } from './utils/create-verify-refresh-token-callback.util'; diff --git a/packages/nestjs-jwt/src/interfaces/jwt-strategy-options.interface.ts b/packages/nestjs-jwt/src/interfaces/jwt-strategy-options.interface.ts index d9a921600..0f8ed81c5 100644 --- a/packages/nestjs-jwt/src/interfaces/jwt-strategy-options.interface.ts +++ b/packages/nestjs-jwt/src/interfaces/jwt-strategy-options.interface.ts @@ -1,9 +1,7 @@ import { StrategyOptions } from 'passport-jwt'; +import { JwtVerifyTokenCallback } from '../jwt.types'; export interface JwtStrategyOptionsInterface extends Pick { - verifyToken: ( - token: string, - done: (err?: Error, decodedToken?: unknown) => void, - ) => void; + verifyToken: JwtVerifyTokenCallback; } diff --git a/packages/nestjs-jwt/src/jwt.strategy.ts b/packages/nestjs-jwt/src/jwt.strategy.ts index a83d91506..fb92d347d 100644 --- a/packages/nestjs-jwt/src/jwt.strategy.ts +++ b/packages/nestjs-jwt/src/jwt.strategy.ts @@ -35,6 +35,8 @@ export class JwtStrategy extends PassportStrategy { } private verifyTokenCallback(e?: Error, decodedToken?: unknown) { + // TODO: configure JWT module to use different access and refresh secrets + if (e) { return this.error(e); } diff --git a/packages/nestjs-jwt/src/jwt.types.ts b/packages/nestjs-jwt/src/jwt.types.ts index df1085924..08bb0081b 100644 --- a/packages/nestjs-jwt/src/jwt.types.ts +++ b/packages/nestjs-jwt/src/jwt.types.ts @@ -1 +1,9 @@ export type JwtTokenType = 'access' | 'refresh'; + +export type JwtVerifyTokenCallback< + ErrorType extends Error = Error, + DecodedTokenType extends unknown = unknown, +> = ( + token: string, + done: (err?: ErrorType, decodedToken?: DecodedTokenType) => void, +) => void; diff --git a/packages/nestjs-jwt/src/utils/create-verify-access-token-callback.util.ts b/packages/nestjs-jwt/src/utils/create-verify-access-token-callback.util.ts new file mode 100644 index 000000000..2b5531686 --- /dev/null +++ b/packages/nestjs-jwt/src/utils/create-verify-access-token-callback.util.ts @@ -0,0 +1,16 @@ +import { JwtVerifyServiceInterface } from '../interfaces/jwt-verify-service.interface'; +import { JwtVerifyTokenCallback } from '../jwt.types'; + +export const createVerifyAccessTokenCallback = ( + verifyTokenService: JwtVerifyServiceInterface, +): JwtVerifyTokenCallback => { + return ( + token: string, + done: (error?: Error, decodedToken?: unknown) => void, + ): void => { + verifyTokenService + .accessToken(token) + .then((decodedToken: unknown) => done(undefined, decodedToken)) + .catch((error) => done(error)); + }; +}; diff --git a/packages/nestjs-jwt/src/utils/create-verify-refresh-token-callback.util.ts b/packages/nestjs-jwt/src/utils/create-verify-refresh-token-callback.util.ts new file mode 100644 index 000000000..8d0381d71 --- /dev/null +++ b/packages/nestjs-jwt/src/utils/create-verify-refresh-token-callback.util.ts @@ -0,0 +1,16 @@ +import { JwtVerifyServiceInterface } from '../interfaces/jwt-verify-service.interface'; +import { JwtVerifyTokenCallback } from '../jwt.types'; + +export const createVerifyRefreshTokenCallback = ( + verifyTokenService: JwtVerifyServiceInterface, +): JwtVerifyTokenCallback => { + return ( + token: string, + done: (error?: Error, decodedToken?: unknown) => void, + ): void => { + verifyTokenService + .refreshToken(token) + .then((decodedToken: unknown) => done(undefined, decodedToken)) + .catch((error) => done(error)); + }; +};