Skip to content

Commit

Permalink
fix: move iat inside payload
Browse files Browse the repository at this point in the history
  • Loading branch information
apsantiso committed Jan 9, 2024
1 parent 8fb078e commit 7a54d69
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/modules/auth/jwt.strategy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('Jwt strategy', () => {
jest.spyOn(userUseCases, 'getUser').mockResolvedValue(user);

await expect(
strategy.validate({ payload: { uuid: 'anyUuid' }, iat: tokenIat }),
strategy.validate({ payload: { uuid: 'anyUuid', iat: tokenIat } }),
).rejects.toThrow(UnauthorizedException);
});

Expand All @@ -55,7 +55,7 @@ describe('Jwt strategy', () => {
jest.spyOn(userUseCases, 'getUser').mockResolvedValue(user);

await expect(
strategy.validate({ payload: { uuid: 'anyUuid' }, iat: tokenIat }),
strategy.validate({ payload: { uuid: 'anyUuid', iat: tokenIat } }),
).resolves.toBe(user);
});

Expand All @@ -67,7 +67,7 @@ describe('Jwt strategy', () => {
jest.spyOn(userUseCases, 'getUser').mockResolvedValue(user);

await expect(
strategy.validate({ payload: { uuid: 'anyUuid' }, iat: tokenIat }),
strategy.validate({ payload: { uuid: 'anyUui', iat: tokenIat } }),
).resolves.toBe(user);
});

Expand All @@ -91,7 +91,7 @@ describe('Jwt strategy', () => {
.mockResolvedValue(owner);

await expect(
strategy.validate({ payload: { uuid: anyUuid }, iat: tokenIat }),
strategy.validate({ payload: { uuid: anyUuid, iat: tokenIat } }),
).resolves.toBe(owner);

expect(getUserSpy).toHaveBeenCalledWith(anyUuid);
Expand Down
7 changes: 2 additions & 5 deletions src/modules/auth/jwt.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class JwtStrategy extends PassportStrategy(Strategy, strategyId) {
if (!payload.payload || !payload.payload.uuid) {
throw new UnauthorizedException('Old token version detected');
}
const { uuid } = payload.payload;
const { uuid, iat } = payload.payload;
const user = await this.userUseCases.getUser(uuid);
if (!user) {
throw new UnauthorizedException();
Expand All @@ -48,10 +48,7 @@ export class JwtStrategy extends PassportStrategy(Strategy, strategyId) {

const tokenOlderThanLastPasswordChangedAt =
user.lastPasswordChangedAt &&
!isTokenIatGreaterThanDate(
new Date(user.lastPasswordChangedAt),
payload.iat,
);
!isTokenIatGreaterThanDate(new Date(user.lastPasswordChangedAt), iat);

if (
!userWithoutLastPasswordChangedAt &&
Expand Down
2 changes: 1 addition & 1 deletion src/modules/user/user.usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,8 @@ export class UserUseCases {
user: userData.bridgeUser,
pass: userData.userId,
},
iat: getTokenDefaultIat(),
},
iat: getTokenDefaultIat(),
};
}

Expand Down

0 comments on commit 7a54d69

Please sign in to comment.