[Backend] Secure Refresh Token Rotation Mechanisms#323
Open
armorbreak001 wants to merge 1 commit intoGalactiGuild:mainfrom
Open
[Backend] Secure Refresh Token Rotation Mechanisms#323armorbreak001 wants to merge 1 commit intoGalactiGuild:mainfrom
armorbreak001 wants to merge 1 commit intoGalactiGuild:mainfrom
Conversation
Fixes GalactiGuild#294 - Add RefreshToken model to Prisma schema (opaque tokens, not JWTs) - Create RefreshTokenService with full rotation logic: - Generate: random 64-byte hex strings, SHA-256 hashed in DB - Rotate: issue new token, invalidate old one (prevents replay attacks) - Replay detection: if revoked token is reused, revoke entire token chain - Revoke single or all tokens for a user - Cleanup expired tokens - Update AuthService to use RefreshTokenService: - Login/Register/WalletAuth now generate opaque refresh tokens - POST /auth/refresh rotates tokens (new token issued, old invalidated) - Access token TTL: 15 minutes, Refresh token TTL: 7 days - Tokens stored in dedicated refresh_tokens table, not on User model
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #294
What was done
Prisma Schema
RefreshTokenmodel toschema.prisma:token(unique, SHA-256 hashed) — opaque random string, NOT a JWTuserId→ User relation with cascade deleteexpiresAt— 7-day TTLreplacedBy— links to new token after rotation (for replay detection)revokedAt— timestamp when token was invalidatedRefreshTokenService (
src/auth/refresh-token.service.ts)replacedBylink) → issues brand new token atomically via Prisma transactionAuthService Updates
RefreshTokenService.generate()instead of JWT-based refresh tokensPOST /auth/refreshnow callsrotate()— each refresh gets a completely new token, old one is invalidatedHow to verify
cd backend && npx prisma db pushto apply schema changesaccessTokenandrefreshTokenPOST /auth/refreshwith{"refreshToken": "..."}— get a NEW refreshToken (old one is now invalid)refresh_tokenstable in DB — see hashed tokens withrevokedAtandreplacedByfields populated