add: 添加获取服务端KEY、生成加密URL、带用户标签的鉴权接口
This commit is contained in:
1 parent
c1fb52408e
commit
15d7edbac9
1 file changed
+134
-7
+134
-7
@@ -1,8 +1,11 @@
|
||||
import { Controller, Get, HttpStatus, Req, Res } from '@nestjs/common';
|
||||
import { Controller, Get, HttpStatus, Post, Req, Res } from '@nestjs/common';
|
||||
import { AppService } from './app.service';
|
||||
import { Request, Response } from 'express';
|
||||
import {
|
||||
createHash,
|
||||
} from 'crypto';
|
||||
|
||||
@Controller()
|
||||
@Controller('/testyoupaiyun')
|
||||
export class AppController {
|
||||
constructor(private readonly appService: AppService) {
|
||||
}
|
||||
@@ -12,15 +15,139 @@ export class AppController {
|
||||
return this.appService.getHello();
|
||||
}
|
||||
|
||||
@Get('/testyoupaiyun')
|
||||
testYouPaiYun(@Req() req: Request, @Res() res: Response): void {
|
||||
console.log(req.params);
|
||||
@Get('/easyAuthentication')
|
||||
easyAuthentication(@Req() req: Request): void {
|
||||
console.log(req.query);
|
||||
console.log(req.path);
|
||||
const responseStatus = req.query.sign && req.query.sign == '1234' ? 200 : 403;
|
||||
req.res.status(responseStatus).json({
|
||||
const responseStatus = req.query.sign && req.query.sign == '1234' && req.query.t && req.query.t == '1' ? 200 : 403;
|
||||
req.res.status(responseStatus).json(responseStatus == 200 ? {
|
||||
code: 0,
|
||||
msg: 'success',
|
||||
} : {
|
||||
code: -1,
|
||||
msg: 'forbidden',
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 模拟在系统中注册的合法的用户标签
|
||||
*/
|
||||
registerUserTag = [
|
||||
'10d6bcc0-d724-4078-910b-05531910b886',
|
||||
'afc2272c-1cec-44c7-806d-41b1da54e783',
|
||||
'00690352-3e0f-472b-b863-64ca21ba4a90',
|
||||
'ca44e10d-2454-479a-9af7-16e5128004e2',
|
||||
'17d5382f-fb68-4174-af26-5d7b6ee5877e',
|
||||
];
|
||||
|
||||
KEY = 'asdfasdvasdvasbvdasdvasdvsa';
|
||||
KEY2 = 'viek56asdfasd56v523vsdavasdvasv';
|
||||
|
||||
// sign = MD5(KEY2 + KEY + path + t + user-tag)
|
||||
|
||||
/**
|
||||
* 返回服务端的KEY
|
||||
* @param req
|
||||
*/
|
||||
@Post('/getServerKEY')
|
||||
getServerKEY(@Req() req: Request): void {
|
||||
console.log(req.body);
|
||||
|
||||
// 判断是否携带了有效的标签
|
||||
let isValid = false;
|
||||
for (let i = 0; i < this.registerUserTag.length; i++) {
|
||||
if (this.registerUserTag[i] == req.body['userTag']) {
|
||||
isValid = true;
|
||||
} else {
|
||||
console.log(`${this.registerUserTag[i]} != ${req.body['userTag']}`);
|
||||
}
|
||||
}
|
||||
if (!isValid) {
|
||||
req.res.status(HttpStatus.OK).json({
|
||||
code: -1,
|
||||
msg: '用户标签无效',
|
||||
});
|
||||
return;
|
||||
}
|
||||
req.res.status(HttpStatus.OK).json({
|
||||
code: 0,
|
||||
data: this.KEY2,
|
||||
});
|
||||
}
|
||||
|
||||
@Post('/generateEncodeURL')
|
||||
generateEncodeURL(@Req() req: Request): void {
|
||||
// sign = MD5(KEY2 + KEY + path + t + user-tag)
|
||||
const { KEY2, userTag, path } = req.body;
|
||||
if (!KEY2 || !userTag) {
|
||||
req.res.status(HttpStatus.OK).json({
|
||||
code: -1,
|
||||
msg: '请求参数不完整',
|
||||
});
|
||||
return;
|
||||
}
|
||||
const t = (Date.now() + 1000 * 60 * 5).toString();
|
||||
const md5 = createHash('md5');
|
||||
const sign = md5.update(KEY2.toString())
|
||||
.update(this.KEY)
|
||||
.update(path.toString())
|
||||
.update(t)
|
||||
.update(userTag.toString())
|
||||
.digest('hex');
|
||||
req.res.status(HttpStatus.OK).json({
|
||||
code: 0,
|
||||
data: {
|
||||
url: `http://cdn.qjm253.cn${path}?sign=${sign}&t=${t}&path=${path}`,
|
||||
sign,
|
||||
t,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@Get('/newAuthentication')
|
||||
newAuthentication(@Req() req: Request): void {
|
||||
const { sign, t, userTag, path } = req.query;
|
||||
|
||||
// 校验参数完整性
|
||||
if (!sign || !t || !userTag || !path) {
|
||||
req.res.status(HttpStatus.FORBIDDEN).json({
|
||||
code: -1,
|
||||
msg: '请求参数不完整',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// 校验时间戳有效性
|
||||
const expireTime = new Date(Number(t));
|
||||
if (Date.now() > expireTime.getTime()) { // 时间戳过期
|
||||
req.res.status(HttpStatus.FORBIDDEN).json({
|
||||
code: -1,
|
||||
msg: '链接过期',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const md5 = createHash('md5');
|
||||
console.log(req.baseUrl);
|
||||
// 校验签名
|
||||
const localCalSign = md5.update(this.KEY2)
|
||||
.update(this.KEY)
|
||||
.update(path.toString())
|
||||
.update(t.toString())
|
||||
.update(userTag.toString())
|
||||
.digest('hex');
|
||||
|
||||
if (localCalSign != sign) {
|
||||
req.res.status(HttpStatus.FORBIDDEN).json({
|
||||
code: -1,
|
||||
msg: '签名验证失败',
|
||||
});
|
||||
return;
|
||||
}
|
||||
req.res.status(HttpStatus.OK).json({
|
||||
code: 0,
|
||||
msg: 'success',
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user