update
This commit is contained in:
+40
-50
@@ -1,80 +1,70 @@
|
||||
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 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;
|
||||
const controller = {}
|
||||
module.exports = controller
|
||||
|
||||
controller.verifyCode = () => async (ctx) => {
|
||||
const { code, session_state: sessionState, state } = ctx.query;
|
||||
const { code, session_state: sessionState, state } = ctx.query
|
||||
|
||||
// logout flow redirect tot frontend
|
||||
if (state === "logout") {
|
||||
ctx.redirect(config.server.frontend_url);
|
||||
return;
|
||||
if (state === 'logout') {
|
||||
ctx.redirect(config.server.frontend_url)
|
||||
return
|
||||
}
|
||||
|
||||
// get back url from redis
|
||||
const cacheKey = `login-${state}`;
|
||||
const cache = getCacheInstance();
|
||||
const cacheKey = `login-${state}`
|
||||
const cache = getCacheInstance()
|
||||
|
||||
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");
|
||||
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')
|
||||
|
||||
const u = new url.URL(backURL);
|
||||
const u = new url.URL(backURL)
|
||||
|
||||
try {
|
||||
const token = await sso.getToken(code, sessionState);
|
||||
const token = await sso.getToken(code, sessionState)
|
||||
|
||||
// generate jwt token
|
||||
const jwtToken = jwt.sign(
|
||||
{
|
||||
user_id: token.user_id,
|
||||
sso: true,
|
||||
},
|
||||
config.server.jwt_secret,
|
||||
{
|
||||
expiresIn: config.server.jwt_expire,
|
||||
issuer: "lawsnote",
|
||||
}
|
||||
);
|
||||
// set accessToken/refreshToken cache
|
||||
cache.set(token.access_token, token.refresh_token, false)
|
||||
|
||||
u.searchParams.append(
|
||||
"success",
|
||||
Buffer.from(JSON.stringify({ token: jwtToken })).toString("base64")
|
||||
);
|
||||
'success',
|
||||
Buffer.from(JSON.stringify({ token: token.access_token })).toString('base64')
|
||||
)
|
||||
|
||||
try {
|
||||
cache.del(cacheKey);
|
||||
cache.del(cacheKey)
|
||||
} catch (err) {
|
||||
debug(`delete cache fail: ${util.inspect(err, false, null)}`);
|
||||
debug(`delete cache fail: ${util.inspect(err, false, null)}`)
|
||||
}
|
||||
} catch (err) {
|
||||
debug(`openid verify fail: ${util.inspect(err, false, null)}`);
|
||||
debug(`openid verify fail: ${util.inspect(err, false, null)}`)
|
||||
|
||||
/** @type {object} */
|
||||
const errObj = { ...codeMessage.CodeInternalError };
|
||||
const errObj = { ...codeMessage.CodeInternalError }
|
||||
|
||||
if (err instanceof APIError) {
|
||||
// @ts-ignore
|
||||
Object.assign(errObj, err.object.object);
|
||||
Object.assign(errObj, err.object.object)
|
||||
}
|
||||
|
||||
errObj.errorStack = err.stack;
|
||||
errObj.errorMessage = err.message;
|
||||
errObj.errorStack = err.stack
|
||||
errObj.errorMessage = err.message
|
||||
u.searchParams.append(
|
||||
"error",
|
||||
Buffer.from(JSON.stringify(errObj)).toString("base64")
|
||||
);
|
||||
'error',
|
||||
Buffer.from(JSON.stringify(errObj)).toString('base64')
|
||||
)
|
||||
}
|
||||
|
||||
ctx.redirect(u.toString());
|
||||
};
|
||||
ctx.redirect(u.toString())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user