* 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>
86 lines
2.5 KiB
TypeScript
86 lines
2.5 KiB
TypeScript
import {
|
|
ExecutionContext,
|
|
Injectable,
|
|
NotFoundException,
|
|
} from '@nestjs/common';
|
|
import { GqlExecutionContext } from '@nestjs/graphql';
|
|
|
|
import { subject } from '@casl/ability';
|
|
|
|
import { IAbilityHandler } from 'src/ability/interfaces/ability-handler.interface';
|
|
|
|
import { AppAbility } from 'src/ability/ability.factory';
|
|
import { AbilityAction } from 'src/ability/ability.action';
|
|
import { PrismaService } from 'src/database/prisma.service';
|
|
import { ApiKeyWhereUniqueInput } from 'src/core/@generated/api-key/api-key-where-unique.input';
|
|
import { ApiKeyWhereInput } from 'src/core/@generated/api-key/api-key-where.input';
|
|
import { assert } from 'src/utils/assert';
|
|
import {
|
|
convertToWhereInput,
|
|
relationAbilityChecker,
|
|
} from 'src/ability/ability.util';
|
|
|
|
class ApiKeyArgs {
|
|
where?: ApiKeyWhereUniqueInput | ApiKeyWhereInput;
|
|
[key: string]: any;
|
|
}
|
|
|
|
@Injectable()
|
|
export class ManageApiKeyAbilityHandler implements IAbilityHandler {
|
|
async handle(ability: AppAbility) {
|
|
return ability.can(AbilityAction.Manage, 'ApiKey');
|
|
}
|
|
}
|
|
|
|
@Injectable()
|
|
export class ReadApiKeyAbilityHandler implements IAbilityHandler {
|
|
async handle(ability: AppAbility) {
|
|
return ability.can(AbilityAction.Read, 'ApiKey');
|
|
}
|
|
}
|
|
|
|
@Injectable()
|
|
export class CreateApiKeyAbilityHandler implements IAbilityHandler {
|
|
constructor(private readonly prismaService: PrismaService) {}
|
|
|
|
async handle(ability: AppAbility, context: ExecutionContext) {
|
|
const gqlContext = GqlExecutionContext.create(context);
|
|
const args = gqlContext.getArgs();
|
|
const allowed = await relationAbilityChecker(
|
|
'ApiKey',
|
|
ability,
|
|
this.prismaService.client,
|
|
args,
|
|
);
|
|
if (!allowed) {
|
|
return false;
|
|
}
|
|
return ability.can(AbilityAction.Create, 'ApiKey');
|
|
}
|
|
}
|
|
|
|
@Injectable()
|
|
export class UpdateApiKeyAbilityHandler implements IAbilityHandler {
|
|
constructor(private readonly prismaService: PrismaService) {}
|
|
|
|
async handle(ability: AppAbility, context: ExecutionContext) {
|
|
const gqlContext = GqlExecutionContext.create(context);
|
|
const args = gqlContext.getArgs<ApiKeyArgs>();
|
|
const where = convertToWhereInput(args.where);
|
|
const apiKey = await this.prismaService.client.apiKey.findFirst({
|
|
where,
|
|
});
|
|
assert(apiKey, '', NotFoundException);
|
|
const allowed = await relationAbilityChecker(
|
|
'ApiKey',
|
|
ability,
|
|
this.prismaService.client,
|
|
args,
|
|
);
|
|
if (!allowed) {
|
|
return false;
|
|
}
|
|
return ability.can(AbilityAction.Update, subject('ApiKey', apiKey));
|
|
}
|
|
}
|