-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Intergrate Redis to save refresh-token instead of client-cookie
- Loading branch information
Showing
9 changed files
with
181 additions
and
5 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
7 changes: 7 additions & 0 deletions
7
server/src/iam/authentication/dto/refresh-token.dto/refresh-token.dto.spec.ts
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { RefreshTokenDto } from './refresh-token.dto'; | ||
|
||
describe('RefreshTokenDto', () => { | ||
it('should be defined', () => { | ||
expect(new RefreshTokenDto()).toBeDefined(); | ||
}); | ||
}); |
6 changes: 6 additions & 0 deletions
6
server/src/iam/authentication/dto/refresh-token.dto/refresh-token.dto.ts
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { IsNotEmpty } from 'class-validator'; | ||
|
||
export class RefreshTokenDto { | ||
@IsNotEmpty() | ||
refreshToken: string; | ||
} |
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
7 changes: 7 additions & 0 deletions
7
.../src/iam/authentication/utils/refresh-token-ids.storage/refresh-token-ids.storage.spec.ts
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { RefreshTokenIdsStorage } from './refresh-token-ids.storage'; | ||
|
||
describe('RefreshTokenIdsStorage', () => { | ||
it('should be defined', () => { | ||
expect(new RefreshTokenIdsStorage()).toBeDefined(); | ||
}); | ||
}); |
46 changes: 46 additions & 0 deletions
46
server/src/iam/authentication/utils/refresh-token-ids.storage/refresh-token-ids.storage.ts
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { | ||
Injectable, | ||
OnApplicationBootstrap, | ||
OnApplicationShutdown, | ||
} from '@nestjs/common'; | ||
|
||
import redisClient from 'ioredis'; | ||
|
||
export class RefreshTokenIdsStorageError extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
} | ||
} | ||
|
||
@Injectable() | ||
export class RefreshTokenIdsStorage | ||
implements OnApplicationBootstrap, OnApplicationShutdown | ||
{ | ||
private redisClient: redisClient; | ||
|
||
onApplicationBootstrap() { | ||
this.redisClient = new redisClient(process.env.REDIS_URL); | ||
} | ||
onApplicationShutdown() { | ||
this.redisClient.quit(); | ||
} | ||
|
||
async insert(userId: number, tokenID: string): Promise<void> { | ||
await this.redisClient.set(this.getKey(userId), tokenID); | ||
} | ||
|
||
async validate(userId: number, tokenID: string): Promise<boolean> { | ||
const storedTokenID = await this.redisClient.get(this.getKey(userId)); | ||
if (!storedTokenID) | ||
throw new RefreshTokenIdsStorageError('Token ID not found'); | ||
return storedTokenID === tokenID; | ||
} | ||
|
||
async invalidate(userId: number): Promise<void> { | ||
await this.redisClient.del(this.getKey(userId)); | ||
} | ||
|
||
private getKey(userId: number): string { | ||
return `user-${userId}`; | ||
} | ||
} |