-
Notifications
You must be signed in to change notification settings - Fork 0
feat(auth): implement resend code functionality with strategies #87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
16a3ff3
feat(auth): implement resend code functionality with strategies
maksberegovoi 88807a3
feat(auth): add rate limiting and retry tracking to resend code use case
maksberegovoi 32207ec
fix(auth): clarify resend code descriptions in API docs
maksberegovoi f967b2d
Merge branch 'dev' into feat/resend-code
maksberegovoi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,19 @@ | ||
| import { SignUpDto } from '@core/auth/application/dtos'; | ||
|
|
||
| export interface SignUpCacheData { | ||
| user: SignUpDto; | ||
| password: string; | ||
| otp: { | ||
| token: string; | ||
| secret: string; | ||
| }; | ||
| } | ||
|
|
||
| export interface ResetPasswordCacheData { | ||
| email: string; | ||
| otp: { | ||
| secret: string; | ||
| token: string; | ||
| }; | ||
| isVerified: boolean; | ||
| } |
This file contains hidden or 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 @@ | ||
| export * from './cache-data.interface'; |
This file contains hidden or 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,11 @@ | ||
| import { ResendCodeDto } from '../dtos'; | ||
| import { ResetPasswordResendStrategy } from './reset-password-resend.strategy'; | ||
| import { ResendCodeStrategy } from './resend-code.strategy'; | ||
| import { SignUpResendStrategy } from './sign-up-resend.strategy'; | ||
|
|
||
| export const RESEND_CODE_STRATEGIES: Record<ResendCodeDto['context'], ResendCodeStrategy> = { | ||
| 'sign-up': new SignUpResendStrategy(), | ||
| 'reset-password': new ResetPasswordResendStrategy(), | ||
| }; | ||
|
|
||
| export { ResendCodeStrategy } from './resend-code.strategy'; |
This file contains hidden or 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,26 @@ | ||
| import { Queue } from 'bullmq'; | ||
| import { ResendCodeDto } from '../dtos'; | ||
|
|
||
| export abstract class ResendCodeStrategy<TCacheData = unknown> { | ||
| abstract readonly context: ResendCodeDto['context']; | ||
| abstract readonly successMessage: string; | ||
| abstract readonly cacheNotFoundCode: string; | ||
| abstract readonly cacheNotFoundMessage: string; | ||
|
|
||
| abstract getCacheKey(email: string): string; | ||
|
|
||
| abstract generateOtp(): Promise<{ token: string; secret: string }>; | ||
|
|
||
| abstract buildNewCacheData( | ||
| cachedData: TCacheData, | ||
| newToken: string, | ||
| newSecret: string, | ||
| ): TCacheData; | ||
|
|
||
| abstract dispatchEmail( | ||
| mailQueue: Queue, | ||
| email: string, | ||
| token: string, | ||
| cachedData: TCacheData, | ||
| ): Promise<void>; | ||
| } |
59 changes: 59 additions & 0 deletions
59
src/auth/application/strategies/reset-password-resend.strategy.ts
This file contains hidden or 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,59 @@ | ||
| import { AuthMailJobs } from '@core/auth/domain/enums'; | ||
| import { ResetPasswordEvent } from '@core/auth/domain/events'; | ||
| import { ResetPasswordCacheData } from '@core/auth/application/interfaces'; | ||
| import { | ||
| EMAIL_CODE_TTL_SECONDS, | ||
| RESET_PASSWORD_CACHE_KEY, | ||
| } from '@core/auth/infrastructure/constants'; | ||
| import { Queue } from 'bullmq'; | ||
| import { generate, generateSecret } from 'otplib'; | ||
| import { ResendCodeStrategy } from './resend-code.strategy'; | ||
|
|
||
| export class ResetPasswordResendStrategy extends ResendCodeStrategy<ResetPasswordCacheData> { | ||
| readonly context = 'reset-password' as const; | ||
| readonly successMessage = 'Повторный код для восстановления пароля отправлен на вашу почту'; | ||
| readonly cacheNotFoundCode = 'RESET_SESSION_EXPIRED'; | ||
| readonly cacheNotFoundMessage = | ||
| 'Время подтверждения истекло или запрос не найден. Запросите код снова.'; | ||
|
|
||
| getCacheKey(email: string): string { | ||
| return RESET_PASSWORD_CACHE_KEY(email); | ||
| } | ||
|
|
||
| async generateOtp(): Promise<{ token: string; secret: string }> { | ||
| const secret = generateSecret(); | ||
| const token = await generate({ | ||
| secret, | ||
| digits: 6, | ||
| period: EMAIL_CODE_TTL_SECONDS, | ||
| strategy: 'totp', | ||
| }); | ||
|
|
||
| return { token, secret }; | ||
| } | ||
|
|
||
| buildNewCacheData( | ||
| cachedData: ResetPasswordCacheData, | ||
| newToken: string, | ||
| newSecret: string, | ||
| ): ResetPasswordCacheData { | ||
| return { | ||
| ...cachedData, | ||
| otp: { token: newToken, secret: newSecret }, | ||
| isVerified: false, | ||
| }; | ||
| } | ||
|
|
||
| async dispatchEmail( | ||
| mailQueue: Queue, | ||
| email: string, | ||
| token: string, | ||
| _cachedData: ResetPasswordCacheData, | ||
| ): Promise<void> { | ||
| const event = new ResetPasswordEvent(email, token); | ||
| await mailQueue.add(AuthMailJobs.SEND_RESET_PASSWORD, event, { | ||
| attempts: 3, | ||
| backoff: { type: 'exponential', delay: 5000 }, | ||
| }); | ||
| } | ||
| } |
55 changes: 55 additions & 0 deletions
55
src/auth/application/strategies/sign-up-resend.strategy.ts
This file contains hidden or 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,55 @@ | ||
| import { AuthMailJobs } from '@core/auth/domain/enums'; | ||
| import { RegisterCodeEvent } from '@core/auth/domain/events'; | ||
| import { SignUpCacheData } from '@core/auth/application/interfaces'; | ||
| import { EMAIL_CODE_TTL_SECONDS, SIGNUP_CACHE_KEY } from '@core/auth/infrastructure/constants'; | ||
| import { Queue } from 'bullmq'; | ||
| import { generate, generateSecret } from 'otplib'; | ||
| import { ResendCodeStrategy } from './resend-code.strategy'; | ||
|
|
||
| export class SignUpResendStrategy extends ResendCodeStrategy<SignUpCacheData> { | ||
| readonly context = 'sign-up' as const; | ||
| readonly successMessage = 'Повторный код подтверждения отправлен на вашу почту'; | ||
| readonly cacheNotFoundCode = 'REGISTRATION_EXPIRED'; | ||
| readonly cacheNotFoundMessage = 'Срок регистрации истек или email не найден. Попробуйте снова.'; | ||
|
|
||
| getCacheKey(email: string): string { | ||
| return SIGNUP_CACHE_KEY(email); | ||
| } | ||
|
|
||
| async generateOtp(): Promise<{ token: string; secret: string }> { | ||
| const secret = generateSecret(); | ||
| const token = await generate({ | ||
| secret, | ||
| algorithm: 'sha256', | ||
| digits: 6, | ||
| period: EMAIL_CODE_TTL_SECONDS, | ||
| strategy: 'totp', | ||
| }); | ||
|
|
||
| return { token, secret }; | ||
| } | ||
|
|
||
| buildNewCacheData( | ||
| cachedData: SignUpCacheData, | ||
| newToken: string, | ||
| newSecret: string, | ||
| ): SignUpCacheData { | ||
| return { | ||
| ...cachedData, | ||
| otp: { token: newToken, secret: newSecret }, | ||
| }; | ||
| } | ||
|
|
||
| async dispatchEmail( | ||
| mailQueue: Queue, | ||
| email: string, | ||
| token: string, | ||
| cachedData: SignUpCacheData, | ||
| ): Promise<void> { | ||
| const event = new RegisterCodeEvent(email, cachedData.user.firstName, token); | ||
| await mailQueue.add(AuthMailJobs.SEND_REGISTER_CODE, event, { | ||
| attempts: 3, | ||
| backoff: { type: 'exponential', delay: 5000 }, | ||
| }); | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.