[feat] Update route format

This commit is contained in:
JasonWu
2021-09-01 15:20:53 +08:00
parent b1e9c5e62a
commit 9174b540fd
11 changed files with 152 additions and 76 deletions
@@ -24,3 +24,17 @@ controller.loginSSO = () => async ctx => {
ctx.resp(resp.Success, { url: u.toString() });
};
controller.logout = () => async ctx => {
let link = '';
if (ctx.token.sso) {
link = sso.getLogoutURL();
}
ctx.resp(resp.Success, { url: link });
};
controller.getInfo = () => async ctx => {
ctx.resp(resp.Success, {});
};
+63 -8
View File
@@ -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();
};
};
-21
View File
@@ -5,24 +5,3 @@ controller.healthCheck = async ctx => {
ctx.body = 'ok';
ctx.status = 200;
};
controller.appleAppSiteAssociation = async ctx => {
ctx.status = 200;
ctx.body = {
applinks: {
details: [
{
appID: 'CL3K9D5FDN.com.lawsnote.college.staging',
paths: ['*'],
},
{
appID: 'CL3K9D5FDN.com.lawsnote.college',
paths: ['*'],
},
],
},
webcredentials: {
apps: ['CL3K9D5FDN.com.lawsnote.college.staging', 'CL3K9D5FDN.com.lawsnote.college'],
},
};
};
+1 -1
View File
@@ -36,7 +36,7 @@ controller.verifyCode = () => async ctx => {
// generate jwt token
const jwtToken = jwt.sign(
{
user_id: `${token}-id`,
user_id: token.user_id,
sso: true,
},
config.server.jwt_secret,