Browse Source

与安心云相互认证

release_0.0.2
yuan_yi 2 years ago
parent
commit
40e0b5194c
  1. 18
      code/api/app/lib/controllers/auth/index.js
  2. 11
      code/api/app/lib/middlewares/authenticator.js
  3. 8
      code/api/app/lib/routes/auth/index.js

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

@ -36,6 +36,7 @@ async function login (ctx, next) {
{ {
authorized: true, authorized: true,
token: token, token: token,
resources: []
} }
); );
@ -88,7 +89,24 @@ async function logout (ctx) {
} }
} }
async function loginAxy (ctx) {
try {
const data = ctx.request.body;
const models = ctx.fs.dc.models;
await models.UserToken.create(data);
ctx.status = 204;
} catch (error) {
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
ctx.status = 400;
ctx.body = {
"message": "IOT AUTH 信息记录失败"
}
}
}
module.exports = { module.exports = {
login, login,
logout, logout,
loginAxy,
}; };

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

@ -70,14 +70,9 @@ 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; 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)) { if (token && tokenFormatRegexp.test(token)) {
try { try {
const tokenRes = await ctx.fs.dc.models.UserToken.findOne({ const expired = await ctx.redis.hget(token, 'expired');
where: { if (expired && moment().valueOf() <= moment(expired).valueOf()) {
token: token, const userInfo = JSON.parse(await ctx.redis.hget(token, 'userInfo'));
expired: { $gte: moment().format('YYYY-MM-DD HH:mm:ss') }
}
});
const { userInfo, expired } = tokenRes;
if (!expired || moment().valueOf() <= moment(expired).valueOf()) {
rslt = { rslt = {
'authorized': userInfo.authorized, 'authorized': userInfo.authorized,
'resources': (userInfo || {}).resources || [], 'resources': (userInfo || {}).resources || [],

8
code/api/app/lib/routes/auth/index.js

@ -13,4 +13,12 @@ module.exports = function (app, router, opts) {
app.fs.api.logAttr['PUT/logout'] = { content: '登出', visible: false }; app.fs.api.logAttr['PUT/logout'] = { content: '登出', visible: false };
router.put('/logout', auth.logout); router.put('/logout', auth.logout);
// 安心云
app.fs.api.logAttr['POST/login/axy'] = { content: '安心云登录信息同步', visible: true };
router.post('/login/axy', auth.loginAxy);
app.fs.api.logAttr['PUT/logout/axy'] = { content: '安心云登出信息同步', visible: false };
router.put('/logout/axy', auth.logout);
}; };

Loading…
Cancel
Save