Skip to content

Commit 635f140

Browse files
authored
Merge pull request #351 from DuendeSoftware/jmdc/par-client-credential-style
use TokenClientCredentialStyle for PAR requests
2 parents b15ff68 + a16e5de commit 635f140

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

identity-model-oidc-client/src/IdentityModel.OidcClient/AuthorizeClient.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ private async Task<PushedAuthorizationResponse> PushAuthorizationRequestAsync(st
140140
ClientSecret = _options.ClientSecret,
141141
ClientAssertion = await _options.GetClientAssertionAsync(),
142142
ClientAssertionFactory = _options.GetClientAssertionAsync,
143+
ClientCredentialStyle = _options.TokenClientCredentialStyle,
143144
Parameters = CreateAuthorizeParameters(state, codeChallenge, frontChannelParameters),
144145
};
145146

identity-model-oidc-client/test/IdentityModel.OidcClient.Tests/CodeFlowResponseTests.cs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,9 @@ public async Task Malformed_identity_token_on_token_response_should_fail()
483483
[Fact]
484484
public async Task Authorize_should_push_parameters_when_PAR_is_enabled()
485485
{
486-
// Configure the client for PAR, authenticating with a client secret
486+
// Configure the client for PAR, authenticating with a client secret.
487+
// The default TokenClientCredentialStyle is PostBody, so the client
488+
// secret should be sent in the POST body (not the Authorization header).
487489
_options.ClientSecret = "secret";
488490
_options.ProviderInformation.PushedAuthorizationRequestEndpoint = "https://this-is-set-so-par-will-be-used";
489491
var client = new OidcClient(_options);
@@ -507,7 +509,44 @@ public async Task Authorize_should_push_parameters_when_PAR_is_enabled()
507509
startUrlQueryParams.GetValues("client_id").Single().ShouldBe("client");
508510
startUrlQueryParams.GetValues("request_uri").Single().ShouldBe(requestUri);
509511

510-
// Validate that the client authentication during the PAR request was correct
512+
// Validate that the client authentication during the PAR request used PostBody
513+
var parRequest = backChannelHandler.Request;
514+
parRequest.Headers.Authorization.ShouldBeNull();
515+
var parContent = await parRequest.Content.ReadAsStringAsync();
516+
var parParams = HttpUtility.ParseQueryString(parContent);
517+
parParams.GetValues("client_id").Single().ShouldBe("client");
518+
parParams.GetValues("client_secret").Single().ShouldBe("secret");
519+
}
520+
521+
[Fact]
522+
public async Task Par_should_use_authorization_header_when_configured()
523+
{
524+
// Configure the client for PAR with AuthorizationHeader credential style
525+
_options.ClientSecret = "secret";
526+
_options.TokenClientCredentialStyle = ClientCredentialStyle.AuthorizationHeader;
527+
_options.ProviderInformation.PushedAuthorizationRequestEndpoint = "https://this-is-set-so-par-will-be-used";
528+
var client = new OidcClient(_options);
529+
530+
// Mock the response from the par endpoint
531+
var requestUri = "mocked_request_uri";
532+
var parResponse = new Dictionary<string, string>
533+
{
534+
{ "request_uri", requestUri }
535+
};
536+
var backChannelHandler = new NetworkHandler(JsonSerializer.Serialize(parResponse), HttpStatusCode.OK);
537+
_options.BackchannelHandler = backChannelHandler;
538+
539+
// Prepare the login to cause the backchannel PAR request
540+
var state = await client.PrepareLoginAsync(cancellationToken: _ct);
541+
542+
// Validate that the resulting PAR state is correct
543+
var startUrl = new Uri(state.StartUrl);
544+
var startUrlQueryParams = HttpUtility.ParseQueryString(startUrl.Query);
545+
startUrlQueryParams.Count.ShouldBe(2);
546+
startUrlQueryParams.GetValues("client_id").Single().ShouldBe("client");
547+
startUrlQueryParams.GetValues("request_uri").Single().ShouldBe(requestUri);
548+
549+
// Validate that the client authentication during the PAR request used AuthorizationHeader
511550
var request = backChannelHandler.Request;
512551
request.Headers.Authorization.ShouldNotBeNull();
513552
request.Headers.Authorization.Scheme.ShouldBe("Basic");

0 commit comments

Comments
 (0)