Skip to content
This repository was archived by the owner on Jan 24, 2025. It is now read-only.

Commit cbc89eb

Browse files
authored
Merge pull request #178 from DuendeSoftware/brock/dpop-test
add test for dpop workflow
2 parents d545697 + d4aa717 commit cbc89eb

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

src/Duende.Bff.Yarp/AccessTokenRequestTransform.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ private async Task ApplyDPoPToken(RequestTransformContext context, DPoPTokenResu
9898
context.ProxyRequest.Headers.Add(OidcConstants.HttpHeaders.DPoP, proofToken.ProofToken);
9999
context.ProxyRequest.Headers.Authorization =
100100
new AuthenticationHeaderValue(OidcConstants.AuthenticationSchemes.AuthorizationHeaderDPoP, token.AccessToken);
101-
} else
101+
}
102+
else
102103
{
103104
// The proof service can opt out of DPoP by returning null. If so,
104105
// we just use the access token as a bearer token.

src/Duende.Bff.Yarp/AccessTokenTransformProvider.cs

-5
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Linq;
7-
using System.Net.Http.Headers;
8-
using System.Threading.Tasks;
97
using Duende.AccessTokenManagement;
10-
using Duende.Bff.Logging;
11-
using Duende.Bff.Yarp.Logging;
12-
using IdentityModel;
138
using Microsoft.Extensions.Logging;
149
using Microsoft.Extensions.Options;
1510
using Yarp.ReverseProxy.Transforms;

src/Duende.Bff.Yarp/IHttpTransformerFactory.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Duende Software. All rights reserved.
22
// See LICENSE in the project root for license information.
33

4-
using Duende.AccessTokenManagement;
54
using Yarp.ReverseProxy.Forwarder;
65

76
namespace Duende.Bff.Yarp;

test/Duende.Bff.Tests/Endpoints/RemoteEndpointTests.cs

+32
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
using Duende.Bff.Tests.TestFramework;
55
using Duende.Bff.Tests.TestHosts;
66
using FluentAssertions;
7+
using Microsoft.Extensions.DependencyInjection;
8+
using Microsoft.IdentityModel.Tokens;
79
using System;
810
using System.Linq;
911
using System.Net;
1012
using System.Net.Http;
13+
using System.Security.Cryptography;
1114
using System.Text;
1215
using System.Text.Json;
1316
using System.Threading.Tasks;
@@ -374,5 +377,34 @@ public async Task calls_to_bff_not_in_endpoint_routing_should_fail()
374377
Func<Task> f = () => BffHost.BrowserClient.SendAsync(req);
375378
await f.Should().ThrowAsync<Exception>();
376379
}
380+
381+
[Fact]
382+
public async Task test_dpop()
383+
{
384+
var rsaKey = new RsaSecurityKey(RSA.Create(2048));
385+
var jsonWebKey = JsonWebKeyConverter.ConvertFromRSASecurityKey(rsaKey);
386+
jsonWebKey.Alg = "PS256";
387+
var jwk = JsonSerializer.Serialize(jsonWebKey);
388+
389+
BffHost.OnConfigureServices += svcs =>
390+
{
391+
svcs.PostConfigure<BffOptions>(opts =>
392+
{
393+
opts.DPoPJsonWebKey = jwk;
394+
});
395+
};
396+
BffHost.InitializeAsync().Wait();
397+
398+
var req = new HttpRequestMessage(HttpMethod.Get, BffHost.Url("/api_client/test"));
399+
req.Headers.Add("x-csrf", "1");
400+
var response = await BffHost.BrowserClient.SendAsync(req);
401+
402+
response.IsSuccessStatusCode.Should().BeTrue();
403+
response.Content.Headers.ContentType.MediaType.Should().Be("application/json");
404+
var json = await response.Content.ReadAsStringAsync();
405+
var apiResult = JsonSerializer.Deserialize<ApiResponse>(json);
406+
apiResult.RequestHeaders["DPoP"].First().Should().NotBeNullOrEmpty();
407+
apiResult.RequestHeaders["Authorization"].First().StartsWith("DPoP ").Should().BeTrue();
408+
}
377409
}
378410
}

0 commit comments

Comments
 (0)