Skip to content

Commit

Permalink
More formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
meziantou committed Aug 5, 2024
1 parent 45232f0 commit e935add
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private static void ValidateClassicPolicy(AuthorizationPolicy? policy, ClientCre
Assert.Equal($"{DefaultAudience}:{AuthorizationExtensions.ScopeClaimMapping[scope]}", allowedScope);
});
}

private static void ValidateRequireClientCredentialsPolicy(AuthorizationPolicy? policy)
{
Assert.NotNull(policy);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Concurrent;
using System.Collections.Concurrent;
using System.Net;
using Duende.IdentityServer.Models;
using Duende.IdentityServer.Stores;
Expand Down Expand Up @@ -152,7 +152,7 @@ public async Task Real_Client_Server_Communication()
// Using the classic policy, reading invoices should be successful because we're authenticated with a JWT that has the "invoices" audience and "invoices.read" scope
var readInvoicesResponse = await invoicesReadHttpClient.GetStringAsync("https://invoice-app.local/read-invoices", cts.Token);
Assert.Equal("This protected endpoint is for reading invoices", readInvoicesResponse);

// Using the granular policy, reading invoices should be successful because we're authenticated with a JWT that has the "invoices" audience and "invoices.read" scope
var readInvoicesGranularResponse = await invoicesReadHttpClient.GetStringAsync("https://invoice-app.local/read-invoices-granular", cts.Token);
Assert.Equal("This protected endpoint is for reading invoices", readInvoicesGranularResponse);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using CliWrap;
using CliWrap;
using Meziantou.Framework;

namespace Workleap.Authentication.ClientCredentialsGrant.Tests.OpenAPI;
Expand All @@ -9,7 +9,7 @@ public class OpenApiSecurityDescriptionTests
public async Task Given_API_With_Client_Credentials_Attribute_When_Generating_OpenAPI_Then_Equal_Expected_Document()
{
var solutionPath = GetSolutionPath();

var testsFolder = Path.Combine(solutionPath, "tests");
var projectFolder = Path.Combine(testsFolder, "WebApi.OpenAPI.SystemTest");
var generatedFilePath = Path.Combine(projectFolder, "openapi-v1.yaml");
Expand All @@ -31,12 +31,12 @@ public async Task Given_API_With_Client_Credentials_Attribute_When_Generating_Op

Assert.Equal(expectedFileContent, generatedFileContent, ignoreLineEndingDifferences: true);
}

private static string GetSolutionPath()
{
return GetGitRoot() / "src";
}

private static FullPath GetGitRoot()
{
if (FullPath.CurrentDirectory().TryFindFirstAncestorOrSelf(current => Directory.Exists(current / ".git"), out var root))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections;
using System.Collections;
using Microsoft.AspNetCore.Authorization;
using Workleap.AspNetCore.Authentication.ClientCredentialsGrant;

Expand All @@ -20,7 +20,7 @@ public void GivenInvalidClassicScope_WhenCreate_ThenThrowArgumentException()
var scope = (ClientCredentialsScope)999;
Assert.Throws<ArgumentOutOfRangeException>(() => new RequireClientCredentialsAttribute(scope));
}

[Fact]
public void GivenSinglePermission_WhenCreate_ThenSamePermission()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Security.Claims;
using System.Security.Claims;
using FakeItEasy;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
Expand Down Expand Up @@ -35,21 +35,21 @@ public async Task GivenUserHaveTheRequiredScopesInOneOfClaimType_WhenHandleRequi
// Then
Assert.True(context.HasSucceeded);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task GivenUserHaveOneOfTheRequiredScopes_WhenHandleRequirement_ThenSucceeded(bool usePrefixAudienceFormat)
{
// Given
var expectedAudience = "invoices";

var userClaims = new List<Claim>
{
new("scope", usePrefixAudienceFormat ? $"{expectedAudience}:requiredPermission" : "requiredPermission"),
new("scope", "otherPermission"),
};

var requiredPermission = "requiredPermission";

var context = ConfigureHandlerContext(userClaims, requiredPermission);
Expand All @@ -64,7 +64,7 @@ public async Task GivenUserHaveOneOfTheRequiredScopes_WhenHandleRequirement_Then
// Then
Assert.True(context.HasSucceeded);
}

[Fact]
public async Task GivenUserDoNotHaveTheRequiredScopes_WhenHandleRequirement_ThenNotSucceeded()
{
Expand All @@ -86,12 +86,12 @@ public async Task GivenUserDoNotHaveTheRequiredScopes_WhenHandleRequirement_Then
// Then
Assert.False(context.HasSucceeded);
}

private static RequireClientCredentialsRequirementHandler ConfigureHandler(JwtBearerOptions jwtOptions)
{
var jwtOptionsMonitor = A.Fake<IOptionsMonitor<JwtBearerOptions>>();
A.CallTo(() => jwtOptionsMonitor.Get(ClientCredentialsDefaults.AuthenticationScheme)).Returns(jwtOptions);

return new RequireClientCredentialsRequirementHandler(jwtOptionsMonitor);
}

Expand All @@ -108,7 +108,7 @@ private static AuthorizationHandlerContext ConfigureHandlerContext(List<Claim> c

return new AuthorizationHandlerContext(new[] { new RequireClientCredentialsRequirement() }, user, httpContext);
}

private sealed class EndpointFeature : IEndpointFeature
{
public Endpoint? Endpoint { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public IActionResult SeeCocktail(int id)
{
return this.Ok("Hello World!");
}

[HttpPost]
[Route("/controller-requires-permission")]
[SwaggerOperation(Summary = "This controller method should require the cocktail.buy permission.")]
Expand Down

0 comments on commit e935add

Please sign in to comment.