-
-
Notifications
You must be signed in to change notification settings - Fork 682
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): implement pat schema & apis (#597)
- Loading branch information
Showing
9 changed files
with
259 additions
and
4 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
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
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,13 @@ | ||
import { ApiProperty } from '@nestjs/swagger' | ||
import { IsNotEmpty, IsString } from 'class-validator' | ||
|
||
export class Pat2TokenDto { | ||
@ApiProperty({ | ||
description: 'PAT', | ||
example: | ||
'laf_1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef', | ||
}) | ||
@IsString() | ||
@IsNotEmpty() | ||
pat: 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,24 @@ | ||
import { ApiProperty } from '@nestjs/swagger' | ||
import { | ||
IsNotEmpty, | ||
IsNumber, | ||
IsString, | ||
Length, | ||
Max, | ||
Min, | ||
} from 'class-validator' | ||
|
||
export class CreatePATDto { | ||
@IsString() | ||
@IsNotEmpty() | ||
@Length(1, 255) | ||
@ApiProperty() | ||
name: string | ||
|
||
@IsNumber() | ||
@IsNotEmpty() | ||
@Min(60) | ||
@Max(3600 * 24 * 365) | ||
@ApiProperty({ minimum: 60 }) | ||
expiresIn: number | ||
} |
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,84 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Delete, | ||
Get, | ||
Logger, | ||
Param, | ||
Post, | ||
Req, | ||
UseGuards, | ||
} from '@nestjs/common' | ||
import { | ||
ApiBearerAuth, | ||
ApiOperation, | ||
ApiResponse, | ||
ApiTags, | ||
} from '@nestjs/swagger' | ||
import { JwtAuthGuard } from 'src/auth/jwt.auth.guard' | ||
import { ResponseUtil } from 'src/utils/response' | ||
import { IRequest } from 'src/utils/types' | ||
import { CreatePATDto } from './dto/create-pat.dto' | ||
import { PatService } from './pat.service' | ||
|
||
@ApiTags('Authentication') | ||
@ApiBearerAuth('Authorization') | ||
@Controller('pats') | ||
export class PatController { | ||
private readonly logger = new Logger(PatController.name) | ||
|
||
constructor(private readonly patService: PatService) {} | ||
|
||
/** | ||
* Create a PAT | ||
* @param req | ||
* @param dto | ||
* @returns | ||
*/ | ||
@ApiOperation({ summary: 'Create a PAT' }) | ||
@ApiResponse({ type: ResponseUtil }) | ||
@UseGuards(JwtAuthGuard) | ||
@Post() | ||
async create(@Req() req: IRequest, @Body() dto: CreatePATDto) { | ||
const uid = req.user.id | ||
// check max count, 10 | ||
const count = await this.patService.count(uid) | ||
if (count >= 10) { | ||
return ResponseUtil.error('Max count of PAT is 10') | ||
} | ||
|
||
const pat = await this.patService.create(uid, dto) | ||
return ResponseUtil.ok(pat) | ||
} | ||
|
||
/** | ||
* List PATs | ||
* @param req | ||
* @returns | ||
*/ | ||
@ApiOperation({ summary: 'List PATs' }) | ||
@ApiResponse({ type: ResponseUtil }) | ||
@UseGuards(JwtAuthGuard) | ||
@Get() | ||
async findAll(@Req() req: IRequest) { | ||
const uid = req.user.id | ||
const pats = await this.patService.findAll(uid) | ||
return ResponseUtil.ok(pats) | ||
} | ||
|
||
/** | ||
* Delete a PAT | ||
* @param req | ||
* @param id | ||
* @returns | ||
*/ | ||
@ApiOperation({ summary: 'Delete a PAT' }) | ||
@ApiResponse({ type: ResponseUtil }) | ||
@UseGuards(JwtAuthGuard) | ||
@Delete(':id') | ||
async remove(@Req() req: IRequest, @Param('id') id: string) { | ||
const uid = req.user.id | ||
const pat = await this.patService.remove(uid, id) | ||
return ResponseUtil.ok(pat) | ||
} | ||
} |
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,66 @@ | ||
import { Injectable, Logger } from '@nestjs/common' | ||
import { GenerateAlphaNumericPassword } from 'src/utils/random' | ||
import { PrismaService } from '../prisma.service' | ||
import { CreatePATDto } from './dto/create-pat.dto' | ||
|
||
@Injectable() | ||
export class PatService { | ||
private readonly logger = new Logger(PatService.name) | ||
|
||
constructor(private readonly prisma: PrismaService) {} | ||
|
||
async create(userid: string, dto: CreatePATDto) { | ||
const { name, expiresIn } = dto | ||
const token = 'laf_' + GenerateAlphaNumericPassword(60) | ||
|
||
const pat = await this.prisma.personalAccessToken.create({ | ||
data: { | ||
name, | ||
token, | ||
expiredAt: new Date(Date.now() + expiresIn * 1000), | ||
user: { | ||
connect: { id: userid }, | ||
}, | ||
}, | ||
}) | ||
return pat | ||
} | ||
|
||
async findAll(userid: string) { | ||
const pats = await this.prisma.personalAccessToken.findMany({ | ||
where: { uid: userid }, | ||
select: { | ||
id: true, | ||
uid: true, | ||
name: true, | ||
expiredAt: true, | ||
createdAt: true, | ||
}, | ||
}) | ||
return pats | ||
} | ||
|
||
async findOne(token: string) { | ||
const pat = await this.prisma.personalAccessToken.findFirst({ | ||
where: { token }, | ||
include: { | ||
user: true, | ||
}, | ||
}) | ||
return pat | ||
} | ||
|
||
async count(userid: string) { | ||
const count = await this.prisma.personalAccessToken.count({ | ||
where: { uid: userid }, | ||
}) | ||
return count | ||
} | ||
|
||
async remove(userid: string, id: string) { | ||
const pat = await this.prisma.personalAccessToken.deleteMany({ | ||
where: { id, uid: userid }, | ||
}) | ||
return pat | ||
} | ||
} |
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 |
---|---|---|
@@ -1,9 +1,12 @@ | ||
import { Module } from '@nestjs/common' | ||
import { PrismaService } from '../prisma.service' | ||
import { UserService } from './user.service' | ||
import { PatService } from './pat.service' | ||
import { PatController } from './pat.controller' | ||
|
||
@Module({ | ||
providers: [UserService, PrismaService], | ||
providers: [UserService, PrismaService, PatService], | ||
exports: [UserService], | ||
controllers: [PatController], | ||
}) | ||
export class UserModule {} |