Remove api keys from old world (#2548)
* Use apiKeyV2 for getApiKeys * Use apiKeyV2 for createApiKey * Use apiKeyV2 for getApiKey * Use apiKeyV2 to deleteapikey * Filter null revokedAt -> not working * Use apiKeyV2 to regenerate * Fix default values injected * Remove useless stuff * Fix type
This commit is contained in:
@ -42,6 +42,21 @@ export class ApiKeyResolver {
|
||||
);
|
||||
}
|
||||
|
||||
@Mutation(() => ApiKeyToken)
|
||||
@UseGuards(AbilityGuard)
|
||||
@CheckAbilities(CreateApiKeyAbilityHandler)
|
||||
async generateApiKeyV2Token(
|
||||
@Args()
|
||||
args: CreateOneApiKeyArgs,
|
||||
@AuthWorkspace() { id: workspaceId }: Workspace,
|
||||
): Promise<Pick<ApiKeyToken, 'token'> | undefined> {
|
||||
return await this.apiKeyService.generateApiKeyV2Token(
|
||||
workspaceId,
|
||||
args.data.id,
|
||||
args.data.expiresAt,
|
||||
);
|
||||
}
|
||||
|
||||
@Mutation(() => ApiKey)
|
||||
@UseGuards(AbilityGuard)
|
||||
@CheckAbilities(UpdateApiKeyAbilityHandler)
|
||||
|
||||
@ -21,6 +21,34 @@ export class ApiKeyService {
|
||||
update = this.prismaService.client.apiKey.update;
|
||||
delete = this.prismaService.client.apiKey.delete;
|
||||
|
||||
async generateApiKeyV2Token(
|
||||
workspaceId: string,
|
||||
apiKeyId?: string,
|
||||
expiresAt?: Date | string,
|
||||
): Promise<Pick<ApiKeyToken, 'token'> | undefined> {
|
||||
if (!apiKeyId) {
|
||||
return;
|
||||
}
|
||||
const jwtPayload = {
|
||||
sub: workspaceId,
|
||||
};
|
||||
const secret = this.environmentService.getAccessTokenSecret();
|
||||
let expiresIn: string | number;
|
||||
if (expiresAt) {
|
||||
expiresIn = Math.floor(
|
||||
(new Date(expiresAt).getTime() - new Date().getTime()) / 1000,
|
||||
);
|
||||
} else {
|
||||
expiresIn = this.environmentService.getApiTokenExpiresIn();
|
||||
}
|
||||
const token = this.jwtService.sign(jwtPayload, {
|
||||
secret,
|
||||
expiresIn,
|
||||
jwtid: apiKeyId,
|
||||
});
|
||||
return { token };
|
||||
}
|
||||
|
||||
async generateApiKeyToken(
|
||||
workspaceId: string,
|
||||
name: string,
|
||||
|
||||
Reference in New Issue
Block a user