Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ LOGIN_LOCK_IN_S=60

## JWT
JWT_SECRET_KEY=cksfkewkfoptyhiop534o239dkja23as

# API KEY
API_KEY=YHImpSchS4iEwVD1IxXp4012
28 changes: 0 additions & 28 deletions .github/workflows/pr-clean.yml

This file was deleted.

9 changes: 8 additions & 1 deletion bin/generate-swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ async function bootstrap(prefix) {
.setTitle('Auth API Server')
.setDescription('Auth API for auth service')
.setVersion('1.0')
.addBearerAuth()
.addApiKey(
{
in: 'header',
name: 'x-api-key',
type: 'apiKey',
},
'ApiKey'
)
.build();

const document = SwaggerModule.createDocument(app, config);
Expand Down
72 changes: 46 additions & 26 deletions openapi.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"hash": "ca9394abcd6643efa047cf77041bd2b8e8d54b2ad10bb13747fb8b843a4021a9",
"hash": "b1e9858642446999790d82b74b35a795a475e92d6bc12918b93199c9d4cd1335",
"openapi": "3.0.0",
"paths": {
"/hello": {
Expand Down Expand Up @@ -1086,7 +1086,7 @@
],
"security": [
{
"bearer": []
"ApiKey": []
}
]
},
Expand Down Expand Up @@ -1201,7 +1201,7 @@
],
"security": [
{
"bearer": []
"ApiKey": []
}
]
}
Expand Down Expand Up @@ -1239,7 +1239,7 @@
],
"security": [
{
"bearer": []
"ApiKey": []
}
]
},
Expand Down Expand Up @@ -1284,7 +1284,7 @@
],
"security": [
{
"bearer": []
"ApiKey": []
}
]
}
Expand Down Expand Up @@ -1314,7 +1314,7 @@
],
"security": [
{
"bearer": []
"ApiKey": []
}
]
}
Expand Down Expand Up @@ -1349,6 +1349,11 @@
},
"tags": [
"session"
],
"security": [
{
"ApiKey": []
}
]
},
"get": {
Expand Down Expand Up @@ -1469,6 +1474,11 @@
},
"tags": [
"session"
],
"security": [
{
"ApiKey": []
}
]
}
},
Expand Down Expand Up @@ -1501,6 +1511,11 @@
},
"tags": [
"session"
],
"security": [
{
"ApiKey": []
}
]
},
"patch": {
Expand Down Expand Up @@ -1541,6 +1556,11 @@
},
"tags": [
"session"
],
"security": [
{
"ApiKey": []
}
]
},
"delete": {
Expand All @@ -1567,7 +1587,7 @@
],
"security": [
{
"bearer": []
"ApiKey": []
}
]
}
Expand Down Expand Up @@ -2111,7 +2131,7 @@
],
"security": [
{
"bearer": []
"ApiKey": []
}
]
},
Expand Down Expand Up @@ -2243,7 +2263,7 @@
],
"security": [
{
"bearer": []
"ApiKey": []
}
]
}
Expand Down Expand Up @@ -2281,7 +2301,7 @@
],
"security": [
{
"bearer": []
"ApiKey": []
}
]
},
Expand Down Expand Up @@ -2326,7 +2346,7 @@
],
"security": [
{
"bearer": []
"ApiKey": []
}
]
},
Expand Down Expand Up @@ -2354,7 +2374,7 @@
],
"security": [
{
"bearer": []
"ApiKey": []
}
]
}
Expand Down Expand Up @@ -2418,7 +2438,7 @@
],
"security": [
{
"bearer": []
"ApiKey": []
}
]
},
Expand Down Expand Up @@ -2550,7 +2570,7 @@
],
"security": [
{
"bearer": []
"ApiKey": []
}
]
}
Expand Down Expand Up @@ -2588,7 +2608,7 @@
],
"security": [
{
"bearer": []
"ApiKey": []
}
]
},
Expand Down Expand Up @@ -2633,7 +2653,7 @@
],
"security": [
{
"bearer": []
"ApiKey": []
}
]
},
Expand Down Expand Up @@ -2661,7 +2681,7 @@
],
"security": [
{
"bearer": []
"ApiKey": []
}
]
}
Expand Down Expand Up @@ -3017,7 +3037,7 @@
],
"security": [
{
"bearer": []
"ApiKey": []
}
]
},
Expand Down Expand Up @@ -3107,7 +3127,7 @@
],
"security": [
{
"bearer": []
"ApiKey": []
}
]
}
Expand Down Expand Up @@ -3145,7 +3165,7 @@
],
"security": [
{
"bearer": []
"ApiKey": []
}
]
}
Expand Down Expand Up @@ -3192,7 +3212,7 @@
],
"security": [
{
"bearer": []
"ApiKey": []
}
]
},
Expand Down Expand Up @@ -3220,7 +3240,7 @@
],
"security": [
{
"bearer": []
"ApiKey": []
}
]
}
Expand All @@ -3236,10 +3256,10 @@
"servers": [],
"components": {
"securitySchemes": {
"bearer": {
"scheme": "bearer",
"bearerFormat": "JWT",
"type": "http"
"ApiKey": {
"type": "apiKey",
"in": "header",
"name": "x-api-key"
}
},
"schemas": {
Expand Down
10 changes: 8 additions & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { BullModule } from '@nestjs/bull';
import { CACHE_MANAGER, CacheModule } from '@nestjs/cache-manager';
import { Inject, MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { APP_GUARD } from '@nestjs/core';
import { EventEmitterModule } from '@nestjs/event-emitter';
import { MongooseModule } from '@nestjs/mongoose/dist/mongoose.module';
import { redisClusterInsStore, redisInsStore } from 'cache-manager-redis-yet';

import * as config from 'src/constants';

import { AppController } from './app.controller';
import { AuthModule } from './auth';
import { ApiKeyAuthGuard, AuthModule } from './auth';
import { CaptchaModule } from './captcha';
import { RouteLoggerMiddleware } from './common/route-logger.middleware';
import { EmailModule } from './email';
Expand Down Expand Up @@ -61,7 +62,12 @@ import { UserModule } from './user';
RoleModule,
],
controllers: [HelloController, AppController],
providers: [],
providers: [
{
provide: APP_GUARD,
useClass: ApiKeyAuthGuard,
},
],
})
export class AppModule implements NestModule {
constructor(@Inject(CACHE_MANAGER) private readonly cacheManager: any) {}
Expand Down
31 changes: 31 additions & 0 deletions src/auth/api-key-auth.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { CanActivate, ExecutionContext, Injectable, SetMetadata } from '@nestjs/common';
import { Reflector } from '@nestjs/core';

import { auth } from 'src/constants';

export const IS_PUBLIC_KEY = 'isPublic';

export const Public = () => SetMetadata(IS_PUBLIC_KEY, true);

@Injectable()
export class ApiKeyAuthGuard implements CanActivate {
constructor(private reflector: Reflector) {}

canActivate(context: ExecutionContext): any {
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
context.getHandler(),
context.getClass(),
]);
if (isPublic) {
return true;
}

const request = context.switchToHttp().getRequest();
const apiKey = request.headers['x-api-key'];
if (apiKey === auth.apiKey) {
return true;
}

return false;
}
}
1 change: 1 addition & 0 deletions src/auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './auth.module';
export * from './entities/jwt.entity';
export * from './entities/session-with-token.entity';
export * from './api-key-auth.guard';
1 change: 1 addition & 0 deletions src/constants/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const auth = {
maxLoginAttempts: toInteger(loadEnv('MAX_LOGIN_ATTEMPTS')), // 过期时间
loginLockInS: toInteger(loadEnv('LOGIN_LOCK_IN_S')), // 验证码长度
jwtSecretKey: loadEnv('JWT_SECRET_KEY'), // jwt secret key
apiKey: loadEnv('API_KEY'), // api key
};

export const captcha = {
Expand Down
Loading
Loading