fix: re-sign jwt delay

Signed-off-by: Innei <i@innei.in>
This commit is contained in:
Innei
2023-06-23 13:14:00 +08:00
parent 972e2cdc21
commit 7536dd17cf
3 changed files with 25 additions and 14 deletions

View File

@@ -53,13 +53,14 @@ export class UserController {
@CurrentUser() user: UserDocument,
@CurrentUserToken() token: string,
) {
await this.authService.jwtServicePublic.revokeToken(token)
await this.userService.recordFootstep(ipLocation.ip)
return {
token: this.authService.jwtServicePublic.sign(user.id, {
ip: ipLocation.ip,
ua: ipLocation.agent,
}),
token: await this.authService.jwtServicePublic
.sign(user.id, {
ip: ipLocation.ip,
ua: ipLocation.agent,
})
.then(() => this.authService.jwtServicePublic.revokeToken(token, 6000)),
}
}
@@ -73,7 +74,7 @@ export class UserController {
const avatar = user.avatar ?? getAvatar(mail)
return {
token: this.authService.jwtServicePublic.sign(user.id, {
token: await this.authService.jwtServicePublic.sign(user.id, {
ip: ipLocation.ip,
ua: ipLocation.agent,
}),

View File

@@ -81,7 +81,7 @@ export class UserService {
}
const res = await this.userModel.create({ ...model })
const token = this.authService.jwtServicePublic.sign(res.id)
const token = await this.authService.jwtServicePublic.sign(res.id)
return { token, username: res.username }
}

View File

@@ -72,13 +72,23 @@ export class JWTService {
})
}
async revokeToken(token: string) {
async revokeToken(token: string, delay?: number) {
const redis = this.cacheService.getClient()
const key = getRedisKey(RedisKeys.JWTStore)
await redis.hdel(
key,
token.startsWith(`jwt-`) ? token.replace(`jwt-`, '') : md5(token),
)
if (delay) {
// FIXME
setTimeout(() => {
redis.hdel(
key,
token.startsWith(`jwt-`) ? token.replace(`jwt-`, '') : md5(token),
)
}, delay)
} else {
await redis.hdel(
key,
token.startsWith(`jwt-`) ? token.replace(`jwt-`, '') : md5(token),
)
}
}
async revokeAll() {
@@ -101,11 +111,11 @@ export class JWTService {
public static readonly expiresDay = SECURITY.jwtExpire
sign(id: string, info?: { ip: string; ua: string }) {
async sign(id: string, info?: { ip: string; ua: string }) {
const token = sign({ id }, this.secret, {
expiresIn: `${JWTService.expiresDay}d`,
})
this.storeTokenInRedis(token, info || {})
await this.storeTokenInRedis(token, info || {})
return token
}
}