Skip to content

Commit 5cc4d72

Browse files
committed
fix(auth): harden OpenIddict client seeding with explicit client type and self-heal for invalid existing clients
1 parent 0afcaa3 commit 5cc4d72

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

src/TaskManagement.Auth/Features/Authorization/Services/OpenIddictClientSeeder.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ public async Task StartAsync(CancellationToken cancellationToken)
2828

2929
foreach (var clientSettings in _clientSettings.Clients)
3030
{
31+
var resolvedClientType = ResolveClientType(clientSettings);
3132
var applicationDescriptor = new OpenIddictApplicationDescriptor
3233
{
3334
ClientId = clientSettings.ClientId,
35+
ClientType = resolvedClientType,
3436
ConsentType = ConsentTypes.Explicit,
3537
DisplayName = clientSettings.DisplayName,
3638
Permissions =
@@ -72,10 +74,34 @@ public async Task StartAsync(CancellationToken cancellationToken)
7274
continue;
7375
}
7476

77+
var existingClientType = await manager.GetClientTypeAsync(existingClient, cancellationToken);
78+
if (string.IsNullOrWhiteSpace(existingClientType))
79+
{
80+
await manager.DeleteAsync(existingClient, cancellationToken);
81+
await manager.CreateAsync(applicationDescriptor, cancellationToken);
82+
continue;
83+
}
84+
7585
await manager.UpdateAsync(existingClient, applicationDescriptor, cancellationToken);
7686
}
7787
}
7888

7989
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
90+
91+
private static string ResolveClientType(ClientSettingsOptions clientSettings)
92+
{
93+
if (string.IsNullOrWhiteSpace(clientSettings.ClientType))
94+
{
95+
return ClientTypes.Public;
96+
}
97+
98+
return clientSettings.ClientType.Trim().ToLowerInvariant() switch
99+
{
100+
"public" => ClientTypes.Public,
101+
"confidential" => ClientTypes.Confidential,
102+
_ => throw new InvalidOperationException(
103+
$"Unsupported OpenIddict client type '{clientSettings.ClientType}' for client '{clientSettings.ClientId}'.")
104+
};
105+
}
80106
}
81107
}

src/TaskManagement.Auth/Infrastructure/Common/Settings/ClientSettings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public class ClientSettings
88
public class ClientSettingsOptions
99
{
1010
public string ClientId { get; set; } = string.Empty;
11+
public string ClientType { get; set; } = string.Empty;
1112
public string DisplayName { get; set; } = string.Empty;
1213
public List<string> RedirectUris { get; set; } = [];
1314
public List<string> PostLogoutRedirectUris { get; set; } = [];

0 commit comments

Comments
 (0)