Browse Source

redis 鉴权查询

master
CODE 11 months ago
parent
commit
d079e40d45
  1. 2
      code/api/.vscode/launch.json
  2. 14
      code/api/app/lib/controllers/auth/index.js
  3. 16
      code/api/app/lib/middlewares/authenticator.js

2
code/api/.vscode/launch.json

@ -15,7 +15,7 @@
"args": [
"-p 4200",
"-f http://localhost:4200",
"-g postgres://postgres:123@10.8.30.32:5432/iot_auth",
"-g postgres://postgres:123@10.8.30.75:5432/iot_auth",
"--redisHost 10.8.30.112",
"--redisPort 6379",
"--iotVcmpApi http://localhost:4000"

14
code/api/app/lib/controllers/auth/index.js

@ -110,9 +110,17 @@ async function checkCrossToken (ctx) {
const { token } = ctx.request.body;
let cross = false
const expired = await ctx.redis.hget(token, 'expired');
// 也可以在这里做延时操作 需要同步数据库(也可能安心云)
if (expired && moment().valueOf() <= moment(expired).valueOf()) {
// const expired = await ctx.redis.hget(token, 'expired');
// // 也可以在这里做延时操作 需要同步数据库(也可能安心云)
// if (expired && moment().valueOf() <= moment(expired).valueOf()) {
// cross = true
// }
// 231121 安心云 token统一存至 redis
const authRslt = await ctx.redis.get(token);
if (authRslt) {
cross = true
}

16
code/api/app/lib/middlewares/authenticator.js

@ -71,9 +71,17 @@ let authorizeToken = async function (ctx, token) {
const tokenFormatRegexp = /^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$/g;
if (token && tokenFormatRegexp.test(token)) {
try {
const expired = await ctx.redis.hget(token, 'expired');
if (expired && moment().valueOf() <= moment(expired).valueOf()) {
const userInfo = JSON.parse(await ctx.redis.hget(token, 'userInfo'));
// const expired = await ctx.redis.hget(token, 'expired');
const userInfo = await ctx.redis.get(token);
if (
userInfo
// expired && moment().valueOf() <= moment(expired).valueOf()
) {
// const userInfo = JSON.parse(await ctx.redis.hget(token, 'userInfo'));
userInfo = JSON.parse(userInfo);
rslt = {
'authorized': userInfo.authorized,
'resources': (userInfo || {}).resources || [],
@ -84,7 +92,7 @@ let authorizeToken = async function (ctx, token) {
}
} catch (err) {
const { error } = err.response || {};
ctx.fs.logger.log('[anxinyun]', '[AUTH] failed', (error || {}).message || `cannot GET /users/${token}`);
ctx.fs.logger.log('[IOT AUTH]', '[AUTH] failed', (error || {}).message || `${token}`);
}
}
return rslt;

Loading…
Cancel
Save