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