Browse Source

app check

release_1.3.0
巴林闲侠 3 years ago
parent
commit
4c0d85ce14
  1. 124
      code/VideoAccess-VCMP/api/app/lib/controllers/application/index.js
  2. 11
      code/VideoAccess-VCMP/api/app/lib/routes/application/index.js
  3. 52
      code/VideoAccess-VCMP/api/app/lib/utils/oauth2.js

124
code/VideoAccess-VCMP/api/app/lib/controllers/application/index.js

@ -4,76 +4,78 @@ const moment = require('moment')
const uuid = require('uuid'); const uuid = require('uuid');
async function check (ctx) { async function check (ctx) {
try { try {
const { models } = this.fs.dc; const { models } = this.fs.dc;
const { appKey, appSecret } = this.request.body; const { Authorization } = ctx.headers;
const existRes = await models.Application.findOne({ const { utils: { oauthParseAuthHeader, oauthParseBody } } = ctx.app.fs
where: { const keySplit = await oauthParseAuthHeader(Authorization);
appKey: appKey, const existRes = await models.Application.findOne({
appSecret: appSecret, where: {
} appKey: keySplit[0],
}) appSecret: keySplit[1],
if (!existRes) { }
throw '应用不存在' })
} else if (existRes.forbidden) { if (!existRes) {
throw '应用已被禁用' throw '应用不存在'
} } else if (existRes.forbidden) {
ctx.status = 204; throw '应用已被禁用'
} catch (error) { }
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 204;
ctx.status = 400; } catch (error) {
ctx.body = ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
typeof error == 'string' ? { ctx.status = 400;
message: error ctx.body =
} : error typeof error == 'string' ? {
} message: error
} : error
}
} }
async function edit (ctx, next) { async function edit (ctx, next) {
let errMsg = '创建应用失败' let errMsg = '创建应用失败'
const transaction = await ctx.fs.dc.orm.transaction(); const transaction = await ctx.fs.dc.orm.transaction();
try { try {
const { models } = ctx.fs.dc; const { models } = ctx.fs.dc;
const { userId } = ctx.fs.api const { userId } = ctx.fs.api
const data = ctx.request.body; const data = ctx.request.body;
if (data.id) { if (data.id) {
// 修改 // 修改
const storageData = Object.assign({}, data,) const storageData = Object.assign({}, data,)
await models.Application.update(storageData, { await models.Application.update(storageData, {
where: { where: {
id: data.id id: data.id
}, },
transaction transaction
}) })
} else { } else {
// 添加 // 添加
const storageData = Object.assign({}, data, { const storageData = Object.assign({}, data, {
appKey: uuid.v4(), appKey: uuid.v4(),
appSecret: uuid.v4(), appSecret: uuid.v4(),
createUserId: userId, createUserId: userId,
createTime: moment().format(), createTime: moment().format(),
forbidden: true forbidden: true
}) })
await models.Application.create(storageData, { transaction }) await models.Application.create(storageData, { transaction })
} }
await transaction.commit(); await transaction.commit();
ctx.status = 204; ctx.status = 204;
} catch (error) { } catch (error) {
await transaction.rollback(); await transaction.rollback();
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400; ctx.status = 400;
ctx.body = { ctx.body = {
message: errMsg message: errMsg
} }
} }
} }
module.exports = { module.exports = {
check, check,
edit, edit,
}; };

11
code/VideoAccess-VCMP/api/app/lib/routes/application/index.js

@ -5,10 +5,13 @@ const application = require('../../controllers/application');
module.exports = function (app, router, opts) { module.exports = function (app, router, opts) {
// app.fs.api.logAttr['GET/application'] = { content: '获取应用信息', visible: false }; app.fs.api.logAttr['GET/application/check'] = { content: '检查应用状态', visible: false };
// router.get('/application', application.get); router.get('/application/check', application.check);
app.fs.api.logAttr['POST/application'] = { content: '创建/修改应用', visible: false }; // app.fs.api.logAttr['GET/application'] = { content: '获取应用信息', visible: false };
router.post('/application', application.edit); // router.get('/application', application.get);
app.fs.api.logAttr['POST/application'] = { content: '创建/修改应用', visible: false };
router.post('/application', application.edit);
}; };

52
code/VideoAccess-VCMP/api/app/lib/utils/oauth2.js

@ -0,0 +1,52 @@
module.exports = function (app, opts) {
async function oauthParseAuthHeader (auth) {
if (!auth) {
throw new Error('参数无效: 未包含Authorization头');
}
const authSplit = auth.split('Basic');
if (authSplit.length != 2) {
throw new Error('参数无效: Authorization头格式无效,请检查是否包含了"Basic "');
}
const authCode = authSplit[1];
const apikey = Buffer.from(authCode, 'base64').toString();
const keySplit = apikey.split(':');
if (keySplit.length != 2) {
throw new Error('参数无效:请检查Authorization头内容是否经过正确Base64编码');
}
return keySplit;
}
async function oauthParseBody (body, type) {
let checked = true, token = '';
if (type == 'apply' && body['grant_type'] != 'client_credentials') {
checked = false;
} else if (type == 'refresh') {
if (body['grant_type'] != 'refresh_token' || body['token'] == null) {
checked = false;
} else {
token = body['token'];
}
} else if (type == 'invalidate') {
if (body['token'] == null) {
checked = false;
} else {
token = body['token'];
}
}
if (!checked) {
throw new Error('参数无效:请求正文中未包含正确的信息');
}
return token;
}
return {
oauthParseAuthHeader,
oauthParseBody
}
}
Loading…
Cancel
Save