Skip to content

Commit 08ccf0d

Browse files
committed
refactor: hash refresh token secret
1 parent 5e56c63 commit 08ccf0d

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

packages/features/oauth/services/OAuthService.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { TeamRepository } from "@calcom/features/ee/teams/repositories/Team
44
import type { AccessCodeRepository } from "@calcom/features/oauth/repositories/AccessCodeRepository";
55
import type { OAuthClientRepository } from "@calcom/features/oauth/repositories/OAuthClientRepository";
66
import type { OAuthRefreshTokenRepository } from "@calcom/features/oauth/repositories/OAuthRefreshTokenRepository";
7-
import { generateSecret } from "@calcom/features/oauth/utils/generateSecret";
7+
import { generateSecret, hashSecretKey } from "@calcom/features/oauth/utils/generateSecret";
88
import { ErrorCode } from "@calcom/lib/errorCodes";
99
import { ErrorWithCode } from "@calcom/lib/errors";
1010
import { verifyCodeChallenge } from "@calcom/lib/pkce";
@@ -327,7 +327,7 @@ export class OAuthService {
327327
});
328328

329329
await this.oAuthRefreshTokenRepository.create({
330-
secret: tokens.refreshTokenSecret,
330+
secret: hashSecretKey(tokens.refreshTokenSecret),
331331
clientId,
332332
userId: accessCode.userId,
333333
teamId: accessCode.teamId,
@@ -369,7 +369,8 @@ export class OAuthService {
369369
// note(Lauris): legacy tokens (issued before invalidating old refresh tokens was implemented) won't have a secret,
370370
// so we accept them once and issue new tokens with a secret.
371371
if (decodedToken.secret) {
372-
const storedToken = await this.oAuthRefreshTokenRepository.findBySecret(decodedToken.secret);
372+
const hashedSecret = hashSecretKey(decodedToken.secret);
373+
const storedToken = await this.oAuthRefreshTokenRepository.findBySecret(hashedSecret);
373374
if (!storedToken) {
374375
throw new ErrorWithCode(ErrorCode.BadRequest, "invalid_grant", { reason: "refresh_token_revoked" });
375376
}
@@ -386,14 +387,14 @@ export class OAuthService {
386387
await this.oAuthRefreshTokenRepository.rotateTokenForUser({
387388
clientId,
388389
userId: decodedToken.userId,
389-
newSecret: tokens.refreshTokenSecret,
390+
newSecret: hashSecretKey(tokens.refreshTokenSecret),
390391
expiresInSeconds: tokens.refreshTokenExpiresIn,
391392
});
392393
} else if (decodedToken.teamId) {
393394
await this.oAuthRefreshTokenRepository.rotateTokenForTeam({
394395
clientId,
395396
teamId: decodedToken.teamId,
396-
newSecret: tokens.refreshTokenSecret,
397+
newSecret: hashSecretKey(tokens.refreshTokenSecret),
397398
expiresInSeconds: tokens.refreshTokenExpiresIn,
398399
});
399400
} else {

0 commit comments

Comments
 (0)