Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit 4fce219

Browse files
authoredJul 20, 2023
Merge pull request #39 from DuendeSoftware/brock/case-sensitive-authz-scheme
use case sensitive values for authorization scheme
2 parents f92e199 + e50bcf6 commit 4fce219

9 files changed

+118
-1
lines changed
 

‎src/Duende.AccessTokenManagement/AccessTokenHandler.cs

+12
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,18 @@ protected virtual async Task SetTokenAsync(HttpRequestMessage request, bool forc
102102
}
103103
}
104104

105+
// since AccessTokenType above in the token endpoint response (the token_type value) could be case insensitive, but
106+
// when we send it as an Authoriization header in the API request it must be case sensitive, we
107+
// are checking for that here and forcing it to the exact casing required.
108+
if (scheme.Equals(AuthenticationSchemes.AuthorizationHeaderBearer, System.StringComparison.OrdinalIgnoreCase))
109+
{
110+
scheme = AuthenticationSchemes.AuthorizationHeaderBearer;
111+
}
112+
else if (scheme.Equals(AuthenticationSchemes.AuthorizationHeaderDPoP, System.StringComparison.OrdinalIgnoreCase))
113+
{
114+
scheme = AuthenticationSchemes.AuthorizationHeaderDPoP;
115+
}
116+
105117
// checking for null AccessTokenType and falling back to "Bearer" since this might be coming
106118
// from an old cache/store prior to adding the AccessTokenType property.
107119
request.SetToken(scheme, token.AccessToken);

‎src/Duende.AccessTokenManagement/DPoPExtensions.cs

-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,5 @@ public static bool IsDPoPError(this HttpResponseMessage response)
7979
public static string GetDPoPUrl(this HttpRequestMessage request)
8080
{
8181
return request.RequestUri!.Scheme + "://" + request.RequestUri!.Authority + request.RequestUri!.LocalPath;
82-
return request.RequestUri!.Scheme + "://" + request.RequestUri!.Authority + request.RequestUri!.AbsolutePath;
8382
}
8483
}

‎test/Tests/AccessTokenHandlerTests.cs

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright (c) Duende Software. All rights reserved.
2+
// See LICENSE in the project root for license information.
3+
4+
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.Extensions.Logging;
6+
using RichardSzalay.MockHttp;
7+
8+
namespace Duende.AccessTokenManagement.Tests;
9+
10+
public class AccessTokenHandlerTests
11+
{
12+
TestDPoPProofService _testDPoPProofService = new TestDPoPProofService();
13+
TestHttpMessageHandler _testHttpMessageHandler = new TestHttpMessageHandler();
14+
15+
AccessTokenHandlerSubject _subject;
16+
17+
public AccessTokenHandlerTests()
18+
{
19+
_subject = new AccessTokenHandlerSubject(_testDPoPProofService, new TestDPoPNonceStore(), new TestLoggerProvider().CreateLogger("AccessTokenHandlerSubject"));
20+
_subject.InnerHandler = _testHttpMessageHandler;
21+
}
22+
23+
[Fact]
24+
public async Task lower_case_token_type_should_be_converted_to_case_sensitive()
25+
{
26+
var client = new HttpClient(_subject);
27+
28+
{
29+
_subject.AccessToken.AccessTokenType = "bearer";
30+
31+
var response = await client.GetAsync("https://test/api");
32+
33+
_testHttpMessageHandler.Request!.Headers.Authorization!.Scheme.ShouldBe("Bearer");
34+
}
35+
36+
{
37+
_subject.AccessToken.AccessTokenType = "dpop";
38+
39+
var response = await client.GetAsync("https://test/api");
40+
41+
_testHttpMessageHandler.Request!.Headers.Authorization!.Scheme.ShouldBe("DPoP");
42+
}
43+
}
44+
45+
public class TestHttpMessageHandler : HttpMessageHandler
46+
{
47+
public HttpRequestMessage? Request { get; set; }
48+
public HttpResponseMessage Response { get; set; } = new HttpResponseMessage(System.Net.HttpStatusCode.NoContent);
49+
50+
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
51+
{
52+
Request = request;
53+
return Task.FromResult(Response);
54+
}
55+
}
56+
57+
public class AccessTokenHandlerSubject : AccessTokenHandler
58+
{
59+
public ClientCredentialsToken AccessToken { get; set; } = new ClientCredentialsToken
60+
{
61+
AccessToken = "at",
62+
AccessTokenType = "bearer",
63+
};
64+
65+
public AccessTokenHandlerSubject(IDPoPProofService dPoPProofService, IDPoPNonceStore dPoPNonceStore, ILogger logger) : base(dPoPProofService, dPoPNonceStore, logger)
66+
{
67+
}
68+
69+
protected override Task<ClientCredentialsToken> GetAccessTokenAsync(bool forceRenewal, CancellationToken cancellationToken)
70+
{
71+
return Task.FromResult(AccessToken);
72+
}
73+
}
74+
}

‎test/Tests/ClientTokenManagementApiTests.cs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Copyright (c) Duende Software. All rights reserved.
2+
// See LICENSE in the project root for license information.
13

24
using Duende.IdentityServer.Configuration;
35
using IdentityModel;

‎test/Tests/ClientTokenManagementTests.cs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright (c) Duende Software. All rights reserved.
2+
// See LICENSE in the project root for license information.
3+
14
using System.Net;
25
using System.Text.Json;
36
using IdentityModel;

‎test/Tests/Framework/TestClientAssertionService.cs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright (c) Duende Software. All rights reserved.
2+
// See LICENSE in the project root for license information.
3+
14
using IdentityModel.Client;
25

36
namespace Duende.AccessTokenManagement.Tests;
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) Duende Software. All rights reserved.
2+
// See LICENSE in the project root for license information.
3+
4+
5+
namespace Duende.AccessTokenManagement.Tests;
6+
7+
public class TestDPoPNonceStore : IDPoPNonceStore
8+
{
9+
public Task<string?> GetNonceAsync(DPoPNonceContext context, CancellationToken cancellationToken = default)
10+
{
11+
return Task.FromResult<string?>(null);
12+
}
13+
14+
public Task StoreNonceAsync(DPoPNonceContext context, string nonce, CancellationToken cancellationToken = default)
15+
{
16+
return Task.CompletedTask;
17+
}
18+
}

‎test/Tests/UserTokenManagementTests.cs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright (c) Duende Software. All rights reserved.
2+
// See LICENSE in the project root for license information.
3+
14
using System.Net.Http.Json;
25
using System.Text.Json;
36
using Duende.AccessTokenManagement.OpenIdConnect;

‎test/Tests/Usings.cs

+3
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1+
// Copyright (c) Duende Software. All rights reserved.
2+
// See LICENSE in the project root for license information.
3+
14
global using Xunit;
25
global using Shouldly;

0 commit comments

Comments
 (0)
This repository has been archived.