-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for JWT-secured OAuth 2.0 authorisation request (JAR) (#17)
- Loading branch information
Showing
3 changed files
with
102 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/GovUk.OneLogin.AspNetCore/JwtSecuredAuthorizationRequestMessage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
using Microsoft.IdentityModel.Protocols.OpenIdConnect; | ||
|
||
namespace GovUk.OneLogin.AspNetCore; | ||
|
||
internal delegate string CreateJwtSecuredAuthorizationRequest(IDictionary<string, object> claims); | ||
|
||
internal class JwtSecuredAuthorizationRequestMessage : OpenIdConnectMessage | ||
{ | ||
private readonly CreateJwtSecuredAuthorizationRequest _createJwt; | ||
|
||
public JwtSecuredAuthorizationRequestMessage(OpenIdConnectMessage message, CreateJwtSecuredAuthorizationRequest createJwt) : base(message) | ||
{ | ||
ArgumentNullException.ThrowIfNull(createJwt); | ||
_createJwt = createJwt; | ||
} | ||
|
||
public override string CreateAuthenticationRequestUrl() | ||
{ | ||
// https://openid.net/specs/openid-connect-core-1_0.html#RequestObject | ||
|
||
var openIdConnectMessage = Clone(); | ||
openIdConnectMessage.RequestType = OpenIdConnectRequestType.Authentication; | ||
|
||
Dictionary<string, object> claims = openIdConnectMessage.Parameters | ||
.ToDictionary( | ||
kvp => kvp.Key, | ||
kvp => | ||
{ | ||
if (kvp.Key is "vtr" or "claims") | ||
{ | ||
return JsonSerializer.SerializeToElement(JsonNode.Parse(kvp.Value)); | ||
} | ||
else | ||
{ | ||
return (object)kvp.Value; | ||
} | ||
}); | ||
claims.Add("iss", ClientId); | ||
|
||
foreach (var key in openIdConnectMessage.Parameters.Keys) | ||
{ | ||
if (key is not "response_type" and not "scope" and not "client_id") | ||
{ | ||
openIdConnectMessage.RemoveParameter(key); | ||
} | ||
} | ||
|
||
var request = _createJwt(claims); | ||
openIdConnectMessage.SetParameter("request", request); | ||
|
||
return openIdConnectMessage.BuildRedirectUrl(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters