public static async Task <int> GetUserId(ActionExecutingContext context, BitcornContext dbContext) { var platformHeaders = LockUserAttributeUtils.GetPlatformHeaders(context); switch (platformHeaders.Platform) { case "auth0": return(await dbContext.Auth0Query(platformHeaders.Id).Select(u => u.UserId).FirstOrDefaultAsync()); case "twitch": return(await dbContext.TwitchQuery(platformHeaders.Id).Select(u => u.UserId).FirstOrDefaultAsync()); case "discord": return(await dbContext.DiscordQuery(platformHeaders.Id).Select(u => u.UserId).FirstOrDefaultAsync()); case "twitter": return(await dbContext.TwitterQuery(platformHeaders.Id).Select(u => u.UserId).FirstOrDefaultAsync()); case "reddit": return(await dbContext.RedditQuery(platformHeaders.Id).Select(u => u.UserId).FirstOrDefaultAsync()); default: return(0); } }
public static IQueryable <User> GetUserFromHeader(ActionExecutingContext context, BitcornContext dbContext) { var platformHeaders = LockUserAttributeUtils.GetPlatformHeaders(context); switch (platformHeaders.Platform) { case "auth0": return(dbContext.Auth0Query(platformHeaders.Id)); case "twitch": return(dbContext.TwitchQuery(platformHeaders.Id)); case "discord": return(dbContext.DiscordQuery(platformHeaders.Id)); case "twitter": return(dbContext.TwitterQuery(platformHeaders.Id)); case "reddit": return(dbContext.RedditQuery(platformHeaders.Id)); default: return(null); } }
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { int userId = 0; try { userId = await LockUserAttributeUtils.GetUserId(context, _dbContext); } catch (Exception e) { Console.WriteLine(e); throw; } if (userId == 0) { //let the api deal with unregistered sender await next(); return; } var userLocked = LockedUsers.Contains(userId); if (userLocked) { context.Result = new ContentResult() { StatusCode = 420, Content = JsonConvert.SerializeObject(new { refused = "Server refuses to serve this request: User is locked" }) }; return; } lock (LockedUsers) { LockedUsers.Add(userId); } try { await next(); } finally { lock (LockedUsers) { LockedUsers.Remove(userId); } } }
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { User user = await CacheUserAttribute.ReadUser(_config, _dbContext, context); try { try { var appId = context.HttpContext.GetAppId(_config); if (!string.IsNullOrEmpty(appId)) { var thirdPartyClient = await _dbContext.ThirdPartyClient.AnyAsync(a => a.ClientId == appId); if (thirdPartyClient) { context.Result = new ContentResult() { StatusCode = (int)HttpStatusCode.Forbidden, Content = JsonConvert.SerializeObject(new { refused = "Server refuses to serve this request: invalid headers" }) }; await BITCORNLogger.LogError(_dbContext, new Exception("Forbidden request for app id:" + appId), appId); return; } } } catch (Exception e) { await BITCORNLogger.LogError(_dbContext, e, null); throw e; } if (user == null) { var query = LockUserAttributeUtils.GetUserFromHeader(context, _dbContext); if (query == null) { context.Result = new ContentResult() { StatusCode = (int)HttpStatusCode.BadRequest, Content = JsonConvert.SerializeObject(new { refused = "Server refuses to serve this request: invalid headers" }) }; return; } user = await query.FirstOrDefaultAsync(); if (user != null) { context.HttpContext.Items.Add("user", user); context.HttpContext.Items.Add("usermode", 1); } } } catch (Exception e) { await BITCORNLogger.LogError(_dbContext, e, null); throw; } if (user == null) { //let the api deal with unregistered sender await next(); return; } if (!UserLockCollection.Lock(user)) { context.Result = new ContentResult() { StatusCode = UserLockCollection.UserLockedReturnCode, Content = JsonConvert.SerializeObject(new { refused = "Server refuses to serve this request: User is locked" }) }; return; } try { await next(); } finally { UserLockCollection.Release(user); } }