diff --git a/Program.cs b/Program.cs index 7a8b2d8..5e028b1 100644 --- a/Program.cs +++ b/Program.cs @@ -4,9 +4,7 @@ using Microsoft.Extensions.Azure; using Azure.Identity; using Azure.Storage; -using Jamly.Auth.Handlers; using Microsoft.AspNetCore.Identity; -using Microsoft.AspNetCore.Identity.UI.Services; using Microsoft.EntityFrameworkCore; using Jamly.Data; using Jamly.Models; @@ -15,13 +13,9 @@ using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.StaticFiles; using Microsoft.IdentityModel.Tokens; -using Microsoft.AspNetCore.Authorization; -using Jamly.Auth.Requirements; var builder = WebApplication.CreateBuilder(args); -builder.Services.AddSingleton(); - // Retrieve settings for Azure Storage var blobServiceUrl = builder.Configuration["AzureStorage:BlobServiceUrl"]; var accountName = builder.Configuration["AzureStorage:AccountName"]; @@ -34,8 +28,31 @@ builder.Services.AddAuthorization(options => { - options.AddPolicy("IsGameJamAdmin", policy => { - policy.Requirements.Add(new GameJamAdminRequirementsModel()); + options.AddPolicy("OwnsGameJam", policy => { + policy.RequireAssertion(context => + { + if (context.User.IsInRole("Admin")) { + return true; + } + // We need the users EventID claim + var eventClaims = context.User.FindAll("EventID"); + if (eventClaims.IsNullOrEmpty()) + { + return false; + } + if (context.Resource is HttpContext httpCon) { + // We need the game jam event id from the http request through the HttpContext + var eventId = httpCon.Request.RouteValues["id"]; + foreach (var eventClaim in eventClaims) { + if (eventId != null) { + return eventClaim.Value == eventId.ToString(); + } + } + + } + + return false; + }); }); }); @@ -43,10 +60,25 @@ var credential = new StorageSharedKeyCredential(accountName, accountKey); var blobServiceClient = new BlobServiceClient(new Uri(blobServiceUrl), credential); +// Check Azure Blob Storage connection +try +{ + // Attempt to list containers as a connectivity check + await foreach (var container in blobServiceClient.GetBlobContainersAsync()) + { + Console.WriteLine($"Connected to Azure Blob Storage. Found container: {container.Name}"); + break; // We only need one result to confirm the connection + } +} +catch (Exception ex) +{ + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine($"Warning: Failed to connect to Azure Blob Storage. Ensure the configuration is correct. Error: {ex.Message}"); + Console.ResetColor(); +} + builder.Services.AddSingleton(blobServiceClient); // Register BlobServiceClient as singleton builder.Services.AddScoped(); -builder.Services.AddTransient(); -builder.Services.Configure(builder.Configuration); // Add CORS policy // Retrieve URLs from launchSettings.json @@ -85,13 +117,6 @@ }); builder.Services.AddScoped(); -// Load configurations -builder.Services.Configure( - builder.Configuration.GetSection("AuthMessageSenderOptions")); - -// Check critical configuration values -ValidateSecrets(builder.Configuration); - var app = builder.Build(); using (var service = app.Services.CreateScope()) { @@ -135,26 +160,4 @@ pattern: "{controller=Home}/{action=Index}/{id?}"); app.MapRazorPages(); -// Idc if its obsolete it works -app.UseEndpoints(endpoints => -{ - endpoints.MapControllerRoute( - name: "playgame", - pattern: "PlayGame/Play/{id}", - defaults: new { controller = "PlayGame", action = "Play" }); -}); - -app.Run(); - -void ValidateSecrets(IConfiguration configuration) -{ - var sendGridKey = configuration["AuthMessageSenderOptions:SendGridKey"]; - var senderEmail = configuration["AuthMessageSenderOptions:SenderEmail"]; - - if (string.IsNullOrEmpty(sendGridKey) || string.IsNullOrEmpty(senderEmail)) - { - Console.ForegroundColor = ConsoleColor.Yellow; - Console.WriteLine("WARNING: SendGridKey or senderEmail is not set. Emails cannot be sent."); - Console.ResetColor(); - } -} +app.Run(); \ No newline at end of file