auth 1.3.0
This commit is contained in:
		
							parent
							
								
									3306db1ff8
								
							
						
					
					
						commit
						c55a29dfd2
					
				
							
								
								
									
										101
									
								
								auth/index.js
									
									
									
									
									
								
							
							
						
						
									
										101
									
								
								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) {
 | 
			
		||||
 | 
			
		||||
@ -1,6 +1,6 @@
 | 
			
		||||
{
 | 
			
		||||
  "name": "@ehason/auth",
 | 
			
		||||
  "version": "1.2.9",
 | 
			
		||||
  "version": "1.3.0",
 | 
			
		||||
  "description": "User auth lib",
 | 
			
		||||
  "main": "index.js",
 | 
			
		||||
  "scripts": {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user