public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { var appFeature = context.HttpContext.Features.Get <IAppFeature>(); if (appFeature?.App != null && FilterDefinition.Weight > 0) { var stopWatch = Stopwatch.StartNew(); try { var plan = appPlanProvider.GetPlanForApp(appFeature.App); var usage = await usageTracker.GetMonthlyCalls(appFeature.App.Id.ToString(), DateTime.Today); if (plan.MaxApiCalls >= 0 && (usage * 1.1) > plan.MaxApiCalls) { context.Result = new StatusCodeResult(429); return; } await next(); } finally { stopWatch.Stop(); await usageTracker.TrackAsync(appFeature.App.Id.ToString(), FilterDefinition.Weight, stopWatch.ElapsedMilliseconds); } } else { await next(); } }
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { var appName = context.RouteData.Values["app"]?.ToString(); if (!string.IsNullOrWhiteSpace(appName)) { var app = await appProvider.FindAppByNameAsync(appName); if (app == null) { context.Result = new NotFoundResult(); return; } var plan = appPlanProvider.GetPlanForApp(app); var usage = await usageTracker.GetMonthlyCalls(app.Id.ToString(), DateTime.Today); if (plan.MaxApiCalls >= 0 && (usage * 1.1) > plan.MaxApiCalls) { context.Result = new StatusCodeResult(429); return; } context.HttpContext.Features.Set <IAppFeature>(new AppFeature(app)); } await next(); }
public async Task OnAuthorizationAsync(AuthorizationFilterContext context) { var appName = context.RouteData.Values["app"]?.ToString(); if (!string.IsNullOrWhiteSpace(appName)) { var app = await appProvider.FindAppByNameAsync(appName); if (app == null) { context.Result = new NotFoundResult(); return; } var user = context.HttpContext.User; var permission = FindByOpenIdSubject(app, user) ?? FindByOpenIdClient(app, user); if (permission == null) { context.Result = new NotFoundResult(); return; } var plan = appPlanProvider.GetPlanForApp(app); var usage = await usageTracker.GetMonthlyCalls(app.Id.ToString(), DateTime.Today); if (plan.MaxApiCalls >= 0 && (usage * 1.1) > plan.MaxApiCalls) { context.Result = new StatusCodeResult(429); return; } var defaultIdentity = context.HttpContext.User.Identities.First(); switch (permission.Value) { case PermissionLevel.Owner: defaultIdentity.AddClaim(new Claim(defaultIdentity.RoleClaimType, SquidexRoles.AppOwner)); defaultIdentity.AddClaim(new Claim(defaultIdentity.RoleClaimType, SquidexRoles.AppDeveloper)); defaultIdentity.AddClaim(new Claim(defaultIdentity.RoleClaimType, SquidexRoles.AppEditor)); break; case PermissionLevel.Developer: defaultIdentity.AddClaim(new Claim(defaultIdentity.RoleClaimType, SquidexRoles.AppDeveloper)); defaultIdentity.AddClaim(new Claim(defaultIdentity.RoleClaimType, SquidexRoles.AppEditor)); break; case PermissionLevel.Editor: defaultIdentity.AddClaim(new Claim(defaultIdentity.RoleClaimType, SquidexRoles.AppEditor)); break; } context.HttpContext.Features.Set <IAppFeature>(new AppFeature(app)); } }
public async Task <IActionResult> GetMonthlyCalls(string app) { var count = await usageTracker.GetMonthlyCalls(App.Id.ToString(), DateTime.Today); var plan = appPlanProvider.GetPlanForApp(App); return(Ok(new CurrentCallsDto { Count = count, MaxAllowed = plan.MaxApiCalls })); }