Skip to content

Commit a89bff0

Browse files
committed
Reduce filecount
1 parent 065b6a4 commit a89bff0

6 files changed

Lines changed: 47 additions & 56 deletions

File tree

‎API/Services/OAuth/IOAuthBuilder.cs‎

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

‎API/Services/OAuth/IOAuthHandlerRegistry.cs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ namespace OpenShock.API.Services.OAuth;
55
public interface IOAuthHandlerRegistry
66
{
77
string[] ListProviderKeys();
8-
bool TryGet(string key, out IOAuthHandler handler);
98
Task<OneOf<Uri, OAuthErrorResult, OAuthProviderNotSupported>> StartAuthorizeAsync(HttpContext http, string provider, OAuthFlow flow, string returnTo);
109
Task<OneOf<OAuthCallbackResult, OAuthErrorResult, OAuthProviderNotSupported>> HandleCallbackAsync(HttpContext http, string provider, IQueryCollection query);
1110
}

‎API/Services/OAuth/OAuthBuilder.cs‎

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

‎API/Services/OAuth/OAuthHandlerRegistry.cs‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,14 @@ public string[] ListProviderKeys()
1919
return _handlers.Keys.ToArray();
2020
}
2121

22-
public bool TryGet(string key, out IOAuthHandler handler)
23-
{
24-
return _handlers.TryGetValue(key, out handler!);
25-
}
26-
2722
private Uri GetCallbackUri(string provider)
2823
{
2924
return new Uri($"https://api.openshock.app/1/oauth/{provider}/callback");
3025
}
3126

3227
public async Task<OneOf<Uri, OAuthErrorResult, OAuthProviderNotSupported>> StartAuthorizeAsync(HttpContext http, string provider, OAuthFlow flow, string returnTo)
3328
{
34-
if (!TryGet(provider, out var handler))
29+
if (!_handlers.TryGetValue(provider, out var handler))
3530
return new OAuthProviderNotSupported();
3631

3732
// Generate state and persist in Redis (+ double-submit cookie inside store)
@@ -53,7 +48,7 @@ public async Task<OneOf<Uri, OAuthErrorResult, OAuthProviderNotSupported>> Start
5348

5449
public async Task<OneOf<OAuthCallbackResult, OAuthErrorResult, OAuthProviderNotSupported>> HandleCallbackAsync(HttpContext http, string provider, IQueryCollection query)
5550
{
56-
if (!TryGet(provider, out var handler))
51+
if (!_handlers.TryGetValue(provider, out var handler))
5752
return new OAuthProviderNotSupported();
5853

5954
var result = await handler.HandleCallbackAsync(http, query, GetCallbackUri(provider));

‎API/Services/OAuth/OAuthServiceCollectionExtensions.cs‎

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Microsoft.Extensions.DependencyInjection.Extensions;
2+
3+
namespace OpenShock.API.Services.OAuth;
4+
5+
public interface IOAuthBuilder
6+
{
7+
IOAuthBuilder AddHandler<THandler, TOptions>(IConfiguration configuration)
8+
where THandler : class, IOAuthHandler
9+
where TOptions : class;
10+
}
11+
12+
internal sealed class OAuthBuilder : IOAuthBuilder
13+
{
14+
private readonly IServiceCollection _services;
15+
internal OAuthBuilder(IServiceCollection services) => _services = services;
16+
17+
public IOAuthBuilder AddHandler<THandler, TOptions>(IConfiguration configuration)
18+
where THandler : class, IOAuthHandler
19+
where TOptions : class
20+
{
21+
_services.Configure<TOptions>(configuration);
22+
23+
// Typed HttpClient per handler (unique type = unique client)
24+
_services.AddHttpClient<THandler>();
25+
26+
// Register handler as IOAuthHandler
27+
_services.AddSingleton<IOAuthHandler, THandler>();
28+
29+
return this;
30+
}
31+
}
32+
33+
public static class ServiceCollectionHelpers
34+
{
35+
public static IOAuthBuilder AddOAuth(this IServiceCollection services)
36+
{
37+
// Default state store if none registered
38+
services.TryAddSingleton<IOAuthStateStore, RedisOAuthStateStore>();
39+
40+
// Registry built from IEnumerable<IOAuthHandler>
41+
services.TryAddSingleton<IOAuthHandlerRegistry, OAuthHandlerRegistry>();
42+
43+
return new OAuthBuilder(services);
44+
}
45+
}

0 commit comments

Comments
 (0)