Skip to content

Commit f23c0ff

Browse files
author
Vincent Wilms
committed
Fixes #260: "Accept license" causes error?
1 parent 2e4407e commit f23c0ff

File tree

5 files changed

+20
-9
lines changed

5 files changed

+20
-9
lines changed

src/Nexus/Core/NexusIdentityProviderExtensions.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,12 @@ public static WebApplication UseNexusIdentityProvider(
127127
.LastOrDefault();
128128

129129
authorization ??= await authorizationManager.CreateAsync(
130-
principal: principal,
131-
subject: subject,
132-
client: (await applicationManager.GetIdAsync(client))!,
133-
type: AuthorizationTypes.Permanent,
134-
scopes: principal.GetScopes());
130+
principal: principal,
131+
subject: subject,
132+
client: (await applicationManager.GetIdAsync(client))!,
133+
type: AuthorizationTypes.Permanent,
134+
scopes: principal.GetScopes()
135+
);
135136

136137
principal.SetAuthorizationId(await authorizationManager.GetIdAsync(authorization));
137138

src/Nexus/Core/PersonalAccessTokenAuthenticationHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ protected async override Task<AuthenticateResult> HandleAuthenticateAsync()
105105
var userIdParts = userId.Split('@', count: 2);
106106
var scheme = userIdParts.Length == 2 ? userIdParts[1] : default;
107107

108-
AuthUtilities.AddEnabledCatalogPatternClaim(principal, scheme, _securityOptions);
108+
AuthUtilities.SetEnabledCatalogPatternClaim(principal, scheme, _securityOptions);
109109
}
110110
}
111111
}

src/Nexus/Services/CatalogManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public async Task<CatalogContainer[]> GetCatalogContainersAsync(
155155
var userIdParts = user.Id.Split('@', count: 2);
156156
var scheme = userIdParts.Length == 2 ? userIdParts[1] : default;
157157

158-
AuthUtilities.AddEnabledCatalogPatternClaim(owner, scheme, _securityOptions);
158+
AuthUtilities.SetEnabledCatalogPatternClaim(owner, scheme, _securityOptions);
159159

160160
/* For each pipeline */
161161
foreach (var (pipelineId, pipeline) in pipelines)

src/Nexus/Services/CustomCookieAuthenticationEvents.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public override Task ValidatePrincipal(CookieValidatePrincipalContext context)
2424
if (scheme is null)
2525
context.RejectPrincipal();
2626

27-
AuthUtilities.AddEnabledCatalogPatternClaim(context.Principal, scheme, _securityOptions);
27+
AuthUtilities.SetEnabledCatalogPatternClaim(context.Principal, scheme, _securityOptions);
2828

2929
return base.ValidatePrincipal(context);
3030
}

src/Nexus/Utilities/AuthUtilities.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,20 @@ namespace Nexus.Utilities;
1313

1414
internal static class AuthUtilities
1515
{
16-
public static void AddEnabledCatalogPatternClaim(ClaimsPrincipal principal, string? scheme, SecurityOptions options)
16+
public static void SetEnabledCatalogPatternClaim(ClaimsPrincipal principal, string? scheme, SecurityOptions options)
1717
{
1818
var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
1919

20+
// Do not store the EnabledCatalogsPattern claim in the cookie: it’s tied to the
21+
// sign-in scheme and should be inherited by the user, not persisted. When a user
22+
// accepts a catalog license, they are re-signed in to refresh the cookie. Since
23+
// the claim has previously been added to the User, it becomes part of the cookie.
24+
// On the next visit, the EnabledCatalogsPattern claim is added again, resulting
25+
// in multiple entries of the same claim. This breaks
26+
// user.GetClaim("EnabledCatalogsPattern"), which correctly expects a single claim
27+
// of a given type. To avoid this we remove all existing instances of the claim.
28+
principal.RemoveClaims(NexusClaimsConstants.ENABLED_CATALOGS_PATTERN_CLAIM);
29+
2030
if (scheme is null)
2131
{
2232
principal.AddClaim(

0 commit comments

Comments
 (0)