From 9e9d2812df045a011239aaf94a1e5fbbfb3a3f26 Mon Sep 17 00:00:00 2001
From: yellows111 <ice_ice@email.com>
Date: Tue, 7 Jan 2025 20:27:19 +0000
Subject: [PATCH] Allow OpenID support to be disabled.

[CONFIGBREAK]

Someone else can do staff passwords. I do not care.
---
 config.example.toml       |  2 ++
 server/HTTP/HTTPServer.cs | 70 +++++++++++++++++++++------------------
 server/IConfig.cs         | 25 ++++++++------
 3 files changed, 53 insertions(+), 44 deletions(-)

diff --git a/config.example.toml b/config.example.toml
index 2ee2ff6..632a8bc 100644
--- a/config.example.toml
+++ b/config.example.toml
@@ -34,6 +34,8 @@ Password = "hunter2"
 
 [OpenID]
 # CollabVM3 uses OpenID Connect for authentication. You will need to specify an OpenID provider here.
+# If you do NOT want OpenID support, set this to false.
+Enabled = true
 # If you wish to use Computernewb central authentication, you can use the default URL. Contact an administrator to get a client ID and secret.
 AuthorityURL = "https://oauth.computernewb.com/realms/Computernewb"
 ClientID = "collabvm"
diff --git a/server/HTTP/HTTPServer.cs b/server/HTTP/HTTPServer.cs
index 181be2b..4e09949 100644
--- a/server/HTTP/HTTPServer.cs
+++ b/server/HTTP/HTTPServer.cs
@@ -49,38 +49,40 @@ namespace CollabVM.Server.HTTP {
 				options.SerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
 			});
 			builder.Services.AddCors();
-			builder.Services
-				.AddAuthentication(options => {
-					options.DefaultScheme = "CollabVM";
-					options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
-				})
-				.AddScheme<CollabVMAuthenticationSchemeOptions, CollabVMAuthenticationHandler>("CollabVM", options => {
-					options.OpenIDConfig = openIDConfig;
-					options.Database = db;
-				})
-				.AddOpenIdConnect(options => {
-					options.Authority = openIDConfig.AuthorityURL;
-					options.ClientId = openIDConfig.ClientID;
-					options.ClientSecret = openIDConfig.ClientSecret;
-					options.SignInScheme = "CollabVM";
-					options.ResponseType = OpenIdConnectResponseType.Code;
-					options.CallbackPath = new PathString("/api/v0/auth/openid/callback");
-					options.Scope.Add("profile");
+			if(openIDConfig.Enabled) {
+				builder.Services
+					.AddAuthentication(options => {
+						options.DefaultScheme = "CollabVM";
+						options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
+					})
+					.AddScheme<CollabVMAuthenticationSchemeOptions, CollabVMAuthenticationHandler>("CollabVM", options => {
+						options.OpenIDConfig = openIDConfig;
+						options.Database = db;
+					})
+					.AddOpenIdConnect(options => {
+						options.Authority = openIDConfig.AuthorityURL;
+						options.ClientId = openIDConfig.ClientID;
+						options.ClientSecret = openIDConfig.ClientSecret;
+						options.SignInScheme = "CollabVM";
+						options.ResponseType = OpenIdConnectResponseType.Code;
+						options.CallbackPath = new PathString("/api/v0/auth/openid/callback");
+						options.Scope.Add("profile");
+					});
+				// Authorization policies
+				builder.Services.AddAuthorization(options => {
+					options.AddPolicy("Authenticated", policy => {
+						policy.RequireAuthenticatedUser();
+					});
+					options.AddPolicy("Moderator", policy => {
+						policy.RequireAuthenticatedUser();
+						policy.RequireClaim("rank", [Rank.Moderator.ToClaimString(), Rank.Administrator.ToClaimString()]);
+					});
+					options.AddPolicy("Administrator", policy => {
+						policy.RequireAuthenticatedUser();
+						policy.RequireClaim("rank", Rank.Administrator.ToClaimString());
+					});
 				});
-			// Authorization policies
-			builder.Services.AddAuthorization(options => {
-				options.AddPolicy("Authenticated", policy => {
-					policy.RequireAuthenticatedUser();
-				});
-				options.AddPolicy("Moderator", policy => {
-					policy.RequireAuthenticatedUser();
-					policy.RequireClaim("rank", [Rank.Moderator.ToClaimString(), Rank.Administrator.ToClaimString()]);
-				});
-				options.AddPolicy("Administrator", policy => {
-					policy.RequireAuthenticatedUser();
-					policy.RequireClaim("rank", Rank.Administrator.ToClaimString());
-				});
-			});
+			}
 			app = builder.Build();
 			app.UseRouting();
 			app.UseForwardedHeaders();
@@ -90,8 +92,10 @@ namespace CollabVM.Server.HTTP {
 			app.UseWebSockets();
 			// TODO: Make this more strict
         	app.UseCors(cors => cors.WithOrigins(config.AllowedOrigins).AllowAnyMethod().AllowAnyHeader().AllowCredentials());
-			app.UseAuthentication();
-			app.UseAuthorization();
+			if(openIDConfig.Enabled) {
+				app.UseAuthentication();
+				app.UseAuthorization();
+			}
 			#pragma warning disable ASP0014
 			app.UseEndpoints(endpoints => {
 				RegisterRoutes(endpoints);
diff --git a/server/IConfig.cs b/server/IConfig.cs
index 47fd628..4159f05 100644
--- a/server/IConfig.cs
+++ b/server/IConfig.cs
@@ -132,23 +132,26 @@ namespace CollabVM.Server {
 	}
 
 	public class OpenIDConfig : ConfigElement {
+		public required bool Enabled { get; set; }
 		public required string AuthorityURL { get; set; }
 		public required string ClientID { get; set; }
 		public required string ClientSecret { get; set; }
 		public string? UsernameClaim { get; set; }
 
 		public void Validate(ILogger logger) {
-			if (string.IsNullOrEmpty(AuthorityURL)) {
-				logger.LogCritical("OpenID authority URL is missing.");
-				Environment.Exit(1);
-			}
-			if (string.IsNullOrEmpty(ClientID)) {
-				logger.LogCritical("OpenID client ID is missing.");
-				Environment.Exit(1);
-			}
-			if (string.IsNullOrEmpty(ClientSecret)) {
-				logger.LogCritical("OpenID client secret is missing.");
-				Environment.Exit(1);
+			if(Enabled) {
+				if (string.IsNullOrEmpty(AuthorityURL)) {
+					logger.LogCritical("OpenID authority URL is missing.");
+					Environment.Exit(1);
+				}
+				if (string.IsNullOrEmpty(ClientID)) {
+					logger.LogCritical("OpenID client ID is missing.");
+					Environment.Exit(1);
+				}
+				if (string.IsNullOrEmpty(ClientSecret)) {
+					logger.LogCritical("OpenID client secret is missing.");
+					Environment.Exit(1);
+				}
 			}
 		}
 	}
-- 
2.47.1

