2060 create a new api key (#2206)
* Add folder for api settings * Init create api key page * Update create api key page * Implement api call to create apiKey * Add create api key mutation * Get id when creating apiKey * Display created Api Key * Add delete api key button * Remove button from InputText * Update stuff * Add test for ApiDetail * Fix type * Use recoil instead of router state * Remane route paths * Remove online return * Move and test date util * Remove useless Component * Rename ApiKeys paths * Rename ApiKeys files * Add input text info testing * Rename hooks to webhooks * Remove console error * Add tests to reach minimum coverage
This commit is contained in:
@ -19,7 +19,7 @@ import {
|
||||
import { JwtAuthGuard } from 'src/guards/jwt.auth.guard';
|
||||
import { UserAbility } from 'src/decorators/user-ability.decorator';
|
||||
import { AppAbility } from 'src/ability/ability.factory';
|
||||
import { AuthToken } from 'src/core/auth/dto/token.entity';
|
||||
import { ApiKeyToken } from 'src/core/auth/dto/token.entity';
|
||||
|
||||
import { ApiKeyService } from './api-key.service';
|
||||
|
||||
@ -28,13 +28,13 @@ import { ApiKeyService } from './api-key.service';
|
||||
export class ApiKeyResolver {
|
||||
constructor(private readonly apiKeyService: ApiKeyService) {}
|
||||
|
||||
@Mutation(() => AuthToken)
|
||||
@Mutation(() => ApiKeyToken)
|
||||
@UseGuards(AbilityGuard)
|
||||
@CheckAbilities(CreateApiKeyAbilityHandler)
|
||||
async createOneApiKey(
|
||||
@Args() args: CreateOneApiKeyArgs,
|
||||
@AuthWorkspace() { id: workspaceId }: Workspace,
|
||||
): Promise<AuthToken> {
|
||||
): Promise<ApiKeyToken> {
|
||||
return await this.apiKeyService.generateApiKeyToken(
|
||||
workspaceId,
|
||||
args.data.name,
|
||||
|
||||
@ -5,7 +5,7 @@ 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 { ApiKeyToken } from 'src/core/auth/dto/token.entity';
|
||||
import { assert } from 'src/utils/assert';
|
||||
import { EnvironmentService } from 'src/integrations/environment/environment.service';
|
||||
|
||||
@ -28,7 +28,7 @@ export class ApiKeyService {
|
||||
workspaceId: string,
|
||||
name: string,
|
||||
expiresAt?: Date | string,
|
||||
): Promise<AuthToken> {
|
||||
): Promise<ApiKeyToken> {
|
||||
const secret = this.environmentService.getAccessTokenSecret();
|
||||
let expiresIn: string | number;
|
||||
let expirationDate: Date;
|
||||
@ -52,6 +52,7 @@ export class ApiKeyService {
|
||||
},
|
||||
});
|
||||
return {
|
||||
id,
|
||||
token: this.jwtService.sign(jwtPayload, {
|
||||
secret,
|
||||
expiresIn,
|
||||
|
||||
@ -9,6 +9,18 @@ export class AuthToken {
|
||||
expiresAt: Date;
|
||||
}
|
||||
|
||||
@ObjectType()
|
||||
export class ApiKeyToken {
|
||||
@Field(() => String)
|
||||
id: string;
|
||||
|
||||
@Field(() => String)
|
||||
token: string;
|
||||
|
||||
@Field(() => Date)
|
||||
expiresAt: Date;
|
||||
}
|
||||
|
||||
@ObjectType()
|
||||
export class AuthTokenPair {
|
||||
@Field(() => AuthToken)
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
|
||||
import { WebHookModule } from 'src/core/web-hook/web-hook.module';
|
||||
|
||||
import { UserModule } from './user/user.module';
|
||||
import { CommentModule } from './comment/comment.module';
|
||||
import { CompanyModule } from './company/company.module';
|
||||
@ -15,7 +17,6 @@ import { ActivityModule } from './activity/activity.module';
|
||||
import { ViewModule } from './view/view.module';
|
||||
import { FavoriteModule } from './favorite/favorite.module';
|
||||
import { ApiKeyModule } from './api-key/api-key.module';
|
||||
import { HookModule } from './hook/hook.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@ -34,7 +35,7 @@ import { HookModule } from './hook/hook.module';
|
||||
ViewModule,
|
||||
FavoriteModule,
|
||||
ApiKeyModule,
|
||||
HookModule,
|
||||
WebHookModule,
|
||||
],
|
||||
exports: [
|
||||
AuthModule,
|
||||
@ -48,7 +49,7 @@ import { HookModule } from './hook/hook.module';
|
||||
AttachmentModule,
|
||||
FavoriteModule,
|
||||
ApiKeyModule,
|
||||
HookModule,
|
||||
WebHookModule,
|
||||
],
|
||||
})
|
||||
export class CoreModule {}
|
||||
|
||||
@ -2,11 +2,10 @@ import { Module } from '@nestjs/common';
|
||||
|
||||
import { PrismaModule } from 'src/database/prisma.module';
|
||||
import { AbilityModule } from 'src/ability/ability.module';
|
||||
|
||||
import { HookResolver } from './hook.resolver';
|
||||
import { WebHookResolver } from 'src/core/web-hook/web-hook.resolver';
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule, AbilityModule],
|
||||
providers: [HookResolver],
|
||||
providers: [WebHookResolver],
|
||||
})
|
||||
export class HookModule {}
|
||||
export class WebHookModule {}
|
||||
@ -4,35 +4,35 @@ import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
|
||||
import { accessibleBy } from '@casl/prisma';
|
||||
|
||||
import { JwtAuthGuard } from 'src/guards/jwt.auth.guard';
|
||||
import { Hook } from 'src/core/@generated/hook/hook.model';
|
||||
import { AbilityGuard } from 'src/guards/ability.guard';
|
||||
import { CheckAbilities } from 'src/decorators/check-abilities.decorator';
|
||||
import {
|
||||
CreateHookAbilityHandler,
|
||||
DeleteHookAbilityHandler,
|
||||
ReadHookAbilityHandler,
|
||||
} from 'src/ability/handlers/hook.ability-handler';
|
||||
import { CreateOneHookArgs } from 'src/core/@generated/hook/create-one-hook.args';
|
||||
CreateWebHookAbilityHandler,
|
||||
DeleteWebHookAbilityHandler,
|
||||
ReadWebHookAbilityHandler,
|
||||
} from 'src/ability/handlers/web-hook.ability-handler';
|
||||
import { PrismaService } from 'src/database/prisma.service';
|
||||
import { AuthWorkspace } from 'src/decorators/auth-workspace.decorator';
|
||||
import { Workspace } from 'src/core/@generated/workspace/workspace.model';
|
||||
import { DeleteOneHookArgs } from 'src/core/@generated/hook/delete-one-hook.args';
|
||||
import { FindManyHookArgs } from 'src/core/@generated/hook/find-many-hook.args';
|
||||
import { UserAbility } from 'src/decorators/user-ability.decorator';
|
||||
import { AppAbility } from 'src/ability/ability.factory';
|
||||
import { CreateOneWebHookArgs } from 'src/core/@generated/web-hook/create-one-web-hook.args';
|
||||
import { DeleteOneWebHookArgs } from 'src/core/@generated/web-hook/delete-one-web-hook.args';
|
||||
import { FindManyWebHookArgs } from 'src/core/@generated/web-hook/find-many-web-hook.args';
|
||||
import { WebHook } from 'src/core/@generated/web-hook/web-hook.model';
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Resolver(() => Hook)
|
||||
export class HookResolver {
|
||||
@Resolver(() => WebHook)
|
||||
export class WebHookResolver {
|
||||
constructor(private readonly prismaService: PrismaService) {}
|
||||
@Mutation(() => Hook)
|
||||
@Mutation(() => WebHook)
|
||||
@UseGuards(AbilityGuard)
|
||||
@CheckAbilities(CreateHookAbilityHandler)
|
||||
async createOneHook(
|
||||
@Args() args: CreateOneHookArgs,
|
||||
@CheckAbilities(CreateWebHookAbilityHandler)
|
||||
async createOneWebHook(
|
||||
@Args() args: CreateOneWebHookArgs,
|
||||
@AuthWorkspace() { id: workspaceId }: Workspace,
|
||||
): Promise<Hook> {
|
||||
return this.prismaService.client.hook.create({
|
||||
): Promise<WebHook> {
|
||||
return this.prismaService.client.webHook.create({
|
||||
data: {
|
||||
...args.data,
|
||||
...{ workspace: { connect: { id: workspaceId } } },
|
||||
@ -40,31 +40,31 @@ export class HookResolver {
|
||||
});
|
||||
}
|
||||
|
||||
@Mutation(() => Hook, { nullable: false })
|
||||
@Mutation(() => WebHook, { nullable: false })
|
||||
@UseGuards(AbilityGuard)
|
||||
@CheckAbilities(DeleteHookAbilityHandler)
|
||||
async deleteOneHook(@Args() args: DeleteOneHookArgs): Promise<Hook> {
|
||||
const hookToDelete = this.prismaService.client.hook.findUnique({
|
||||
@CheckAbilities(DeleteWebHookAbilityHandler)
|
||||
async deleteOneWebHook(@Args() args: DeleteOneWebHookArgs): Promise<WebHook> {
|
||||
const hookToDelete = this.prismaService.client.webHook.findUnique({
|
||||
where: args.where,
|
||||
});
|
||||
if (!hookToDelete) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
return await this.prismaService.client.hook.delete({
|
||||
return await this.prismaService.client.webHook.delete({
|
||||
where: args.where,
|
||||
});
|
||||
}
|
||||
|
||||
@Query(() => [Hook])
|
||||
@Query(() => [WebHook])
|
||||
@UseGuards(AbilityGuard)
|
||||
@CheckAbilities(ReadHookAbilityHandler)
|
||||
async findManyHook(
|
||||
@Args() args: FindManyHookArgs,
|
||||
@CheckAbilities(ReadWebHookAbilityHandler)
|
||||
async findManyWebHook(
|
||||
@Args() args: FindManyWebHookArgs,
|
||||
@UserAbility() ability: AppAbility,
|
||||
) {
|
||||
const filterOptions = [accessibleBy(ability).WorkspaceMember];
|
||||
if (args.where) filterOptions.push(args.where);
|
||||
return this.prismaService.client.hook.findMany({
|
||||
return this.prismaService.client.webHook.findMany({
|
||||
...args,
|
||||
where: { AND: filterOptions },
|
||||
});
|
||||
Reference in New Issue
Block a user