auth 1.3.0

This commit is contained in:
lik 2020-06-29 13:18:38 +08:00
parent 3306db1ff8
commit c55a29dfd2
2 changed files with 47 additions and 56 deletions

View File

@ -7,7 +7,7 @@ function AuthToken(redisdb) {
this.tokenDB = redisdb; this.tokenDB = redisdb;
} }
AuthToken.prototype.genToken = async function(userData, userkey, expiresSeconds) { AuthToken.prototype.genToken = async function (userData, userkey, expiresSeconds) {
// 生成系统内部的user token // 生成系统内部的user token
let hash = crypto.createHash('md5'); let hash = crypto.createHash('md5');
hash.update(JSON.stringify(userData) + Date() + Math.random()); hash.update(JSON.stringify(userData) + Date() + Math.random());
@ -17,7 +17,7 @@ AuthToken.prototype.genToken = async function(userData, userkey, expiresSeconds)
let tokenData = { let tokenData = {
userData: userData, userData: userData,
userkey: userkey, userkey: userkey,
expires: {ttl: expiresSeconds, ts: Math.floor(Date.now() / 1000)} expires: { ttl: expiresSeconds, ts: Math.floor(Date.now() / 1000) }
}; };
await this.tokenDB.set(userToken, JSON.stringify(tokenData), 'EX', expiresSeconds); await this.tokenDB.set(userToken, JSON.stringify(tokenData), 'EX', expiresSeconds);
@ -25,7 +25,7 @@ AuthToken.prototype.genToken = async function(userData, userkey, expiresSeconds)
}; };
AuthToken.prototype.delToken = async function (userToken) { AuthToken.prototype.delToken = async function (userToken) {
let tokenData = await this.tokenDB.get(userToken).then(function(data) { let tokenData = await this.tokenDB.get(userToken).then(function (data) {
return JSON.parse(data); return JSON.parse(data);
}); });
@ -34,71 +34,30 @@ AuthToken.prototype.delToken = async function (userToken) {
} }
}; };
AuthToken.prototype.checkToken = async function (userToken, userkey) { AuthToken.prototype.checkToken = async function (userToken, userKey, checkKey = false, updateExpire = true) {
let tokenData = await this.tokenDB.get(userToken).then(function(data) { let tokenData = await this.tokenDB.get(userToken).then(function (data) {
return JSON.parse(data); return JSON.parse(data);
}); });
// token不存在 // token不存在
if (!tokenData) { if (!tokenData) {
return false; return null;
} }
if (userkey != tokenData.userkey) { if (checkKey && userKey != tokenData.userkey) {
return false; return null;
} }
// if (updateExpire) {
tokenData.expires.ts = Math.floor(Date.now() / 1000); tokenData.expires.ts = Math.floor(Date.now() / 1000);
this.tokenDB.set(userToken, JSON.stringify(tokenData), 'EX', tokenData.expires.ttl); this.tokenDB.set(userToken, JSON.stringify(tokenData), 'EX', tokenData.expires.ttl);
return true;
};
AuthToken.prototype.checkTokenKoaRequest = async function (ctx, userkey, next) {
if (!ctx.request.body.token) {
ctx.body = {
result: 'fail', error: { code: 401, msg: 'Need user token.' }, data: {}
};
return;
} }
let tokenData = await this.tokenDB.get(ctx.request.body.token).then(function(data) { return tokenData;
return JSON.parse(data);
});
if (!tokenData) {
ctx.body = {
result: 'fail', error: { code: 401, msg: 'User token error.' }, data: {}
};
return;
}
if (userkey != tokenData.userkey) {
ctx.body = {
result: 'fail', error: { code: 403, msg: 'User token has risk.' }, data: {}
};
return;
}
ctx.userData = tokenData.userData;
//
tokenData.expires.ts = Math.floor(Date.now() / 1000);
this.tokenDB.set(ctx.request.body.token, JSON.stringify(tokenData), 'EX', tokenData.expires.ttl);
return next();
};
AuthToken.prototype.checkTokenKoaRequestPost = async function (ctx, next) {
if (ctx.req.method === 'POST') {
return this.checkTokenKoaRequest(ctx, ctx.userAgent.source, next);
}
next();
}; };
AuthToken.prototype.getTokenData = async function (userToken) { AuthToken.prototype.getTokenData = async function (userToken) {
let tokenData = await this.tokenDB.get(userToken).then(function(data) { let tokenData = await this.tokenDB.get(userToken).then(function (data) {
return JSON.parse(data); return JSON.parse(data);
}); });
@ -110,6 +69,38 @@ AuthToken.prototype.getTokenData = async function (userToken) {
return tokenData; 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) {
ctx.body = {
result: 'fail', error: { code: 401, msg: 'Need user token.' }, data: {}
};
return;
}
let tokenData = await this.checkToken(token, userkey, checkKey);
if (!ret) {
ctx.body = {
result: 'fail', error: { code: 401, msg: 'User token error.' }, data: {}
};
return;
}
ctx.userData = tokenData.userData;
return next();
};
/*
AuthToken.prototype.checkTokenKoaRequestByAgent = async function (ctx, next) {
return this.checkTokenKoaRequest(ctx, ctx.userAgent.source, true, next);
};
*/
let tokenInstance = null; let tokenInstance = null;
module.exports = function getTokenInstance(redisdb) { module.exports = function getTokenInstance(redisdb) {
if (!tokenInstance) { if (!tokenInstance) {

View File

@ -1,6 +1,6 @@
{ {
"name": "@ehason/auth", "name": "@ehason/auth",
"version": "1.2.9", "version": "1.3.0",
"description": "User auth lib", "description": "User auth lib",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {