refactor: changed auth module to work with signed cookies

This commit is contained in:
Balu Babu 2023-01-30 06:31:10 +05:30
parent 1bbcd638b8
commit a8d50223aa
7 changed files with 10 additions and 8 deletions

View File

@ -6,6 +6,7 @@ POSTMARK_SERVER_TOKEN=************************************************"
POSTMARK_SENDER_EMAIL=************************************************" POSTMARK_SENDER_EMAIL=************************************************"
# Auth Tokens Config # Auth Tokens Config
SIGNED_COOKIE_SECRET='add some secret here'
JWT_SECRET='add some secret here' JWT_SECRET='add some secret here'
REFRESH_TOKEN_VALIDITY="604800000" # Default validity is 7 days REFRESH_TOKEN_VALIDITY="604800000" # Default validity is 7 days
ACCESS_TOKEN_VALIDITY="120000" # Default validity is 1 day ACCESS_TOKEN_VALIDITY="120000" # Default validity is 1 day
@ -13,13 +14,13 @@ ACCESS_TOKEN_VALIDITY="120000" # Default validity is 1 day
# Hoppscotch App Domain Config # Hoppscotch App Domain Config
APP_DOMAIN="************************************************"" APP_DOMAIN="************************************************""
REDIRECT_URL="************************************************"" REDIRECT_URL="************************************************""
WHITELISTED_ORIGINS ="************************************************" WHITELISTED_ORIGINS="************************************************"
# Google Auth Config # Google Auth Config
GOOGLE_CLIENT_ID="************************************************" GOOGLE_CLIENT_ID="************************************************"
GOOGLE_CLIENT_SECRET="************************************************" GOOGLE_CLIENT_SECRET="************************************************"
GOOGLE_CALLBACK_URL="************************************************" GOOGLE_CALLBACK_URL="************************************************"
GOOGLE_SCOPE= ['email', 'profile'], GOOGLE_SCOPE="['email', 'profile'],"
# Github Auth Config # Github Auth Config
GITHUB_CLIENT_ID="************************************************" GITHUB_CLIENT_ID="************************************************"

View File

@ -98,6 +98,7 @@ model User {
model Account { model Account {
id String @id @default(cuid()) id String @id @default(cuid())
userId String userId String
user User @relation(fields: [userId], references: [uid], onDelete: Cascade)
provider String provider String
providerAccountId String providerAccountId String
providerRefreshToken String? providerRefreshToken String?
@ -105,8 +106,6 @@ model Account {
providerScope String? providerScope String?
loggedIn DateTime @default(now()) @db.Timestamp(3) loggedIn DateTime @default(now()) @db.Timestamp(3)
user User @relation(fields: [userId], references: [uid], onDelete: Cascade)
@@unique(fields: [provider, providerAccountId], name: "verifyProviderAccount") @@unique(fields: [provider, providerAccountId], name: "verifyProviderAccount")
} }

View File

@ -22,7 +22,7 @@ export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
super({ super({
jwtFromRequest: ExtractJwt.fromExtractors([ jwtFromRequest: ExtractJwt.fromExtractors([
(request: Request) => { (request: Request) => {
const ATCookie = request.cookies['access_token']; const ATCookie = request.signedCookies['access_token'];
if (!ATCookie) { if (!ATCookie) {
throw new ForbiddenException(COOKIES_NOT_FOUND); throw new ForbiddenException(COOKIES_NOT_FOUND);
} }

View File

@ -21,7 +21,7 @@ export class RTJwtStrategy extends PassportStrategy(Strategy, 'jwt-refresh') {
super({ super({
jwtFromRequest: ExtractJwt.fromExtractors([ jwtFromRequest: ExtractJwt.fromExtractors([
(request: Request) => { (request: Request) => {
const RTCookie = request.cookies['refresh_token']; const RTCookie = request.signedCookies['refresh_token'];
if (!RTCookie) { if (!RTCookie) {
throw new ForbiddenException(COOKIES_NOT_FOUND); throw new ForbiddenException(COOKIES_NOT_FOUND);
} }

View File

@ -4,6 +4,6 @@ import { GqlExecutionContext } from '@nestjs/graphql';
export const RTCookie = createParamDecorator( export const RTCookie = createParamDecorator(
(data: unknown, context: ExecutionContext) => { (data: unknown, context: ExecutionContext) => {
const ctx = GqlExecutionContext.create(context); const ctx = GqlExecutionContext.create(context);
return ctx.getContext().req.cookies['refresh_token']; return ctx.getContext().req.signedCookies['refresh_token'];
}, },
); );

View File

@ -31,7 +31,7 @@ async function bootstrap() {
origin: true, origin: true,
}); });
} }
app.use(cookieParser()); app.use(cookieParser(process.env.SIGNED_COOKIE_SECRET));
await app.listen(process.env.PORT || 3170); await app.listen(process.env.PORT || 3170);
} }
bootstrap(); bootstrap();

View File

@ -164,12 +164,14 @@ export const authCookieHandler = (
secure: true, secure: true,
sameSite: 'lax', sameSite: 'lax',
maxAge: accessTokenValidity, maxAge: accessTokenValidity,
signed: true,
}); });
res.cookie('refresh_token', authTokens.refresh_token, { res.cookie('refresh_token', authTokens.refresh_token, {
httpOnly: true, httpOnly: true,
secure: true, secure: true,
sameSite: 'lax', sameSite: 'lax',
maxAge: refreshTokenValidity, maxAge: refreshTokenValidity,
signed: true,
}); });
if (redirect) { if (redirect) {
res.status(HttpStatus.OK).redirect(process.env.REDIRECT_URL); res.status(HttpStatus.OK).redirect(process.env.REDIRECT_URL);