Skip to content

Commit 7cbd5af

Browse files
committed
fix: add state for google to enhance security
1 parent 9829b71 commit 7cbd5af

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

app/controllers/auth.controller.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import crypto from 'crypto'
12
import type {
23
GoogleCodeSchema,
34
RefreshTokenSchema,
@@ -15,7 +16,7 @@ import { Conflict, Forbidden, Unauthorized } from 'http-errors'
1516
import { jwtVerify, SignJWT } from 'jose'
1617
import { JWTExpired } from 'jose/errors'
1718

18-
import { env } from '@/config'
19+
import { env, redis } from '@/config'
1920

2021
const {
2122
ACCESS_JWT_EXPIRES_IN,
@@ -93,7 +94,12 @@ class AuthController {
9394
}
9495

9596
getGoogleRedirectUrl = async (_: Request, res: Response) => {
97+
const state = crypto.randomBytes(32).toString('hex')
98+
99+
await redis.set(`oauth_state:${state}`, 'true', 'EX', 5 * 60)
100+
96101
const url = this.googleClient.generateAuthUrl({
102+
state,
97103
access_type: 'offline',
98104
scope: ['profile', 'email']
99105
})
@@ -106,7 +112,17 @@ class AuthController {
106112
res: Response,
107113
next: NextFunction
108114
) => {
109-
const { tokens } = await this.googleClient.getToken(req.body.code)
115+
const { code, state: receivedState } = req.body
116+
117+
const redisStateKey = `oauth_state:${receivedState}`
118+
119+
const storedState = await redis.get(redisStateKey)
120+
121+
if (storedState) {
122+
await redis.del(redisStateKey)
123+
}
124+
125+
const { tokens } = await this.googleClient.getToken(code)
110126

111127
if (!tokens.id_token) return next(Forbidden())
112128

app/schemas/auth.schema.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ export const RefreshTokenSchema = z.object({
1717
})
1818

1919
export const GoogleCodeSchema = z.object({
20-
code: z.string().min(1, 'Code is required')
20+
code: z.string().min(1, 'Code is required'),
21+
state: z.string().min(1, 'State is required')
2122
})

0 commit comments

Comments
 (0)