1043 timebox prepare zapier integration (#1967)
* Add create api-key route * Import module * Remove required mutation parameter * Fix Authentication * Generate random key * Update Read ApiKeyAbility handler * Add findMany apiKey route * Remove useless attribute * Use signed token for apiKeys * Authenticate with api keys * Fix typo * Add a test for apiKey module * Revoke token when api key does not exist * Handler expiresAt parameter * Fix user passport * Code review returns: Add API_TOKEN_SECRET * Code review returns: Rename variable * Code review returns: Update code style * Update apiKey schema * Update create token route * Update delete token route * Filter revoked api keys from listApiKeys * Rename endpoint * Set default expiry to 2 years * Code review returns: Update comment * Generate token after create apiKey * Code review returns: Update env variable * Code review returns: Move method to proper service --------- Co-authored-by: martmull <martmull@hotmail.com>
This commit is contained in:
63
server/src/core/api-key/api-key.service.ts
Normal file
63
server/src/core/api-key/api-key.service.ts
Normal file
@ -0,0 +1,63 @@
|
||||
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 { AuthToken } from 'src/core/auth/dto/token.entity';
|
||||
import { assert } from 'src/utils/assert';
|
||||
import { EnvironmentService } from 'src/integrations/environment/environment.service';
|
||||
|
||||
@Injectable()
|
||||
export class ApiKeyService {
|
||||
constructor(
|
||||
private readonly prismaService: PrismaService,
|
||||
private readonly environmentService: EnvironmentService,
|
||||
private readonly jwtService: JwtService,
|
||||
) {}
|
||||
|
||||
findFirst = this.prismaService.client.apiKey.findFirst;
|
||||
findUniqueOrThrow = this.prismaService.client.apiKey.findUniqueOrThrow;
|
||||
findMany = this.prismaService.client.apiKey.findMany;
|
||||
create = this.prismaService.client.apiKey.create;
|
||||
update = this.prismaService.client.apiKey.update;
|
||||
delete = this.prismaService.client.apiKey.delete;
|
||||
|
||||
async generateApiKeyToken(
|
||||
workspaceId: string,
|
||||
name: string,
|
||||
expiresAt?: Date | string,
|
||||
): Promise<AuthToken> {
|
||||
const secret = this.environmentService.getApiTokenSecret();
|
||||
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({
|
||||
data: {
|
||||
expiresAt: expiresAt,
|
||||
name: name,
|
||||
workspaceId: workspaceId,
|
||||
},
|
||||
});
|
||||
return {
|
||||
token: this.jwtService.sign(jwtPayload, {
|
||||
secret,
|
||||
expiresIn,
|
||||
jwtid: id,
|
||||
}),
|
||||
expiresAt: expirationDate,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user