mirror of
https://github.com/vastxie/99AI.git
synced 2025-11-13 20:23:43 +08:00
NineAI 2.4.2
This commit is contained in:
31
dist/common/auth/adminAuth.guard.js
vendored
Normal file
31
dist/common/auth/adminAuth.guard.js
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
"use strict";
|
||||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
||||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
||||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
||||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
||||
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.AdminAuthGuard = void 0;
|
||||
const common_1 = require("@nestjs/common");
|
||||
const jwtAuth_guard_1 = require("./jwtAuth.guard");
|
||||
let AdminAuthGuard = class AdminAuthGuard extends jwtAuth_guard_1.JwtAuthGuard {
|
||||
async canActivate(context) {
|
||||
const isAuthorized = await super.canActivate(context);
|
||||
if (!isAuthorized) {
|
||||
return false;
|
||||
}
|
||||
const request = context.switchToHttp().getRequest();
|
||||
const user = request.user;
|
||||
if (user && ['admin', 'super'].includes(user.role)) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
throw new common_1.UnauthorizedException('非法操作、您的权限等级不足、无法执行当前请求!');
|
||||
}
|
||||
}
|
||||
};
|
||||
AdminAuthGuard = __decorate([
|
||||
(0, common_1.Injectable)()
|
||||
], AdminAuthGuard);
|
||||
exports.AdminAuthGuard = AdminAuthGuard;
|
||||
33
dist/common/auth/jwt.strategy.js
vendored
Normal file
33
dist/common/auth/jwt.strategy.js
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
"use strict";
|
||||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
||||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
||||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
||||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
||||
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
||||
};
|
||||
var __metadata = (this && this.__metadata) || function (k, v) {
|
||||
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.JwtStrategy = void 0;
|
||||
const nestjs_config_1 = require("nestjs-config");
|
||||
const passport_jwt_1 = require("passport-jwt");
|
||||
const passport_1 = require("@nestjs/passport");
|
||||
const common_1 = require("@nestjs/common");
|
||||
let JwtStrategy = class JwtStrategy extends (0, passport_1.PassportStrategy)(passport_jwt_1.Strategy) {
|
||||
constructor(configService) {
|
||||
super({
|
||||
jwtFromRequest: passport_jwt_1.ExtractJwt.fromAuthHeaderAsBearerToken(),
|
||||
secretOrKey: configService.get('jwt').secret,
|
||||
});
|
||||
this.configService = configService;
|
||||
}
|
||||
async validate(payload) {
|
||||
return payload;
|
||||
}
|
||||
};
|
||||
JwtStrategy = __decorate([
|
||||
(0, common_1.Injectable)(),
|
||||
__metadata("design:paramtypes", [nestjs_config_1.ConfigService])
|
||||
], JwtStrategy);
|
||||
exports.JwtStrategy = JwtStrategy;
|
||||
82
dist/common/auth/jwtAuth.guard.js
vendored
Normal file
82
dist/common/auth/jwtAuth.guard.js
vendored
Normal file
@@ -0,0 +1,82 @@
|
||||
"use strict";
|
||||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
||||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
||||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
||||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
||||
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
||||
};
|
||||
var __metadata = (this && this.__metadata) || function (k, v) {
|
||||
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.JwtAuthGuard = void 0;
|
||||
const redisCache_service_1 = require("../../modules/redisCache/redisCache.service");
|
||||
const common_1 = require("@nestjs/common");
|
||||
const passport_1 = require("@nestjs/passport");
|
||||
const jwt = require("jsonwebtoken");
|
||||
const core_1 = require("@nestjs/core");
|
||||
const globalConfig_service_1 = require("../../modules/globalConfig/globalConfig.service");
|
||||
const auth_service_1 = require("../../modules/auth/auth.service");
|
||||
let JwtAuthGuard = class JwtAuthGuard extends (0, passport_1.AuthGuard)('jwt') {
|
||||
constructor(redisCacheService, moduleRef, globalConfigService, authService) {
|
||||
super();
|
||||
this.redisCacheService = redisCacheService;
|
||||
this.moduleRef = moduleRef;
|
||||
this.globalConfigService = globalConfigService;
|
||||
this.authService = authService;
|
||||
}
|
||||
async canActivate(context) {
|
||||
if (!this.redisCacheService) {
|
||||
this.redisCacheService = this.moduleRef.get(redisCache_service_1.RedisCacheService, { strict: false });
|
||||
}
|
||||
const request = context.switchToHttp().getRequest();
|
||||
const domain = request.headers['x-website-domain'];
|
||||
const token = this.extractToken(request);
|
||||
request.user = this.validateToken(token);
|
||||
const auth = this.globalConfigService.getNineAiToken();
|
||||
await this.redisCacheService.checkTokenAuth(token, request);
|
||||
return true;
|
||||
}
|
||||
extractToken(request) {
|
||||
if (!request.headers.authorization) {
|
||||
if (request.headers.fingerprint) {
|
||||
let id = request.headers.fingerprint;
|
||||
if (id > 2147483647) {
|
||||
id = id.toString().slice(-9);
|
||||
id = Number(String(Number(id)));
|
||||
}
|
||||
const token = this.authService.createTokenFromFingerprint(id);
|
||||
return token;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
const parts = request.headers.authorization.split(' ');
|
||||
if (parts.length !== 2 || parts[0] !== 'Bearer') {
|
||||
return null;
|
||||
}
|
||||
return parts[1];
|
||||
}
|
||||
validateToken(token) {
|
||||
try {
|
||||
return jwt.verify(token, process.env.JWT_SECRET);
|
||||
}
|
||||
catch (error) {
|
||||
throw new common_1.HttpException('亲爱的用户,请登录后继续操作,我们正在等您的到来!', common_1.HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
}
|
||||
handleRequest(err, user, info) {
|
||||
if (err || !user) {
|
||||
console.log('err: ', err);
|
||||
throw err || new common_1.UnauthorizedException();
|
||||
}
|
||||
return user;
|
||||
}
|
||||
};
|
||||
JwtAuthGuard = __decorate([
|
||||
(0, common_1.Injectable)(),
|
||||
__metadata("design:paramtypes", [redisCache_service_1.RedisCacheService,
|
||||
core_1.ModuleRef,
|
||||
globalConfig_service_1.GlobalConfigService,
|
||||
auth_service_1.AuthService])
|
||||
], JwtAuthGuard);
|
||||
exports.JwtAuthGuard = JwtAuthGuard;
|
||||
31
dist/common/auth/superAuth.guard.js
vendored
Normal file
31
dist/common/auth/superAuth.guard.js
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
"use strict";
|
||||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
||||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
||||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
||||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
||||
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.SuperAuthGuard = void 0;
|
||||
const common_1 = require("@nestjs/common");
|
||||
const jwtAuth_guard_1 = require("./jwtAuth.guard");
|
||||
let SuperAuthGuard = class SuperAuthGuard extends jwtAuth_guard_1.JwtAuthGuard {
|
||||
async canActivate(context) {
|
||||
const isAuthorized = await super.canActivate(context);
|
||||
if (!isAuthorized) {
|
||||
return false;
|
||||
}
|
||||
const request = context.switchToHttp().getRequest();
|
||||
const user = request.user;
|
||||
if (user && user.role === 'super') {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
throw new common_1.UnauthorizedException('非法操作、非超级管理员无权操作!');
|
||||
}
|
||||
}
|
||||
};
|
||||
SuperAuthGuard = __decorate([
|
||||
(0, common_1.Injectable)()
|
||||
], SuperAuthGuard);
|
||||
exports.SuperAuthGuard = SuperAuthGuard;
|
||||
Reference in New Issue
Block a user