Skip to content

Commit ff4e03c

Browse files
committed
Cleanup Not Needed ResourceApi Definitions
1 parent dd571b8 commit ff4e03c

15 files changed

Lines changed: 60 additions & 227 deletions

File tree

‎samples/blazor-standalone-wasm/CodeBeam.UltimateAuth.Sample.BlazorStandaloneWasm/ResourceApi/ProductApiService.cs‎

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,22 @@ public ProductApiService(HttpClient http)
1212
_http = http;
1313
}
1414

15-
public async Task<List<Product>> GetAllAsync()
15+
private HttpRequestMessage CreateRequest(HttpMethod method, string url, object? body = null)
1616
{
17-
var request = new HttpRequestMessage(HttpMethod.Get, "/api/products");
17+
var request = new HttpRequestMessage(method, url);
1818
request.SetBrowserRequestCredentials(BrowserRequestCredentials.Include);
1919

20+
if (body is not null)
21+
{
22+
request.Content = JsonContent.Create(body);
23+
}
24+
25+
return request;
26+
}
27+
28+
public async Task<List<Product>> GetAllAsync()
29+
{
30+
var request = CreateRequest(HttpMethod.Get, "/api/products");
2031
var response = await _http.SendAsync(request);
2132

2233
if (!response.IsSuccessStatusCode)
@@ -27,7 +38,8 @@ public async Task<List<Product>> GetAllAsync()
2738

2839
public async Task<Product?> GetAsync(int id)
2940
{
30-
var response = await _http.GetAsync($"/api/products/{id}");
41+
var request = CreateRequest(HttpMethod.Get, $"/api/products/{id}");
42+
var response = await _http.SendAsync(request);
3143

3244
if (!response.IsSuccessStatusCode)
3345
return null;
@@ -37,17 +49,30 @@ public async Task<List<Product>> GetAllAsync()
3749

3850
public async Task<Product?> CreateAsync(Product product)
3951
{
40-
var response = await _http.PostAsJsonAsync("/api/products", product);
52+
var request = CreateRequest(HttpMethod.Post, "/api/products", product);
53+
var response = await _http.SendAsync(request);
4154

4255
if (!response.IsSuccessStatusCode)
4356
throw new Exception($"Create failed: {response.StatusCode}");
4457

4558
return await response.Content.ReadFromJsonAsync<Product>();
4659
}
4760

61+
public async Task<Product?> UpdateAsync(int id, Product product)
62+
{
63+
var request = CreateRequest(HttpMethod.Put, $"/api/products/{id}", product);
64+
var response = await _http.SendAsync(request);
65+
66+
if (!response.IsSuccessStatusCode)
67+
throw new Exception($"Update failed: {response.StatusCode}");
68+
69+
return await response.Content.ReadFromJsonAsync<Product>();
70+
}
71+
4872
public async Task DeleteAsync(int id)
4973
{
50-
var response = await _http.DeleteAsync($"/api/products/{id}");
74+
var request = CreateRequest(HttpMethod.Delete, $"/api/products/{id}");
75+
var response = await _http.SendAsync(request);
5176

5277
if (!response.IsSuccessStatusCode)
5378
throw new Exception($"Delete failed: {response.StatusCode}");

‎samples/resource-api/CodeBeam.UltimateAuth.Sample.ResourceApi/Program.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
1111
builder.Services.AddOpenApi();
1212

13-
builder.Services.AddUltimateAuthResourceApi();
13+
builder.Services.AddUltimateAuthResourceApi(o => o.UAuthHubBaseUrl = "https://localhost:6110");
1414

1515
builder.Services.AddCors(options =>
1616
{

‎src/CodeBeam.UltimateAuth.Server/Extensions/ServiceCollectionExtensions.cs‎

Lines changed: 23 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -67,26 +67,17 @@ public static IServiceCollection AddUltimateAuthServer(this IServiceCollection s
6767
return services;
6868
}
6969

70-
public static IServiceCollection AddUltimateAuthResourceApi(this IServiceCollection services, Action<UAuthServerOptions>? configure = null)
70+
public static IServiceCollection AddUltimateAuthResourceApi(this IServiceCollection services, Action<UAuthResourceApiOptions>? configure = null)
7171
{
7272
ArgumentNullException.ThrowIfNull(services);
7373
services.AddUltimateAuth();
7474

75-
//AddUsersInternal(services);
76-
//AddCredentialsInternal(services);
77-
//AddAuthorizationInternal(services);
78-
//AddUltimateAuthPolicies(services);
79-
80-
services.AddOptions<UAuthServerOptions>()
75+
services.AddOptions<UAuthResourceApiOptions>()
8176
.Configure(options =>
8277
{
8378
configure?.Invoke(options);
8479
})
85-
.BindConfiguration("UltimateAuth:Server")
86-
.PostConfigure(options =>
87-
{
88-
options.Endpoints.Authentication = false;
89-
});
80+
.BindConfiguration("UltimateAuth:ResourceApi");
9081

9182
services.AddUltimateAuthResourceInternal();
9283

@@ -126,7 +117,7 @@ private static IServiceCollection AddUltimateAuthServerInternal(this IServiceCol
126117
services.TryAddEnumerable(ServiceDescriptor.Singleton<IAuthorityInvariant, InvalidOrRevokedSessionInvariant>());
127118
services.TryAddEnumerable(ServiceDescriptor.Singleton<IAuthorityInvariant, TenantResolvedInvariant>());
128119

129-
// EVENTS
120+
// Events
130121
services.AddSingleton(sp =>
131122
{
132123
var options = sp.GetRequiredService<IOptions<UAuthServerOptions>>().Value;
@@ -247,9 +238,7 @@ private static IServiceCollection AddUltimateAuthServerInternal(this IServiceCol
247238

248239
services.TryAddScoped<IHubCapabilities, HubCapabilities>();
249240

250-
// -----------------------------
251-
// ENDPOINTS
252-
// -----------------------------
241+
// Endpoints
253242
services.TryAddScoped<AuthFlowEndpointFilter>();
254243
services.TryAddSingleton<IAuthEndpointRegistrar, UAuthEndpointRegistrar>();
255244

@@ -260,9 +249,7 @@ private static IServiceCollection AddUltimateAuthServerInternal(this IServiceCol
260249
services.TryAddScoped<IRefreshEndpointHandler, RefreshEndpointHandler>();
261250
services.TryAddScoped<IPkceEndpointHandler, PkceEndpointHandler>();
262251

263-
// ------------------------------
264-
// ASP.NET CORE INTEGRATION
265-
// ------------------------------
252+
// ASP.NET Core Integration
266253
services.AddAuthentication();
267254

268255
services.PostConfigureAll<AuthenticationOptions>(options =>
@@ -389,17 +376,16 @@ public static IServiceCollection AddUAuthHub(this IServiceCollection services, A
389376
return services;
390377
}
391378

392-
// TODO: This is not true, need to build true pipeline for ResourceApi.
393379
private static IServiceCollection AddUltimateAuthResourceInternal(this IServiceCollection services)
394380
{
381+
// Resource API Specific
395382
services.AddSingleton<IUAuthRuntimeMarker, ResourceRuntimeMarker>();
396383

397384
services.AddScoped<ISessionValidator, RemoteSessionValidator>();
398385
services.AddScoped<IUserAccessor<UserKey>, ResourceUserAccessor<UserKey>>();
399386
services.AddScoped<IAuthContextFactory, ResourceAuthContextFactory>();
400-
401-
services.TryAddScoped<IRefreshTokenValidator, UAuthRefreshTokenValidator>();
402387

388+
// Server & Resource API Shared
403389
services.TryAddScoped<IUserAccessor, UserAccessorBridge>();
404390
services.TryAddScoped<ISessionContextAccessor, SessionContextAccessor>();
405391
services.TryAddScoped<ICurrentUser, HttpContextCurrentUser>();
@@ -413,14 +399,12 @@ private static IServiceCollection AddUltimateAuthResourceInternal(this IServiceC
413399

414400
services.TryAddScoped<ITransportCredentialResolver, TransportCredentialResolver>();
415401
services.TryAddScoped<IDeviceContextFactory, DeviceContextFactory>();
416-
services.TryAddScoped<IAuthStateSnapshotFactory, AuthStateSnapshotFactory>();
417402
services.TryAddScoped<IPrimaryCredentialResolver, PrimaryCredentialResolver>();
418403
services.TryAddScoped<IDeviceResolver, DeviceResolver>();
419404

420405
services.TryAddSingleton<IClock, CodeBeam.UltimateAuth.Server.Infrastructure.SystemClock>();
421406

422-
services.AddHttpContextAccessor();
423-
services.AddAuthentication();
407+
424408

425409
services.TryAddScoped<ITenantResolver, UAuthTenantResolver>();
426410
services.TryAddSingleton<ITenantIdResolver>(sp =>
@@ -446,37 +430,24 @@ private static IServiceCollection AddUltimateAuthResourceInternal(this IServiceC
446430
};
447431
});
448432

449-
services.AddAuthentication();
450-
451-
services.PostConfigureAll<AuthenticationOptions>(options =>
433+
// ASP.NET Core Integration
434+
services.AddHttpContextAccessor();
435+
services.AddAuthentication(options =>
452436
{
453-
options.DefaultAuthenticateScheme ??= UAuthConstants.SchemeDefaults.GlobalScheme;
454-
options.DefaultSignInScheme ??= UAuthConstants.SchemeDefaults.GlobalScheme;
455-
options.DefaultChallengeScheme ??= UAuthConstants.SchemeDefaults.GlobalScheme;
456-
});
457-
458-
services.AddAuthentication().AddUAuthResourceApi();
437+
options.DefaultAuthenticateScheme = UAuthConstants.SchemeDefaults.GlobalScheme;
438+
options.DefaultChallengeScheme = UAuthConstants.SchemeDefaults.GlobalScheme;
439+
})
440+
.AddUAuthResourceApi();
459441
services.AddAuthorization();
460442

461-
services.TryAddSingleton<IRefreshTokenStoreFactory, NotSupportedRefreshTokenStoreFactory>();
462-
services.TryAddSingleton<IUserRoleStoreFactory, NotSupportedUserRoleStoreFactory>();
463-
services.TryAddSingleton<IUAuthPasswordHasher, NotSupportedPasswordHasher>();
464-
services.TryAddSingleton<IIdentifierValidator, NoOpIdentifierValidator>();
465-
services.TryAddSingleton<IAccessPolicyProvider, AllowAllAccessPolicyProvider>();
466-
services.TryAddScoped<IUserClaimsProvider, NoOpUserClaimsProvider>();
467-
services.TryAddSingleton<ITokenHasher, NoOpTokenHasher>();
468-
services.TryAddScoped<IPrimaryUserIdentifierProvider, NoOpPrimaryUserIdentifierProvider>();
469-
services.TryAddScoped<IUserProfileSnapshotProvider, NoOpUserProfileSnapshotProvider>();
470-
services.TryAddScoped<IUserLifecycleSnapshotProvider, NoOpUserLifecycleSnapshotProvider>();
471-
472-
//services.PostConfigureAll<AuthenticationOptions>(options =>
473-
//{
474-
// options.DefaultAuthenticateScheme ??= UAuthConstants.SchemeDefaults.GlobalScheme;
475-
//});
476-
477-
services.AddHttpClient<ISessionValidator, RemoteSessionValidator>(client =>
443+
services.AddHttpClient<ISessionValidator, RemoteSessionValidator>((sp, client) =>
478444
{
479-
client.BaseAddress = new Uri("https://localhost:6110"); // Auth Server
445+
var opts = sp.GetRequiredService<IOptions<UAuthResourceApiOptions>>().Value;
446+
447+
if (string.IsNullOrWhiteSpace(opts.UAuthHubBaseUrl))
448+
throw new InvalidOperationException("UAuthHubBaseUrl is not configured. Add it via UAuthResourceApiOptions.");
449+
450+
client.BaseAddress = new Uri(opts.UAuthHubBaseUrl);
480451
});
481452

482453
return services;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace CodeBeam.UltimateAuth.Server.Options;
2+
3+
public class UAuthResourceApiOptions
4+
{
5+
public string UAuthHubBaseUrl { get; set; } = default!;
6+
}

‎src/CodeBeam.UltimateAuth.Server/ResourceApi/AllowAllAccessPolicyProvider.cs‎

Lines changed: 0 additions & 33 deletions
This file was deleted.

‎src/CodeBeam.UltimateAuth.Server/ResourceApi/NoOpIdentifierValidator.cs‎

Lines changed: 0 additions & 13 deletions
This file was deleted.

‎src/CodeBeam.UltimateAuth.Server/ResourceApi/NoOpPrimaryUserIdentifierProvider.cs‎

Lines changed: 0 additions & 14 deletions
This file was deleted.

‎src/CodeBeam.UltimateAuth.Server/ResourceApi/NoOpRefreshTokenValidator.cs‎

Lines changed: 0 additions & 15 deletions
This file was deleted.

‎src/CodeBeam.UltimateAuth.Server/ResourceApi/NoOpTokenHasher.cs‎

Lines changed: 0 additions & 9 deletions
This file was deleted.

‎src/CodeBeam.UltimateAuth.Server/ResourceApi/NoOpUserClaimsProvider.cs‎

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)