This repository has been archived by the owner on Apr 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ffd8fdf
commit f133777
Showing
5 changed files
with
300 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Delete, | ||
Get, | ||
Param, | ||
ParseIntPipe, | ||
Patch, | ||
Post, | ||
Put, | ||
Query, | ||
} from '@nestjs/common'; | ||
import { apiKeys } from '@prisma/client'; | ||
import { Expose } from 'src/modules/prisma/prisma.interface'; | ||
import { CursorPipe } from 'src/pipes/cursor.pipe'; | ||
import { OptionalIntPipe } from 'src/pipes/optional-int.pipe'; | ||
import { OrderByPipe } from 'src/pipes/order-by.pipe'; | ||
import { WherePipe } from 'src/pipes/where.pipe'; | ||
import { Scopes } from '../auth/scope.decorator'; | ||
import { | ||
CreateApiKeyDto, | ||
ReplaceApiKeyDto, | ||
UpdateApiKeyDto, | ||
} from './api-keys.dto'; | ||
import { ApiKeysService } from './api-keys.service'; | ||
|
||
@Controller('groups/:groupId/api-keys') | ||
export class ApiKeyController { | ||
constructor(private apiKeysService: ApiKeysService) {} | ||
|
||
@Post() | ||
@Scopes('group-{groupId}:write-api-key') | ||
async create( | ||
@Param('groupId', ParseIntPipe) groupId: number, | ||
@Body() data: CreateApiKeyDto, | ||
): Promise<Expose<apiKeys>> { | ||
return this.apiKeysService.createApiKey(groupId, data); | ||
} | ||
|
||
@Get() | ||
@Scopes('group-{groupId}:read-api-key') | ||
async getAll( | ||
@Param('groupId', ParseIntPipe) groupId: number, | ||
@Query('skip', OptionalIntPipe) skip?: number, | ||
@Query('take', OptionalIntPipe) take?: number, | ||
@Query('cursor', CursorPipe) cursor?: Record<string, number | string>, | ||
@Query('where', WherePipe) where?: Record<string, number | string>, | ||
@Query('orderBy', OrderByPipe) orderBy?: Record<string, 'asc' | 'desc'>, | ||
): Promise<Expose<apiKeys>[]> { | ||
return this.apiKeysService.getApiKeys(groupId, { | ||
skip, | ||
take, | ||
orderBy, | ||
cursor, | ||
where, | ||
}); | ||
} | ||
|
||
@Get(':id') | ||
@Scopes('group-{groupId}:read-api-key-{id}') | ||
async get( | ||
@Param('groupId', ParseIntPipe) groupId: number, | ||
@Param('id', ParseIntPipe) id: number, | ||
): Promise<Expose<apiKeys>> { | ||
return this.apiKeysService.getApiKey(groupId, Number(id)); | ||
} | ||
|
||
@Patch(':id') | ||
@Scopes('group-{groupId}:write-api-key-{id}') | ||
async update( | ||
@Body() data: UpdateApiKeyDto, | ||
@Param('groupId', ParseIntPipe) groupId: number, | ||
@Param('id', ParseIntPipe) id: number, | ||
): Promise<Expose<apiKeys>> { | ||
return this.apiKeysService.updateApiKey(groupId, Number(id), data); | ||
} | ||
|
||
@Put(':id') | ||
@Scopes('group-{groupId}:write-api-key-{id}') | ||
async replace( | ||
@Body() data: ReplaceApiKeyDto, | ||
@Param('groupId', ParseIntPipe) groupId: number, | ||
@Param('id', ParseIntPipe) id: number, | ||
): Promise<Expose<apiKeys>> { | ||
return this.apiKeysService.updateApiKey(groupId, Number(id), data); | ||
} | ||
|
||
@Delete(':id') | ||
@Scopes('group-{groupId}:delete-api-key-{id}') | ||
async remove( | ||
@Param('groupId', ParseIntPipe) groupId: number, | ||
@Param('id', ParseIntPipe) id: number, | ||
): Promise<Expose<apiKeys>> { | ||
return this.apiKeysService.deleteApiKey(groupId, Number(id)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { IsArray, IsNotEmpty, IsOptional, IsString } from 'class-validator'; | ||
|
||
export class CreateApiKeyDto { | ||
@IsString() | ||
@IsOptional() | ||
description?: string; | ||
|
||
@IsString() | ||
@IsOptional() | ||
name?: string; | ||
|
||
@IsArray() | ||
@IsString({ each: true }) | ||
@IsOptional() | ||
scopes?: string[]; | ||
|
||
@IsArray() | ||
@IsString({ each: true }) | ||
@IsOptional() | ||
ipRestrictions?: string[]; | ||
|
||
@IsArray() | ||
@IsString({ each: true }) | ||
@IsOptional() | ||
referrerRestrictions?: string[]; | ||
} | ||
|
||
export class UpdateApiKeyDto { | ||
@IsString() | ||
@IsOptional() | ||
description?: string; | ||
|
||
@IsString() | ||
@IsOptional() | ||
name?: string; | ||
|
||
@IsArray() | ||
@IsString({ each: true }) | ||
@IsOptional() | ||
scopes?: string[]; | ||
|
||
@IsArray() | ||
@IsString({ each: true }) | ||
@IsOptional() | ||
ipRestrictions?: string[]; | ||
|
||
@IsArray() | ||
@IsString({ each: true }) | ||
@IsOptional() | ||
referrerRestrictions?: string[]; | ||
} | ||
|
||
export class ReplaceApiKeyDto { | ||
@IsString() | ||
@IsNotEmpty() | ||
description: string; | ||
|
||
@IsString() | ||
@IsNotEmpty() | ||
name: string; | ||
|
||
@IsArray() | ||
@IsString({ each: true }) | ||
@IsNotEmpty() | ||
scopes: string[]; | ||
|
||
@IsArray() | ||
@IsString({ each: true }) | ||
@IsNotEmpty() | ||
ipRestrictions: string[]; | ||
|
||
@IsArray() | ||
@IsString({ each: true }) | ||
@IsNotEmpty() | ||
referrerRestrictions: string[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { PrismaModule } from '../prisma/prisma.module'; | ||
import { ApiKeyController } from './api-keys.controller'; | ||
import { ApiKeysService } from './api-keys.service'; | ||
|
||
@Module({ | ||
imports: [PrismaModule], | ||
controllers: [ApiKeyController], | ||
providers: [ApiKeysService], | ||
}) | ||
export class ApiKeysModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { | ||
HttpException, | ||
HttpStatus, | ||
Injectable, | ||
UnauthorizedException, | ||
} from '@nestjs/common'; | ||
import { randomStringGenerator } from '@nestjs/common/utils/random-string-generator.util'; | ||
import { | ||
apiKeys, | ||
apiKeysCreateInput, | ||
apiKeysOrderByInput, | ||
apiKeysUpdateInput, | ||
apiKeysWhereInput, | ||
apiKeysWhereUniqueInput, | ||
} from '@prisma/client'; | ||
import { Expose } from 'src/modules/prisma/prisma.interface'; | ||
import { PrismaService } from '../prisma/prisma.service'; | ||
|
||
@Injectable() | ||
export class ApiKeysService { | ||
constructor(private prisma: PrismaService) {} | ||
|
||
async createApiKey( | ||
groupId: number, | ||
data: Omit<Omit<apiKeysCreateInput, 'apiKey'>, 'group'>, | ||
): Promise<apiKeys> { | ||
const apiKey = randomStringGenerator(); | ||
return this.prisma.apiKeys.create({ | ||
data: { ...data, apiKey, group: { connect: { id: groupId } } }, | ||
}); | ||
} | ||
|
||
async getApiKeys( | ||
groupId: number, | ||
params: { | ||
skip?: number; | ||
take?: number; | ||
cursor?: apiKeysWhereUniqueInput; | ||
where?: apiKeysWhereInput; | ||
orderBy?: apiKeysOrderByInput; | ||
}, | ||
): Promise<Expose<apiKeys>[]> { | ||
const { skip, take, cursor, where, orderBy } = params; | ||
const apiKeys = await this.prisma.apiKeys.findMany({ | ||
skip, | ||
take, | ||
cursor, | ||
where: { ...where, group: { id: groupId } }, | ||
orderBy, | ||
}); | ||
return apiKeys.map(group => this.prisma.expose<apiKeys>(group)); | ||
} | ||
|
||
async getApiKey( | ||
groupId: number, | ||
id: number, | ||
): Promise<Expose<apiKeys> | null> { | ||
const apiKey = await this.prisma.apiKeys.findOne({ | ||
where: { id }, | ||
}); | ||
if (!apiKey) | ||
throw new HttpException('ApiKey not found', HttpStatus.NOT_FOUND); | ||
if (apiKey.groupId !== groupId) throw new UnauthorizedException(); | ||
return this.prisma.expose<apiKeys>(apiKey); | ||
} | ||
|
||
async updateApiKey( | ||
groupId: number, | ||
id: number, | ||
data: apiKeysUpdateInput, | ||
): Promise<Expose<apiKeys>> { | ||
const testApiKey = await this.prisma.apiKeys.findOne({ | ||
where: { id }, | ||
}); | ||
if (!testApiKey) | ||
throw new HttpException('ApiKey not found', HttpStatus.NOT_FOUND); | ||
if (testApiKey.groupId !== groupId) throw new UnauthorizedException(); | ||
const apiKey = await this.prisma.apiKeys.update({ | ||
where: { id }, | ||
data, | ||
}); | ||
return this.prisma.expose<apiKeys>(apiKey); | ||
} | ||
|
||
async replaceApiKey( | ||
groupId: number, | ||
id: number, | ||
data: apiKeysCreateInput, | ||
): Promise<Expose<apiKeys>> { | ||
const testApiKey = await this.prisma.apiKeys.findOne({ | ||
where: { id }, | ||
}); | ||
if (!testApiKey) | ||
throw new HttpException('ApiKey not found', HttpStatus.NOT_FOUND); | ||
if (testApiKey.groupId !== groupId) throw new UnauthorizedException(); | ||
const apiKey = await this.prisma.apiKeys.update({ | ||
where: { id }, | ||
data, | ||
}); | ||
return this.prisma.expose<apiKeys>(apiKey); | ||
} | ||
|
||
async deleteApiKey(groupId: number, id: number): Promise<Expose<apiKeys>> { | ||
const testApiKey = await this.prisma.apiKeys.findOne({ | ||
where: { id }, | ||
}); | ||
if (!testApiKey) | ||
throw new HttpException('ApiKey not found', HttpStatus.NOT_FOUND); | ||
if (testApiKey.groupId !== groupId) throw new UnauthorizedException(); | ||
const apiKey = await this.prisma.apiKeys.delete({ | ||
where: { id }, | ||
}); | ||
return this.prisma.expose<apiKeys>(apiKey); | ||
} | ||
} |