Fix cache management (#2264)

This commit is contained in:
martmull
2023-10-27 18:20:11 +02:00
committed by GitHub
parent acbcd2f162
commit 35237c05f3
11 changed files with 85 additions and 32 deletions

View File

@ -1,9 +1,6 @@
import { Injectable, InternalServerErrorException } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { addMilliseconds, addSeconds } from 'date-fns';
import ms from 'ms';
import { PrismaService } from 'src/database/prisma.service';
import { ApiKeyToken } from 'src/core/auth/dto/token.entity';
import { assert } from 'src/utils/assert';
@ -31,20 +28,17 @@ export class ApiKeyService {
): Promise<ApiKeyToken> {
const secret = this.environmentService.getAccessTokenSecret();
let expiresIn: string | number;
let expirationDate: Date;
const now = new Date().getTime();
if (expiresAt) {
expiresIn = Math.floor((new Date(expiresAt).getTime() - now) / 1000);
expirationDate = addSeconds(now, expiresIn);
} else {
expiresIn = this.environmentService.getApiTokenExpiresIn();
expirationDate = addMilliseconds(now, ms(expiresIn));
}
assert(expiresIn, '', InternalServerErrorException);
const jwtPayload = {
sub: workspaceId,
};
const { id } = await this.prismaService.client.apiKey.create({
const newApiKey = await this.prismaService.client.apiKey.create({
data: {
expiresAt: expiresAt,
name: name,
@ -52,13 +46,12 @@ export class ApiKeyService {
},
});
return {
id,
...newApiKey,
token: this.jwtService.sign(jwtPayload, {
secret,
expiresIn,
jwtid: id,
jwtid: newApiKey.id,
}),
expiresAt: expirationDate,
};
}
}

View File

@ -1,5 +1,7 @@
import { Field, ObjectType } from '@nestjs/graphql';
import { ApiKey } from 'src/core/@generated/api-key/api-key.model';
@ObjectType()
export class AuthToken {
@Field(() => String)
@ -10,15 +12,9 @@ export class AuthToken {
}
@ObjectType()
export class ApiKeyToken {
@Field(() => String)
id: string;
export class ApiKeyToken extends ApiKey {
@Field(() => String)
token: string;
@Field(() => Date)
expiresAt: Date;
}
@ObjectType()