Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Altinn.Authorization.sln

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions src/apps/Altinn.AccessManagement/Altinn.AccessManagement.sln

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions src/apps/Altinn.Authorization/Altinn.Authorization.sln

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 20 additions & 2 deletions src/pkgs/Altinn.Authorization.PEP/Altinn.Authorization.PEP.sln

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Altinn.Common.PEP.Authorization;
using Altinn.Common.PEP.Authorization;
using Microsoft.AspNetCore.Authorization;

namespace Altinn.Authorization.PEP.Extensions;
Expand Down Expand Up @@ -41,20 +41,4 @@ public static AuthorizationBuilder AddAltinnPEPResourceAccessPolicy(this Authori
ArgumentException.ThrowIfNullOrEmpty(actionType, nameof(actionType));
return builder.AddPolicy(name, policy => policy.Requirements.Add(new ResourceAccessRequirement(resourceId, actionType)));
}

/// <summary>
/// Adds a scope-based access policy to the authorization builder.
/// </summary>
/// <param name="builder">The <see cref="AuthorizationBuilder"/> to which the policy will be added.</param>
/// <param name="name">The name of the policy.</param>
/// <param name="scopes">An array of scopes required by the policy.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="name"/> is null or empty or if <paramref name="scopes"/> contains null or empty values.</exception>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="scopes"/> is null.</exception>
/// <returns>The updated <see cref="AuthorizationBuilder"/>.</returns>
public static AuthorizationBuilder AddAltinnPEPScopePolicy(this AuthorizationBuilder builder, string name, params string[] scopes)
{
ArgumentException.ThrowIfNullOrEmpty(name, nameof(name));
ArgumentNullException.ThrowIfNull(scopes, nameof(scopes));
return builder.AddPolicy(name, policy => policy.Requirements.Add(new ScopeAccessRequirement(scopes)));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Altinn.Common.PEP.Authorization;
using Altinn.Common.PEP.Authorization;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
Expand All @@ -20,7 +20,6 @@ public static IServiceCollection AddAltinnPEP(this IServiceCollection services)
{
services.TryAddScoped<IAuthorizationHandler, ClaimAccessHandler>();
services.TryAddScoped<IAuthorizationHandler, ResourceAccessHandler>();
services.TryAddScoped<IAuthorizationHandler, ScopeAccessHandler>();
return services;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Nullable>enable</Nullable>
<LangVersion>13.0</LangVersion>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="Altinn.Authorization.PEP.Tests" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Microsoft.AspNetCore.Authorization;

namespace Altinn.Authorization.Scopes;

/// <summary>
/// Represents an authorization handler that can perform authorization based on scope
/// </summary>
internal sealed class AnyOfScopeAuthorizationHandler
: AuthorizationHandler<IAnyOfScopeAuthorizationRequirement>
{
/// <inheritdoc/>
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, IAnyOfScopeAuthorizationRequirement requirement)
{
foreach (var identity in context.User.Identities.Where(static i => string.Equals(i.AuthenticationType, "AuthenticationTypes.Federation")))
{
foreach (var claim in identity.Claims.Where(static c => string.Equals(c.Type, "urn:altinn:scope")))

Check warning on line 16 in src/pkgs/Altinn.Authorization.PEP/src/Altinn.Authorization.Scopes/AnyOfScopeAuthorizationHandler.cs

View workflow job for this annotation

GitHub Actions / ci (app: Authorization) / Analyze

Loops should be simplified using the "Where" LINQ method (https://rules.sonarsource.com/csharp/RSPEC-3267)

Check warning on line 16 in src/pkgs/Altinn.Authorization.PEP/src/Altinn.Authorization.Scopes/AnyOfScopeAuthorizationHandler.cs

View workflow job for this annotation

GitHub Actions / ci (pkg: PEP) / Analyze

Loops should be simplified using the "Where" LINQ method (https://rules.sonarsource.com/csharp/RSPEC-3267)

Check warning on line 16 in src/pkgs/Altinn.Authorization.PEP/src/Altinn.Authorization.Scopes/AnyOfScopeAuthorizationHandler.cs

View workflow job for this annotation

GitHub Actions / ci (pkg: PEP) / Analyze

Loops should be simplified using the "Where" LINQ method (https://rules.sonarsource.com/csharp/RSPEC-3267)
{
if (requirement.AnyOfScopes.Check(claim.Value))
{
context.Succeed(requirement);
return Task.CompletedTask;
}
}
}

foreach (var claim in context.User.Claims.Where(static c => string.Equals(c.Type, "scope")))

Check warning on line 26 in src/pkgs/Altinn.Authorization.PEP/src/Altinn.Authorization.Scopes/AnyOfScopeAuthorizationHandler.cs

View workflow job for this annotation

GitHub Actions / ci (app: Authorization) / Analyze

Loops should be simplified using the "Where" LINQ method (https://rules.sonarsource.com/csharp/RSPEC-3267)

Check warning on line 26 in src/pkgs/Altinn.Authorization.PEP/src/Altinn.Authorization.Scopes/AnyOfScopeAuthorizationHandler.cs

View workflow job for this annotation

GitHub Actions / ci (pkg: PEP) / Analyze

Loops should be simplified using the "Where" LINQ method (https://rules.sonarsource.com/csharp/RSPEC-3267)

Check warning on line 26 in src/pkgs/Altinn.Authorization.PEP/src/Altinn.Authorization.Scopes/AnyOfScopeAuthorizationHandler.cs

View workflow job for this annotation

GitHub Actions / ci (pkg: PEP) / Analyze

Loops should be simplified using the "Where" LINQ method (https://rules.sonarsource.com/csharp/RSPEC-3267)
{
if (requirement.AnyOfScopes.Check(claim.Value))
{
context.Succeed(requirement);
return Task.CompletedTask;
}
}

return Task.CompletedTask;
}
}
Loading
Loading