[feat] Update route format
This commit is contained in:
@@ -3,9 +3,11 @@ const debug = require('debug')('ctrl:common');
|
||||
const util = require('util');
|
||||
const joi = require('joi');
|
||||
const response = require('src/utils/response/index.js');
|
||||
const config = require('src/config/index.js');
|
||||
const { jwt } = require('src/utils/pkgs.js');
|
||||
const { copyObject, toNumber } = require('src/utils/index.js');
|
||||
|
||||
const { Success, InternalError, DataFormat } = response.resp;
|
||||
const { Success, InternalError, DataFormat, Forbidden, Unauthorized } = response.resp;
|
||||
|
||||
const controller = {};
|
||||
module.exports = controller;
|
||||
@@ -101,13 +103,66 @@ controller.validate = schema => {
|
||||
};
|
||||
};
|
||||
|
||||
controller.getAppVersion = () => async (ctx, next) => {
|
||||
// appVersion Format x.y.z (major.minor.patch)
|
||||
const appVersion = ctx.get('x-app-version');
|
||||
const appBuildNumber = toNumber(ctx.get('x-app-buildnumber'), 0);
|
||||
const appPlatform = ctx.get('x-app-platform');
|
||||
/**
|
||||
* @param {boolean=} allowExpired
|
||||
* @return {import('koa').Middleware}
|
||||
*/
|
||||
controller.authorization = allowExpired => {
|
||||
return async (ctx, next) => {
|
||||
ctx.token = {};
|
||||
/** @type {string} */
|
||||
const token = ctx.get('authorization');
|
||||
|
||||
Object.assign(ctx.state, { appVersion, appBuildNumber, appPlatform });
|
||||
if (!token) ctx.err(Unauthorized);
|
||||
|
||||
return next();
|
||||
try {
|
||||
const strs = token.split(/\s/);
|
||||
debug(`Get Header: ${token}`);
|
||||
if (strs.length !== 2 || !/^bearer$/i.test(strs[0])) ctx.err(Unauthorized, response.codeMessage.CodeTokenInvalid);
|
||||
|
||||
[, ctx.token.origin] = strs;
|
||||
|
||||
let decoded = {};
|
||||
let expired = false;
|
||||
|
||||
try {
|
||||
decoded = jwt.verify(strs[1], config.server.jwt_secret);
|
||||
|
||||
await joi
|
||||
.object({
|
||||
user_id: joi.string().required(),
|
||||
})
|
||||
.unknown()
|
||||
.validateAsync(decoded);
|
||||
} catch (err) {
|
||||
debug(`jwt token verify fail: ${util.inspect(err, false, null)}`);
|
||||
if (err instanceof jwt.TokenExpiredError) {
|
||||
decoded = jwt.decode(ctx.token.origin);
|
||||
expired = true;
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
ctx.token.user_id = decoded.user_id;
|
||||
ctx.token.sso = !!decoded.sso;
|
||||
|
||||
if (expired) ctx.err(Forbidden, response.codeMessage.CodeTokenExpired);
|
||||
|
||||
ctx.verified = true;
|
||||
} catch (err) {
|
||||
debug(`Token valid fail: ${util.inspect(err, false, null)}`);
|
||||
if (err instanceof response.APIError) {
|
||||
// 如果是過期的錯誤,判斷是否允許過期存取
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line
|
||||
if (err._object?.object?.code === response.codeMessage.CodeTokenExpired.code) {
|
||||
if (!!allowExpired) return next();
|
||||
}
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
|
||||
return next();
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user