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

@ -34,67 +34,26 @@ 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) {
@ -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": {