25 lines
1 KiB
C#
25 lines
1 KiB
C#
namespace ASPTemplate.Backend.Utils;
|
|
|
|
public static class AuthUtil {
|
|
public const string OverrideAuthCli = "--disable-auth";
|
|
private const string InternalUserPrefix = "_";
|
|
private const string GuestUser = $"{InternalUserPrefix}guest";
|
|
private const string FallbackUser = $"{InternalUserPrefix}debug";
|
|
|
|
private static readonly string[] FallbackGroups = { "admin" };
|
|
|
|
public static readonly bool OverrideAuth = Environment.GetCommandLineArgs().Contains(OverrideAuthCli);
|
|
|
|
public static string GetRemoteUser(this HttpContext ctx) =>
|
|
OverrideAuth ? FallbackUser : ctx.Request.Headers.TryGetValue("Remote-User", out var header) ? header.ToString() : GuestUser;
|
|
|
|
public static bool IsAuthenticated(this HttpContext ctx) => OverrideAuth || ctx.Request.Headers.ContainsKey("Remote-User");
|
|
|
|
public static IEnumerable<string> GetRemoteGroups(this HttpContext ctx) {
|
|
if (OverrideAuth)
|
|
return FallbackGroups;
|
|
|
|
return ctx.Request.Headers.TryGetValue("Remote-Groups", out var header) ? header.ToString().Split(",") : Array.Empty<string>();
|
|
}
|
|
}
|