Browse Source

token 鉴权信息保存至 redis

dev
巴林闲侠 2 years ago
parent
commit
dad65bad50
  1. 9
      api/app/lib/controllers/auth/index.js
  2. 30
      api/app/lib/middlewares/authenticator.js

9
api/app/lib/controllers/auth/index.js

@ -33,6 +33,8 @@ async function login (ctx, next) {
) { ) {
throw '当前账号已禁用' throw '当前账号已禁用'
} }
emisLoginRes.authorized = true
emisLoginRes.expired = moment().add(1, 'day')
emisLoginRes.pomsUserInfo = pomsRegisterRes.dataValues emisLoginRes.pomsUserInfo = pomsRegisterRes.dataValues
let userUpdateData = { let userUpdateData = {
@ -60,13 +62,17 @@ async function login (ctx, next) {
ctx.fs.logger.error(`IP GET, error: ${error}`); ctx.fs.logger.error(`IP GET, error: ${error}`);
} }
await models.User.update(userUpdateData, { await models.User.update(userUpdateData, {
where: { where: {
id: emisLoginRes.id id: emisLoginRes.id
} }
}) })
await ctx.redis.hmset(emisLoginRes.token, {
expired: moment().add(1, 'day'),
userInfo:JSON.stringify(emisLoginRes)
});
ctx.status = 200; ctx.status = 200;
ctx.body = emisLoginRes; ctx.body = emisLoginRes;
} }
@ -95,6 +101,7 @@ async function logout (ctx) {
await ctx.app.fs.emisRequest.put('logout', { await ctx.app.fs.emisRequest.put('logout', {
data: params data: params
}) })
await ctx.redisTools.hdelall(token);
ctx.status = 204; ctx.status = 204;
} catch (error) { } catch (error) {

30
api/app/lib/middlewares/authenticator.js

@ -72,24 +72,34 @@ let authorizeToken = async function (ctx, token) {
if (token && tokenFormatRegexp.test(token)) { if (token && tokenFormatRegexp.test(token)) {
try { try {
console.log(`DurationCalc: auth 1 用时 ${moment().diff(startTime, 'milliseconds')}`); console.log(`DurationCalc: auth 1 用时 ${moment().diff(startTime, 'milliseconds')}`);
const authorizeRes = await ctx.app.fs.emisRequest.get('authorize', {
query: { token } const expired = await ctx.redis.hget(token, 'expired');
})
// const authorizeRes = await ctx.app.fs.emisRequest.get('authorize', {
// query: { token }
// })
// const { userInfo, expired } = authorizeRes;
console.log(`DurationCalc: auth 2 用时 ${moment().diff(startTime, 'milliseconds')}`); console.log(`DurationCalc: auth 2 用时 ${moment().diff(startTime, 'milliseconds')}`);
const { userInfo, expired } = authorizeRes;
// TODO 从项企 clickhouse 数据库中查 token 并更新
if (expired && moment().valueOf() <= moment(expired).valueOf()) { if (expired && moment().valueOf() <= moment(expired).valueOf()) {
const pomsUser = await ctx.app.fs.dc.models.User.findOne({ const userInfo = JSON.parse(await ctx.redis.hmget(token, 'userInfo'));
where: { const { pomsUserInfo: pomsUser } = userInfo
pepUserId: userInfo.id
} // const pomsUser = await ctx.app.fs.dc.models.User.findOne({
}) || {} // where: {
// pepUserId: userInfo.id
// }
// }) || {}
console.log(`DurationCalc: auth 3 用时 ${moment().diff(startTime, 'milliseconds')}`); console.log(`DurationCalc: auth 3 用时 ${moment().diff(startTime, 'milliseconds')}`);
rslt = { rslt = {
'authorized': userInfo.authorized, 'authorized': userInfo.authorized,
'resources': (userInfo || {}).resources || [], 'resources': (userInfo || {}).resources || [],
}; };
ctx.fs.api.userId = pomsUser.id; ctx.fs.api.userId = pomsUser.id;
ctx.fs.api.userInfo = pomsUser.dataValues; ctx.fs.api.userInfo = pomsUser;
ctx.fs.api.pepUserId = userInfo.id; ctx.fs.api.pepUserId = userInfo.id;
ctx.fs.api.pepUserInfo = userInfo; ctx.fs.api.pepUserInfo = userInfo;
ctx.fs.api.token = token; ctx.fs.api.token = token;

Loading…
Cancel
Save