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: ...@@ -17,4 +17,6 @@ jwt:
secretOrPrivateKey: 'secret' secretOrPrivateKey: 'secret'
signOptions: signOptions:
expiresIn: '1d' expiresIn: '1d'
centerAccount: '1111111111' centerAccount: '1111111111'
\ No newline at end of file es:
secret: 'http://localhost:9200'
\ No newline at end of file
...@@ -21,8 +21,9 @@ import { AuthController } from './auth/auth.controller'; ...@@ -21,8 +21,9 @@ import { AuthController } from './auth/auth.controller';
load: [loadConfig], load: [loadConfig],
isGlobal: true, isGlobal: true,
}), }),
ElasticsearchModule.register({ ElasticsearchModule.registerAsync({
node: 'http://poi.lan:9200', inject: [ConfigService],
useFactory: (configService: ConfigService) => configService.get('es'),
}), }),
KoishiModule.registerAsync({ KoishiModule.registerAsync({
inject: [ConfigService], 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 { ...@@ -31,8 +31,7 @@ export class BlankReturnMessageDto implements BlankReturnMessage {
export class ReturnMessageDto<T> export class ReturnMessageDto<T>
extends BlankReturnMessageDto extends BlankReturnMessageDto
implements ReturnMessage<T> implements ReturnMessage<T> {
{
@ApiProperty({ description: '返回内容' }) @ApiProperty({ description: '返回内容' })
data?: T; data?: T;
constructor(statusCode: number, message?: string, data?: T) { constructor(statusCode: number, message?: string, data?: T) {
...@@ -40,3 +39,8 @@ export class ReturnMessageDto<T> ...@@ -40,3 +39,8 @@ export class ReturnMessageDto<T>
this.data = data; this.data = data;
} }
} }
export class StringReturnMessageDto extends BlankReturnMessageDto {
@ApiProperty({ description: '返回内容' })
data: string;
}
...@@ -55,7 +55,7 @@ export class MessageService ...@@ -55,7 +55,7 @@ export class MessageService
); );
await this.elasticsearchService.index({ await this.elasticsearchService.index({
index: session.selfId, index: `user_message_${session.selfId}`,
body: { body: {
selfId: session.selfId, selfId: session.selfId,
type: session.subtype as string, type: session.subtype as string,
......
...@@ -5,6 +5,7 @@ import S3Assets from '@koishijs/plugin-assets-s3'; ...@@ -5,6 +5,7 @@ import S3Assets from '@koishijs/plugin-assets-s3';
import { AdapterConfig } from '../onebot-improved/utils'; import { AdapterConfig } from '../onebot-improved/utils';
import { BotConfig } from '../onebot-improved'; import { BotConfig } from '../onebot-improved';
import { JwtModuleOptions } from '@nestjs/jwt'; import { JwtModuleOptions } from '@nestjs/jwt';
import { ClientOptions } from '@elastic/elasticsearch';
export interface Config { export interface Config {
host: string; host: string;
...@@ -13,6 +14,7 @@ export interface Config { ...@@ -13,6 +14,7 @@ export interface Config {
s3?: S3Assets.Config; s3?: S3Assets.Config;
jwt: JwtModuleOptions; jwt: JwtModuleOptions;
centerAccount: string; centerAccount: string;
es: ClientOptions;
} }
const defaultConfig: Config = { const defaultConfig: Config = {
...@@ -26,6 +28,9 @@ const defaultConfig: Config = { ...@@ -26,6 +28,9 @@ const defaultConfig: Config = {
}, },
}, },
centerAccount: '1111111111', centerAccount: '1111111111',
es: {
node: 'http://localhost:9200',
},
}; };
export async function loadConfig(): Promise<Config> { 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