Skip to content

Commit

Permalink
Merge pull request #125 from conceptadev/feature/export-verify-token-…
Browse files Browse the repository at this point in the history
…helpers

feat: export create verify token helpers
  • Loading branch information
MrMaz authored Sep 29, 2023
2 parents 4dd5a48 + 26330c2 commit 448ddd9
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 40 deletions.
9 changes: 6 additions & 3 deletions packages/nestjs-auth-jwt/src/auth-jwt.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<JwtStrategy>(
Expand All @@ -33,7 +36,7 @@ export class AuthJwtStrategy extends PassportStrategyFactory<JwtStrategy>(
private userLookupService: AuthJwtUserLookupServiceInterface,
) {
const options: Partial<JwtStrategyOptionsInterface> = {
verifyToken: createVerifyTokenCallback(verifyTokenService),
verifyToken: createVerifyAccessTokenCallback(verifyTokenService),
...settings,
};

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ describe(AuthRefreshStrategy, () => {
let authorizationPayloadInterface: AuthorizationPayloadInterface;

beforeEach(async () => {
// TODO: configure JWT module to use different access and refresh secrets

settings = mock<Partial<AuthRefreshSettingsInterface>>();

userLookUpService = mock<AuthRefreshUserLookupServiceInterface>();
Expand Down
8 changes: 5 additions & 3 deletions packages/nestjs-auth-refresh/src/auth-refresh.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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<JwtStrategy>(
Expand All @@ -32,7 +34,7 @@ export class AuthRefreshStrategy extends PassportStrategyFactory<JwtStrategy>(
private userLookupService: AuthRefreshUserLookupServiceInterface,
) {
const options: Partial<AuthRefreshSettingsInterface> = {
verifyToken: createVerifyTokenCallback(verifyTokenService),
verifyToken: createVerifyRefreshTokenCallback(verifyTokenService),
...settings,
};

Expand Down

This file was deleted.

7 changes: 7 additions & 0 deletions packages/nestjs-jwt/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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';
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { StrategyOptions } from 'passport-jwt';
import { JwtVerifyTokenCallback } from '../jwt.types';

export interface JwtStrategyOptionsInterface
extends Pick<StrategyOptions, 'jwtFromRequest'> {
verifyToken: (
token: string,
done: (err?: Error, decodedToken?: unknown) => void,
) => void;
verifyToken: JwtVerifyTokenCallback;
}
2 changes: 2 additions & 0 deletions packages/nestjs-jwt/src/jwt.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
8 changes: 8 additions & 0 deletions packages/nestjs-jwt/src/jwt.types.ts
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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));
};
};
Original file line number Diff line number Diff line change
@@ -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));
};
};

0 comments on commit 448ddd9

Please sign in to comment.