Skip to content

Commit 0e72e5e

Browse files
committed
update for ath logic dpop validator in API
1 parent 9f80c1b commit 0e72e5e

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

src/DPoP/DPoPJwtBearerEvents.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ public override async Task TokenValidated(TokenValidatedContext context)
4141
{
4242
var dpopOptions = _optionsMonitor.Get(context.Scheme.Name);
4343

44-
if (context.HttpContext.Request.IsDPoPAuthorizationScheme())
44+
if (context.HttpContext.Request.TryGetDPoPAccessToken(out var at))
4545
{
4646
var proofToken = context.HttpContext.Request.GetDPoPProofToken();
4747
var result = await _validator.ValidateAsync(new DPoPProofValidatonContext
4848
{
4949
Scheme = context.Scheme.Name,
5050
ProofToken = proofToken,
51-
AccessTokenClaims = context.Principal.Claims,
51+
AccessToken = at,
5252
Method = context.HttpContext.Request.Method,
5353
Url = context.HttpContext.Request.Scheme + "://" + context.HttpContext.Request.Host + context.HttpContext.Request.PathBase + context.HttpContext.Request.Path
5454
});

src/DPoP/DPoPProofValidatonContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class DPoPProofValidatonContext
2626
public string ProofToken { get; set; }
2727

2828
/// <summary>
29-
/// The validated claims from the access token
29+
/// The access token
3030
/// </summary>
31-
public IEnumerable<Claim> AccessTokenClaims { get; set; }
31+
public string AccessToken { get; set; }
3232
}

src/DPoP/DPoPProofValidatonResult.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public class DPoPProofValidatonResult
4545
/// The jti value read from the payload.
4646
/// </summary>
4747
public string TokenId { get; set; }
48+
49+
/// <summary>
50+
/// The ath value read from the payload.
51+
/// </summary>
52+
public string AccessTokenHash { get; set; }
4853

4954
/// <summary>
5055
/// The nonce value read from the payload.

src/DPoP/DPoPProofValidator.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
using System;
88
using System.Collections.Generic;
99
using System.Linq;
10+
using System.Security.Cryptography;
11+
using System.Text;
1012
using System.Text.Json;
1113
using System.Threading.Tasks;
1214

13-
namespace DPoPApi;
15+
namespace ApiHost;
1416

1517
public class DPoPProofValidator
1618
{
@@ -211,6 +213,32 @@ protected virtual Task ValidateSignatureAsync(DPoPProofValidatonContext context,
211213
/// </summary>
212214
protected virtual async Task ValidatePayloadAsync(DPoPProofValidatonContext context, DPoPProofValidatonResult result)
213215
{
216+
if (result.Payload.TryGetValue(JwtClaimTypes.DPoPAccessTokenHash, out var ath))
217+
{
218+
result.AccessTokenHash = ath as string;
219+
}
220+
221+
if (String.IsNullOrEmpty(result.AccessTokenHash))
222+
{
223+
result.IsError = true;
224+
result.ErrorDescription = "Invalid 'ath' value.";
225+
return;
226+
}
227+
228+
using (var sha = SHA256.Create())
229+
{
230+
var bytes = Encoding.UTF8.GetBytes(context.AccessToken);
231+
var hash = sha.ComputeHash(bytes);
232+
233+
var accessTokenHash = Base64Url.Encode(hash);
234+
if (accessTokenHash != result.AccessTokenHash)
235+
{
236+
result.IsError = true;
237+
result.ErrorDescription = "Invalid 'ath' value.";
238+
return;
239+
}
240+
}
241+
214242
if (result.Payload.TryGetValue(JwtClaimTypes.JwtId, out var jti))
215243
{
216244
result.TokenId = jti as string;

0 commit comments

Comments
 (0)