fix: client captcha (#8186)

This commit is contained in:
darkskygit
2024-09-10 09:34:20 +00:00
parent 9ccf517e06
commit 95738e796f
2 changed files with 43 additions and 1 deletions

View File

@@ -40,6 +40,48 @@ export class TokenService {
return this.crypto.encrypt(token);
}
/**
* get token by type
*
* token will be revoked if expired or keep is not set
*/
async getToken(type: TokenType, token: string, keep?: boolean) {
token = this.crypto.decrypt(token);
const record = await this.db.verificationToken.findUnique({
where: {
type_token: {
token,
type,
},
},
});
if (!record) {
return null;
}
const expired = record.expiresAt <= new Date();
// always revoke expired token
if (expired || !keep) {
const deleted = await this.revokeToken(type, token);
// already deleted, means token has been used
if (!deleted.count) {
return null;
}
}
return !expired ? record : null;
}
/**
* get token and verify credential
*
* if credential is not provided, it will be failed
*
* token will be revoked if expired or keep is not set
*/
async verifyToken(
type: TokenType,
token: string,

View File

@@ -90,7 +90,7 @@ export class CaptchaService {
const challenge = credential.challenge;
if (typeof challenge === 'string' && challenge) {
const resource = await this.token
.verifyToken(TokenType.Challenge, challenge)
.getToken(TokenType.Challenge, challenge)
.then(token => token?.credential);
if (!resource) {