-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthSetup.cs
More file actions
175 lines (157 loc) · 7.31 KB
/
Copy pathAuthSetup.cs
File metadata and controls
175 lines (157 loc) · 7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using Microsoft.IdentityModel.Tokens;
namespace AdaptiveApi.Api.Auth;
public enum AuthMode { None, Oidc }
public sealed class AuthOptions
{
/// `none` (default — open dev mode) | `oidc`
public string Mode { get; set; } = "none";
public string Authority { get; set; } = "";
public string Audience { get; set; } = "";
public string ClientId { get; set; } = "";
public string ClientSecret { get; set; } = "";
/// Claim name holding the tenant id, if the IdP carries it. Empty falls back to
/// `Seeder.DevTenantId` (single-tenant dev behaviour).
public string TenantClaim { get; set; } = "";
public string AdminRole { get; set; } = "admin";
public bool RequireHttpsMetadata { get; set; } = true;
public string CookieName { get; set; } = "adaptiveapi.session";
public string[] Scopes { get; set; } = new[] { "openid", "profile", "email" };
}
public static class AuthSetup
{
public const string AdminPolicy = "adaptiveapi-admin";
public const string CookieScheme = "adaptiveapi-cookie";
public static AuthMode Configure(WebApplicationBuilder builder)
{
var opts = new AuthOptions();
builder.Configuration.GetSection("AdaptiveApi:Auth").Bind(opts);
var mode = string.Equals(opts.Mode, "oidc", StringComparison.OrdinalIgnoreCase)
? AuthMode.Oidc
: AuthMode.None;
builder.Services.AddSingleton(opts);
if (mode == AuthMode.None)
{
// Still register the auth primitives so `[Authorize]` doesn't throw;
// the admin-policy grants everything under this mode.
builder.Services.AddAuthentication();
builder.Services.AddAuthorizationBuilder()
.AddPolicy(AdminPolicy, p => p.RequireAssertion(_ => true));
return mode;
}
if (string.IsNullOrEmpty(opts.Authority))
throw new InvalidOperationException("AdaptiveApi:Auth:Mode=oidc requires AdaptiveApi:Auth:Authority");
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = CookieScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie(CookieScheme, o =>
{
o.Cookie.Name = opts.CookieName;
o.Cookie.HttpOnly = true;
o.Cookie.SameSite = SameSiteMode.Lax;
o.Cookie.SecurePolicy = opts.RequireHttpsMetadata
? CookieSecurePolicy.Always
: CookieSecurePolicy.SameAsRequest;
o.ExpireTimeSpan = TimeSpan.FromHours(12);
o.SlidingExpiration = true;
})
.AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, o =>
{
o.Authority = opts.Authority;
o.ClientId = opts.ClientId;
o.ClientSecret = string.IsNullOrEmpty(opts.ClientSecret) ? null : opts.ClientSecret;
o.ResponseType = OpenIdConnectResponseType.Code;
o.UsePkce = true;
o.SaveTokens = true;
o.RequireHttpsMetadata = opts.RequireHttpsMetadata;
o.GetClaimsFromUserInfoEndpoint = true;
foreach (var s in opts.Scopes) o.Scope.Add(s);
o.TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "name",
RoleClaimType = "roles",
};
})
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, o =>
{
// Bearer scheme for programmatic admin clients (CI, scripts).
// UI uses the cookie scheme via the OIDC code flow.
o.Authority = opts.Authority;
o.Audience = string.IsNullOrEmpty(opts.Audience) ? opts.ClientId : opts.Audience;
o.RequireHttpsMetadata = opts.RequireHttpsMetadata;
o.TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "sub",
RoleClaimType = "roles",
};
});
builder.Services.AddAuthorizationBuilder()
.AddPolicy(AdminPolicy, p =>
{
p.RequireAuthenticatedUser();
p.AddAuthenticationSchemes(CookieScheme, JwtBearerDefaults.AuthenticationScheme);
if (!string.IsNullOrEmpty(opts.AdminRole))
p.RequireAssertion(ctx =>
ctx.User.IsInRole(opts.AdminRole)
|| ctx.User.HasClaim("role", opts.AdminRole)
|| ctx.User.HasClaim("roles", opts.AdminRole));
});
return mode;
}
/// Wires `/auth/me`, `/auth/login`, `/auth/logout`. Safe no-ops under AuthMode.None.
public static void MapAuthEndpoints(WebApplication app, AuthMode mode, AuthOptions opts)
{
app.MapGet("/admin/auth/me", (HttpContext ctx) =>
{
if (mode == AuthMode.None)
return Results.Ok(new MeDto(Authenticated: false, Mode: "none",
Name: null, Email: null, TenantId: null, Roles: Array.Empty<string>()));
var user = ctx.User;
if (!(user.Identity?.IsAuthenticated ?? false))
return Results.Ok(new MeDto(Authenticated: false, Mode: "oidc",
Name: null, Email: null, TenantId: null, Roles: Array.Empty<string>()));
var roles = user.FindAll(ClaimTypes.Role).Select(c => c.Value)
.Concat(user.FindAll("role").Select(c => c.Value))
.Concat(user.FindAll("roles").Select(c => c.Value))
.Distinct().ToArray();
var tenantId = !string.IsNullOrEmpty(opts.TenantClaim)
? user.FindFirst(opts.TenantClaim)?.Value
: null;
return Results.Ok(new MeDto(
Authenticated: true,
Mode: "oidc",
Name: user.Identity?.Name,
Email: user.FindFirst("email")?.Value ?? user.FindFirst(ClaimTypes.Email)?.Value,
TenantId: tenantId,
Roles: roles));
});
if (mode == AuthMode.Oidc)
{
app.MapGet("/admin/auth/login", (string? returnUrl) =>
Results.Challenge(
new Microsoft.AspNetCore.Authentication.AuthenticationProperties
{ RedirectUri = returnUrl ?? "/" },
new[] { OpenIdConnectDefaults.AuthenticationScheme }));
app.MapPost("/admin/auth/logout", () =>
Results.SignOut(
new Microsoft.AspNetCore.Authentication.AuthenticationProperties
{ RedirectUri = "/" },
new[] { CookieScheme, OpenIdConnectDefaults.AuthenticationScheme }));
}
}
public sealed record MeDto(
bool Authenticated,
string Mode,
string? Name,
string? Email,
string? TenantId,
IReadOnlyList<string> Roles);
}