Commit b9159b32 authored by nanahira's avatar nanahira

jwt

parent d9c2f05e
Pipeline #8117 passed with stages
in 4 minutes and 4 seconds
......@@ -17,4 +17,6 @@ jwt:
secretOrPrivateKey: 'secret'
signOptions:
expiresIn: '1d'
centerAccount: '1111111111'
\ No newline at end of file
centerAccount: '1111111111'
es:
secret: 'http://localhost:9200'
\ No newline at end of file
......@@ -21,8 +21,9 @@ import { AuthController } from './auth/auth.controller';
load: [loadConfig],
isGlobal: true,
}),
ElasticsearchModule.register({
node: 'http://poi.lan:9200',
ElasticsearchModule.registerAsync({
inject: [ConfigService],
useFactory: (configService: ConfigService) => configService.get('es'),
}),
KoishiModule.registerAsync({
inject: [ConfigService],
......
import { Test, TestingModule } from '@nestjs/testing';
import { AuthController } from './auth.controller';
describe('AuthController', () => {
let controller: AuthController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [AuthController],
}).compile();
controller = module.get<AuthController>(AuthController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});
import {
Body,
Controller,
Headers,
Post,
ValidationPipe,
} from '@nestjs/common';
import {
ApiBody,
ApiCreatedResponse,
ApiHeader,
ApiOperation,
ApiTags,
} from '@nestjs/swagger';
import { AuthService } from './auth.service';
import { SendCodeDto } from './dto/SendCode.dto';
import {
BlankReturnMessageDto,
ReturnMessageDto,
StringReturnMessageDto,
} from '../dto/ReturnMessage.dto';
@Controller('api/auth')
@ApiTags('Auth')
export class AuthController {
constructor(private readonly authService: AuthService) {}
@Post('sendcode')
@ApiOperation({ summary: '发送 token' })
@ApiBody({ type: SendCodeDto })
@ApiCreatedResponse({ type: BlankReturnMessageDto })
async sendCode(
@Body(new ValidationPipe({ transform: true })) sendCodeDto: SendCodeDto,
): Promise<BlankReturnMessageDto> {
await this.authService.sendCode(sendCodeDto.userId);
return new BlankReturnMessageDto(201, 'success');
}
@Post('verifycode')
@ApiOperation({ summary: '验证 token' })
@ApiHeader({ name: 'Authorization', required: true })
@ApiCreatedResponse({ type: StringReturnMessageDto })
async verifyCode(@Headers('Authorization') token: string) {
const realToken = token?.startsWith('Bearer ') ? token.substring(7) : token;
const userId = await this.authService.verify(realToken);
return new ReturnMessageDto(201, 'success', userId);
}
}
import { IsNumberString, MaxLength, MinLength } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
export class SendCodeDto {
@IsNumberString()
@MinLength(5)
@MaxLength(11)
@ApiProperty({ description: 'QQ号码', example: '123456789' })
userId: string;
}
......@@ -31,8 +31,7 @@ export class BlankReturnMessageDto implements BlankReturnMessage {
export class ReturnMessageDto<T>
extends BlankReturnMessageDto
implements ReturnMessage<T>
{
implements ReturnMessage<T> {
@ApiProperty({ description: '返回内容' })
data?: T;
constructor(statusCode: number, message?: string, data?: T) {
......@@ -40,3 +39,8 @@ export class ReturnMessageDto<T>
this.data = data;
}
}
export class StringReturnMessageDto extends BlankReturnMessageDto {
@ApiProperty({ description: '返回内容' })
data: string;
}
......@@ -55,7 +55,7 @@ export class MessageService
);
await this.elasticsearchService.index({
index: session.selfId,
index: `user_message_${session.selfId}`,
body: {
selfId: session.selfId,
type: session.subtype as string,
......
......@@ -5,6 +5,7 @@ import S3Assets from '@koishijs/plugin-assets-s3';
import { AdapterConfig } from '../onebot-improved/utils';
import { BotConfig } from '../onebot-improved';
import { JwtModuleOptions } from '@nestjs/jwt';
import { ClientOptions } from '@elastic/elasticsearch';
export interface Config {
host: string;
......@@ -13,6 +14,7 @@ export interface Config {
s3?: S3Assets.Config;
jwt: JwtModuleOptions;
centerAccount: string;
es: ClientOptions;
}
const defaultConfig: Config = {
......@@ -26,6 +28,9 @@ const defaultConfig: Config = {
},
},
centerAccount: '1111111111',
es: {
node: 'http://localhost:9200',
},
};
export async function loadConfig(): Promise<Config> {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment