This commit is contained in:
Jay
2021-09-01 19:30:21 +08:00
parent 9174b540fd
commit c0faaf1599
17 changed files with 145 additions and 476 deletions
+14 -13
View File
@@ -1,14 +1,14 @@
const { resp } = require('src/utils/response/index.js');
const redis = require('src/utils/redis.js');
const sso = require('src/utils/sso/index.js');
const { OPENID_EXPIRE } = require('src/constants/index.js');
const uuid = require('uuid');
const url = require('url');
const { resp } = require("src/utils/response/index.js");
const { get: getCacheInstance } = require("src/utils/cache.js");
const sso = require("src/utils/sso/index.js");
const { OPENID_EXPIRE } = require("src/constants/index.js");
const uuid = require("uuid");
const url = require("url");
const controller = {};
module.exports = controller;
controller.loginSSO = () => async ctx => {
controller.loginSSO = () => async (ctx) => {
const { back_url: backURL } = ctx.query;
const state = uuid.v4();
@@ -16,17 +16,18 @@ controller.loginSSO = () => async ctx => {
const authURL = sso.getAuthURL(state);
// store back url to cache
const cacheKey = redis.Key.ssoLoginCache(state);
const cacheKey = `login-${state}`;
const cache = getCacheInstance();
await redis.set(cacheKey, JSON.stringify({ back_url: backURL }), 'EX', OPENID_EXPIRE);
cache.set(cacheKey, JSON.stringify({ back_url: backURL }), true);
const u = new url.URL(authURL);
ctx.resp(resp.Success, { url: u.toString() });
};
controller.logout = () => async ctx => {
let link = '';
controller.logout = () => async (ctx) => {
let link = "";
if (ctx.token.sso) {
link = sso.getLogoutURL();
@@ -35,6 +36,6 @@ controller.logout = () => async ctx => {
ctx.resp(resp.Success, { url: link });
};
controller.getInfo = () => async ctx => {
controller.getInfo = () => async (ctx) => {
ctx.resp(resp.Success, {});
};
};
+25 -18
View File
@@ -1,32 +1,33 @@
const debug = require('debug')('ctrl:common');
const util = require('util');
const url = require('url');
const sso = require('src/utils/sso/index.js');
const redis = require('src/utils/redis.js');
const { codeMessage, APIError } = require('src/utils/response/index.js');
const config = require('src/config/index.js');
const { jwt } = require('src/utils/pkgs.js');
const debug = require("debug")("ctrl:common");
const util = require("util");
const url = require("url");
const sso = require("src/utils/sso/index.js");
const { get: getCacheInstance } = require("src/utils/cache.js");
const { codeMessage, APIError } = require("src/utils/response/index.js");
const config = require("src/config/index.js");
const { jwt } = require("src/utils/pkgs.js");
const controller = {};
module.exports = controller;
controller.verifyCode = () => async ctx => {
controller.verifyCode = () => async (ctx) => {
const { code, session_state: sessionState, state } = ctx.query;
// logout flow redirect tot frontend
if (state === 'logout') {
if (state === "logout") {
ctx.redirect(config.server.frontend_url);
return;
}
// get back url from redis
const cacheKey = redis.Key.ssoLoginCache(state);
const cacheKey = `login-${state}`;
const cache = getCacheInstance();
const data = await redis.get(cacheKey);
if (!data) ctx.throw('get login cache fail');
const data = cache.get(cacheKey);
if (!data) ctx.throw("get login cache fail");
const stateObj = JSON.parse(data);
const { back_url: backURL } = stateObj;
if (!backURL) ctx.throw('cache data missing');
if (!backURL) ctx.throw("cache data missing");
const u = new url.URL(backURL);
@@ -42,14 +43,17 @@ controller.verifyCode = () => async ctx => {
config.server.jwt_secret,
{
expiresIn: config.server.jwt_expire,
issuer: 'lawsnote',
issuer: "lawsnote",
}
);
u.searchParams.append('success', Buffer.from(JSON.stringify({ token: jwtToken })).toString('base64'));
u.searchParams.append(
"success",
Buffer.from(JSON.stringify({ token: jwtToken })).toString("base64")
);
try {
await redis.del(cacheKey);
cache.del(cacheKey);
} catch (err) {
debug(`delete cache fail: ${util.inspect(err, false, null)}`);
}
@@ -66,7 +70,10 @@ controller.verifyCode = () => async ctx => {
errObj.errorStack = err.stack;
errObj.errorMessage = err.message;
u.searchParams.append('error', Buffer.from(JSON.stringify(errObj)).toString('base64'));
u.searchParams.append(
"error",
Buffer.from(JSON.stringify(errObj)).toString("base64")
);
}
ctx.redirect(u.toString());