npm-package/auth/index.js

131 lines
3.2 KiB
JavaScript
Raw Normal View History

2019-08-28 14:08:14 +08:00
'use strict';
const crypto = require('crypto');
const Redis = require('ioredis');
2020-12-04 14:31:54 +08:00
function AuthToken(redisdb, expiresSeconds) {
2019-09-04 09:00:31 +08:00
this.tokenDB = redisdb;
2020-12-04 14:31:54 +08:00
this.defaultExpiresSeconds = expiresSeconds;
2019-08-28 14:08:14 +08:00
}
2020-12-04 14:32:39 +08:00
// 创建一条Redis记录并生成Token
AuthToken.prototype.genToken = async function ({ userData, userApp }) {
2019-08-28 14:08:14 +08:00
// 生成系统内部的user token
let hash = crypto.createHash('md5');
hash.update(JSON.stringify(userData) + Date() + Math.random());
2019-08-28 14:08:14 +08:00
let userToken = hash.digest('hex');
// 缓存到redis
let tokenData = {
userData: userData,
2020-12-04 14:31:54 +08:00
apps: {},
createts: Math.floor(Date.now() / 1000),
ttl: this.defaultExpiresSeconds
2019-08-28 14:08:14 +08:00
};
2020-12-04 14:32:39 +08:00
tokenData.apps[userApp.appid] = userApp;
2020-12-04 14:31:54 +08:00
await this.tokenDB.set(userToken, JSON.stringify(tokenData), 'EX', this.defaultExpiresSeconds);
2019-08-28 14:08:14 +08:00
return userToken;
};
2020-12-04 14:32:39 +08:00
// 更新Token数据
AuthToken.prototype.update = async function (userToken, { userData, userApp, ttl }) {
2020-12-04 14:31:54 +08:00
let tokenData = await this.tokenDB.get(userToken).then(function (data) {
return JSON.parse(data);
});
if (tokenData) {
if (userData) tokenData.userData = userData;
2020-12-04 14:32:39 +08:00
if (userApp) tokenData.apps[userApp.appid] = userApp;
if (ttl) tokenData.ttl = ttl;
2020-12-04 14:31:54 +08:00
await this.tokenDB.set(userToken, JSON.stringify(tokenData), 'EX', this.defaultExpiresSeconds);
2020-12-04 14:32:39 +08:00
return tokenData;
2020-12-04 14:31:54 +08:00
}
2020-12-04 14:32:39 +08:00
return null
2020-12-04 14:31:54 +08:00
};
2020-12-04 14:32:39 +08:00
// 删除Token记录
2019-08-28 14:08:14 +08:00
AuthToken.prototype.delToken = async function (userToken) {
2020-06-29 13:18:38 +08:00
let tokenData = await this.tokenDB.get(userToken).then(function (data) {
2019-08-28 14:08:14 +08:00
return JSON.parse(data);
});
if (tokenData) {
this.tokenDB.del(userToken);
}
};
2020-12-04 14:32:39 +08:00
// 检查Token是否存在
2020-12-04 14:31:54 +08:00
AuthToken.prototype.checkToken = async function (userToken, updateExpire = true) {
2020-06-29 13:18:38 +08:00
let tokenData = await this.tokenDB.get(userToken).then(function (data) {
2019-08-28 14:08:14 +08:00
return JSON.parse(data);
});
// token不存在
if (!tokenData) {
2020-06-29 13:18:38 +08:00
return null;
2019-08-28 14:08:14 +08:00
}
2020-06-29 13:18:38 +08:00
if (updateExpire) {
2020-12-04 14:32:39 +08:00
this.tokenDB.expires(userToken, tokenData.ttl)
2020-06-29 13:18:38 +08:00
}
2019-08-28 14:08:14 +08:00
2020-06-29 13:18:38 +08:00
return tokenData;
2019-08-28 14:08:14 +08:00
};
2020-12-04 14:32:39 +08:00
// 获取Token数据
2020-06-29 13:18:38 +08:00
AuthToken.prototype.getTokenData = async function (userToken) {
let tokenData = await this.tokenDB.get(userToken).then(function (data) {
2019-08-28 14:08:14 +08:00
return JSON.parse(data);
});
2020-06-29 13:18:38 +08:00
// token不存在
2019-08-28 14:08:14 +08:00
if (!tokenData) {
2020-06-29 13:18:38 +08:00
return null;
}
return tokenData;
};
2020-11-27 16:40:08 +08:00
AuthToken.prototype.checkTokenKoaRequest = async function (ctx, next) {
2020-06-29 13:18:38 +08:00
let token = ctx.request.body.token
if (!token) token = ctx.header['authorization']
2020-12-04 14:32:39 +08:00
if (!token) token = ctx.header['token']
2020-06-29 13:18:38 +08:00
if (!token) {
2020-12-04 14:32:39 +08:00
ctx.body = { code: 401, msg: 'Need user token.', data: {} };
2019-08-28 14:08:14 +08:00
return;
}
2020-12-04 14:32:39 +08:00
let tokenData = await this.checkToken(token);
2020-11-30 23:05:55 +08:00
if (!tokenData) {
2020-12-04 14:32:39 +08:00
ctx.body = { code: 401, msg: 'User token not exist.', data: {} };
return;
}
2020-12-04 14:31:54 +08:00
2019-08-28 14:08:14 +08:00
ctx.userData = tokenData.userData;
2020-12-04 14:32:39 +08:00
let appid = ctx.request.body.appid;
if (!appid) appid = ctx.header['appid'];
if (appid) ctx.userApp = tokenData.apps[appid];
2019-08-28 14:08:14 +08:00
return next();
};
2020-06-29 13:18:38 +08:00
/*
AuthToken.prototype.checkTokenKoaRequestByAgent = async function (ctx, next) {
return this.checkTokenKoaRequest(ctx, ctx.userAgent.source, true, next);
2019-08-28 14:08:14 +08:00
};
2020-06-29 13:18:38 +08:00
*/
2019-08-28 14:08:14 +08:00
2019-09-04 09:00:31 +08:00
let tokenInstance = null;
module.exports = function getTokenInstance(redisdb) {
2019-09-04 09:00:31 +08:00
if (!tokenInstance) {
tokenInstance = new AuthToken(redisdb);
2019-09-04 09:00:31 +08:00
}
return tokenInstance;
};