public override bool AuthorizeHubConnection(HubDescriptor hubDescriptor, IRequest request) { try { var token = request.QueryString.Get("token"); if (string.IsNullOrWhiteSpace(token)) { Logger.Trace("Token not exist"); return(false); } var context = WorkerContext.Build(token); var tokenModel = new TokenModel { Token = token }; var user = _backend.ValidateToken(context, tokenModel); var isValidToken = user != null && user.Success; if (isValidToken) { var userId = user.Body.UserId; request.Environment[Param.UserId] = userId; request.Environment[Param.UserToken] = token; return(true); } Logger.Trace($"Token [{token}] is not valid"); return(false); } catch (Exception e) { Logger.Error(e); return(false); } }
public ResponseModel <User> ValidateToken(IWorkerContext context, TokenModel model) { var key = "validate_" + model.Token; var response = _cache[key] as ResponseModel <User>; if (response == null) { response = _backend.ValidateToken(context, model); _cache.Add(key, response, DateTimeOffset.Now.AddMinutes(5)); } return(response); }
public ResponseModel <User> ValidateToken(IWorkerContext context, TokenModel model) { var key = Pref("ValidateToken_" + model.Token); var userId = _redis.GetDatabase().StringGet(key); if (userId.HasValue) { return(ResponseModel <User> .Ok(new User { UserId = userId })); } var response = _backend.ValidateToken(context, model); if (response.Success) { _redis.GetDatabase() .StringSet(key, response.Body.UserId, TimeSpan.FromMinutes(_validateTokenExpiryInMin)); } return(response); }