npm-package/auth/index.js

111 lines
2.6 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-06-29 13:18:38 +08:00
function AuthToken(redisdb) {
2019-09-04 09:00:31 +08:00
this.tokenDB = redisdb;
2019-08-28 14:08:14 +08:00
}
2020-06-29 13:18:38 +08:00
AuthToken.prototype.genToken = async function (userData, userkey, expiresSeconds) {
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,
userkey: userkey,
2020-06-29 13:18:38 +08:00
expires: { ttl: expiresSeconds, ts: Math.floor(Date.now() / 1000) }
2019-08-28 14:08:14 +08:00
};
2019-08-28 14:25:56 +08:00
await this.tokenDB.set(userToken, JSON.stringify(tokenData), 'EX', expiresSeconds);
2019-08-28 14:08:14 +08:00
return userToken;
};
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-06-29 13:18:38 +08:00
AuthToken.prototype.checkToken = async function (userToken, userKey, checkKey = false, updateExpire = true) {
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 (checkKey && userKey != tokenData.userkey) {
return null;
}
2020-06-29 13:18:38 +08:00
if (updateExpire) {
tokenData.expires.ts = Math.floor(Date.now() / 1000);
this.tokenDB.set(userToken, JSON.stringify(tokenData), 'EX', tokenData.expires.ttl);
}
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-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;
};
AuthToken.prototype.checkTokenKoaRequest = async function (ctx, userkey, checkKey, next) {
let token = ctx.request.body.token
if (!token) token = ctx.header['authorization']
if (!token) token = token = ctx.header['token']
if (!token) {
2019-08-28 14:08:14 +08:00
ctx.body = {
2020-06-29 13:18:38 +08:00
result: 'fail', error: { code: 401, msg: 'Need user token.' }, data: {}
2019-08-28 14:08:14 +08:00
};
return;
}
2020-06-29 13:18:38 +08:00
let tokenData = await this.checkToken(token, userkey, checkKey);
if (!ret) {
ctx.body = {
2020-06-29 13:18:38 +08:00
result: 'fail', error: { code: 401, msg: 'User token error.' }, data: {}
};
return;
}
2020-06-29 13:18:38 +08:00
2019-08-28 14:08:14 +08:00
ctx.userData = tokenData.userData;
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;
};