From c55a29dfd2d2a10d641cc9a5784f9c9248c87b95 Mon Sep 17 00:00:00 2001 From: lik Date: Mon, 29 Jun 2020 13:18:38 +0800 Subject: [PATCH] auth 1.3.0 --- auth/index.js | 101 +++++++++++++++++++++------------------------- auth/package.json | 2 +- 2 files changed, 47 insertions(+), 56 deletions(-) diff --git a/auth/index.js b/auth/index.js index 275e062..1f5c6ce 100644 --- a/auth/index.js +++ b/auth/index.js @@ -3,11 +3,11 @@ const crypto = require('crypto'); const Redis = require('ioredis'); -function AuthToken(redisdb) { +function AuthToken(redisdb) { this.tokenDB = redisdb; } -AuthToken.prototype.genToken = async function(userData, userkey, expiresSeconds) { +AuthToken.prototype.genToken = async function (userData, userkey, expiresSeconds) { // 生成系统内部的user token let hash = crypto.createHash('md5'); hash.update(JSON.stringify(userData) + Date() + Math.random()); @@ -17,7 +17,7 @@ AuthToken.prototype.genToken = async function(userData, userkey, expiresSeconds) let tokenData = { userData: userData, 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); @@ -25,7 +25,7 @@ AuthToken.prototype.genToken = async function(userData, userkey, expiresSeconds) }; 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); }); @@ -34,71 +34,30 @@ AuthToken.prototype.delToken = async function (userToken) { } }; -AuthToken.prototype.checkToken = async function (userToken, userkey) { - let tokenData = await this.tokenDB.get(userToken).then(function(data) { +AuthToken.prototype.checkToken = async function (userToken, userKey, checkKey = false, updateExpire = true) { + let tokenData = await this.tokenDB.get(userToken).then(function (data) { return JSON.parse(data); }); // token不存在 if (!tokenData) { - return false; + return null; } - if (userkey != tokenData.userkey) { - return false; + if (checkKey && userKey != tokenData.userkey) { + return null; } - // - tokenData.expires.ts = Math.floor(Date.now() / 1000); - 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; + if (updateExpire) { + tokenData.expires.ts = Math.floor(Date.now() / 1000); + this.tokenDB.set(userToken, JSON.stringify(tokenData), 'EX', tokenData.expires.ttl); } - let tokenData = await this.tokenDB.get(ctx.request.body.token).then(function(data) { - 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(); + return tokenData; }; 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); }); @@ -110,6 +69,38 @@ AuthToken.prototype.getTokenData = async function (userToken) { 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; module.exports = function getTokenInstance(redisdb) { if (!tokenInstance) { diff --git a/auth/package.json b/auth/package.json index cee0a9e..5678bec 100644 --- a/auth/package.json +++ b/auth/package.json @@ -1,6 +1,6 @@ { "name": "@ehason/auth", - "version": "1.2.9", + "version": "1.3.0", "description": "User auth lib", "main": "index.js", "scripts": {