temp: 临时
This commit is contained in:
		
							parent
							
								
									2e2d2b6c3e
								
							
						
					
					
						commit
						acbaa67c22
					
				@ -8,7 +8,8 @@ function AuthToken(redisdb, expiresSeconds) {
 | 
				
			|||||||
  this.defaultExpiresSeconds = expiresSeconds;
 | 
					  this.defaultExpiresSeconds = expiresSeconds;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
AuthToken.prototype.genToken = async function ({ userData, userapp }) {
 | 
					// 创建一条Redis记录,并生成Token
 | 
				
			||||||
 | 
					AuthToken.prototype.genToken = async function ({ userData, userApp }) {
 | 
				
			||||||
  // 生成系统内部的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());
 | 
				
			||||||
@ -21,25 +22,31 @@ AuthToken.prototype.genToken = async function ({ userData, userapp }) {
 | 
				
			|||||||
    createts: Math.floor(Date.now() / 1000),
 | 
					    createts: Math.floor(Date.now() / 1000),
 | 
				
			||||||
    ttl: this.defaultExpiresSeconds
 | 
					    ttl: this.defaultExpiresSeconds
 | 
				
			||||||
  };
 | 
					  };
 | 
				
			||||||
  tokenData.apps[userapp.appid] = userapp;
 | 
					  tokenData.apps[userApp.appid] = userApp;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  await this.tokenDB.set(userToken, JSON.stringify(tokenData), 'EX', this.defaultExpiresSeconds);
 | 
					  await this.tokenDB.set(userToken, JSON.stringify(tokenData), 'EX', this.defaultExpiresSeconds);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return userToken;
 | 
					  return userToken;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
AuthToken.prototype.update = async function (userToken, { userData, userapp }) {
 | 
					// 更新Token数据
 | 
				
			||||||
 | 
					AuthToken.prototype.update = async function (userToken, { userData, userApp, ttl }) {
 | 
				
			||||||
  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);
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (tokenData) {
 | 
					  if (tokenData) {
 | 
				
			||||||
    if (userData) tokenData.userData = userData;
 | 
					    if (userData) tokenData.userData = userData;
 | 
				
			||||||
    if (userapp) tokenData.apps[userapp.appid] = userapp;
 | 
					    if (userApp) tokenData.apps[userApp.appid] = userApp;
 | 
				
			||||||
 | 
					    if (ttl) tokenData.ttl = ttl;
 | 
				
			||||||
    await this.tokenDB.set(userToken, JSON.stringify(tokenData), 'EX', this.defaultExpiresSeconds);
 | 
					    await this.tokenDB.set(userToken, JSON.stringify(tokenData), 'EX', this.defaultExpiresSeconds);
 | 
				
			||||||
 | 
					    return tokenData;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  return null
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 删除Token记录
 | 
				
			||||||
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);
 | 
				
			||||||
@ -50,6 +57,7 @@ AuthToken.prototype.delToken = async function (userToken) {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 检查Token是否存在
 | 
				
			||||||
AuthToken.prototype.checkToken = async function (userToken, updateExpire = true) {
 | 
					AuthToken.prototype.checkToken = async function (userToken, 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);
 | 
				
			||||||
@ -61,13 +69,13 @@ AuthToken.prototype.checkToken = async function (userToken, updateExpire = true)
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (updateExpire) {
 | 
					  if (updateExpire) {
 | 
				
			||||||
    tokenData.expires.ts = Math.floor(Date.now() / 1000);
 | 
					    this.tokenDB.expires(userToken, tokenData.ttl)
 | 
				
			||||||
    this.tokenDB.set(userToken, JSON.stringify(tokenData), 'EX', tokenData.expires.ttl);
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return tokenData;
 | 
					  return tokenData;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 获取Token数据
 | 
				
			||||||
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);
 | 
				
			||||||
@ -84,25 +92,25 @@ AuthToken.prototype.getTokenData = async function (userToken) {
 | 
				
			|||||||
AuthToken.prototype.checkTokenKoaRequest = async function (ctx, next) {
 | 
					AuthToken.prototype.checkTokenKoaRequest = async function (ctx, next) {
 | 
				
			||||||
  let token = ctx.request.body.token
 | 
					  let token = ctx.request.body.token
 | 
				
			||||||
  if (!token) token = ctx.header['authorization']
 | 
					  if (!token) token = ctx.header['authorization']
 | 
				
			||||||
  if (!token) token = token = ctx.header['token']
 | 
					  if (!token) token = ctx.header['token']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (!token) {
 | 
					  if (!token) {
 | 
				
			||||||
    ctx.body = {
 | 
					    ctx.body = { code: 401, msg: 'Need user token.', data: {} };
 | 
				
			||||||
      code: 401, msg: 'Need user token.', data: {}
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
    return;
 | 
					    return;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  let tokenData = await this.checkToken(token, "");
 | 
					  let tokenData = await this.checkToken(token);
 | 
				
			||||||
  if (!tokenData) {
 | 
					  if (!tokenData) {
 | 
				
			||||||
    ctx.body = {
 | 
					    ctx.body = { code: 401, msg: 'User token not exist.', data: {} };
 | 
				
			||||||
      code: 401, msg: 'Need user token.', data: {}
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
    return;
 | 
					    return;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  ctx.userData = tokenData.userData;
 | 
					  ctx.userData = tokenData.userData;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  let appid = ctx.request.body.appid;
 | 
				
			||||||
 | 
					  if (!appid) appid = ctx.header['appid'];
 | 
				
			||||||
 | 
					  if (appid) ctx.userApp = tokenData.apps[appid];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return next();
 | 
					  return next();
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user