Skip to content
This repository was archived by the owner on Sep 25, 2025. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 43 additions & 40 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<IAuthorizationHandler, PolicyRequirementHandler>();

// Retrieve settings for Azure Storage
var blobServiceUrl = builder.Configuration["AzureStorage:BlobServiceUrl"];
var accountName = builder.Configuration["AzureStorage:AccountName"];
Expand All @@ -34,19 +28,57 @@

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;
});
});
});

// Create StorageSharedKeyCredential and BlobServiceClient
var credential = new StorageSharedKeyCredential(accountName, accountKey);
var blobServiceClient = new BlobServiceClient(new Uri(blobServiceUrl), credential);

// Check Azure Blob Storage connection
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Er dette jeg har lagt til vetta faen hva som skjer med resten av linjene her

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<IBlobService, BlobService>();
builder.Services.AddTransient<IEmailSender, EmailSender>();
builder.Services.Configure<AuthMessageSenderOptions>(builder.Configuration);

// Add CORS policy
// Retrieve URLs from launchSettings.json
Expand Down Expand Up @@ -85,13 +117,6 @@
});
builder.Services.AddScoped<BlobService>();

// Load configurations
builder.Services.Configure<AuthMessageSenderOptions>(
builder.Configuration.GetSection("AuthMessageSenderOptions"));

// Check critical configuration values
ValidateSecrets(builder.Configuration);

var app = builder.Build();

using (var service = app.Services.CreateScope()) {
Expand Down Expand Up @@ -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();