Skip to content

Commit 8716dfc

Browse files
committed
Use AuthServer requested in the CredentialOffer
Signed-off-by: Johannes Tuerk <johannes.tuerk@lissi.id>
1 parent 0c4566a commit 8716dfc

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

src/WalletFramework.Oid4Vc/Oid4Vci/CredOffer/GrantTypes/PreAuthorizedCode.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Newtonsoft.Json.Linq;
44
using WalletFramework.Core.Functional;
55
using WalletFramework.Core.Json;
6+
using WalletFramework.Oid4Vc.Oid4Vci.Authorization.Models;
67
using static WalletFramework.Oid4Vc.Oid4Vci.CredOffer.GrantTypes.TransactionCode;
78

89
namespace WalletFramework.Oid4Vc.Oid4Vci.CredOffer.GrantTypes;
@@ -24,11 +25,18 @@ public record PreAuthorizedCode
2425
/// </summary>
2526
[JsonProperty("tx_code")]
2627
public Option<TransactionCode> TransactionCode { get; }
28+
29+
/// <summary>
30+
/// Specifying whether the user must send a Transaction Code along with the Token Request in a Pre-Authorized Code Flow.
31+
/// </summary>
32+
[JsonProperty("authorization_server")]
33+
public Option<AuthorizationServerId> AuthorizationServer { get; }
2734

28-
private PreAuthorizedCode(string value, Option<TransactionCode> transactionCode)
35+
private PreAuthorizedCode(string value, Option<TransactionCode> transactionCode, Option<AuthorizationServerId> authorizationServer)
2936
{
3037
Value = value;
3138
TransactionCode = transactionCode;
39+
AuthorizationServer = authorizationServer;
3240
}
3341

3442
public static Option<PreAuthorizedCode> OptionalPreAuthorizedCode(JToken preAuthCode)
@@ -38,12 +46,17 @@ public static Option<PreAuthorizedCode> OptionalPreAuthorizedCode(JToken preAuth
3846
.ToOption()
3947
.OnSome(OptionalTransactionCode);
4048

49+
var authorizationServer = preAuthCode
50+
.GetByKey("authorization_server")
51+
.OnSuccess(AuthorizationServerId.ValidAuthorizationServerId)
52+
.ToOption();
53+
4154
return preAuthCode
4255
.GetByKey("pre-authorized_code")
4356
.OnSuccess(token =>
4457
{
4558
var value = token.ToString();
46-
return new PreAuthorizedCode(value, transactionCode);
59+
return new PreAuthorizedCode(value, transactionCode, authorizationServer);
4760
})
4861
.ToOption();
4962
}

src/WalletFramework.Oid4Vc/Oid4Vci/Implementations/Oid4VciClientService.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -711,9 +711,7 @@ from server in code.AuthorizationServer
711711
var getAuthServerResponse = await _httpClient.GetAsync(authServerUrl);
712712

713713
if (!getAuthServerResponse.IsSuccessStatusCode)
714-
throw new HttpRequestException(
715-
$"Failed to get authorization server metadata. Status Code is: {getAuthServerResponse.StatusCode}"
716-
);
714+
continue;
717715

718716
var content = await getAuthServerResponse.Content.ReadAsStringAsync();
719717

@@ -724,28 +722,37 @@ from server in code.AuthorizationServer
724722
authorizationServerMetadatas.Add(authServer);
725723
}
726724

727-
if (authorizationServerMetadatas.Count == 1)
728-
return authorizationServerMetadatas.First();
729-
730725
return credentialOffer.Match(
731726
Some: offer =>
732727
{
733728
var credentialOfferAuthCodeGrantType = from grants in offer.Grants
734729
from code in grants.AuthorizationCode
735730
select code;
736731

737-
return credentialOfferAuthCodeGrantType.Match(
738-
Some: code => authorizationServerMetadatas.Find(authServer => authServer.SupportsAuthCodeFlow) ??
739-
throw new InvalidOperationException("No suitable Authorization Server found"),
732+
return credentialOfferAuthCodeGrantType.Match(
733+
Some: code => code.AuthorizationServer.Match(
734+
Some: requestedAuthServer =>
735+
authorizationServerMetadatas.Find(authServer =>
736+
authServer.Issuer == requestedAuthServer.ToString())
737+
?? throw new InvalidOperationException("No suitable Authorization Server found"),
738+
None: () => authorizationServerMetadatas.Find(authServer => authServer.SupportsAuthCodeFlow) ??
739+
throw new InvalidOperationException("No suitable Authorization Server found")),
740740
None: () =>
741741
{
742742
var credentialOfferPreAuthGrantType = from grants in offer.Grants
743-
from code in grants.AuthorizationCode
743+
from code in grants.PreAuthorizedCode
744744
select code;
745745

746746
return credentialOfferPreAuthGrantType.Match(
747-
Some: preAuth => authorizationServerMetadatas.Find(authServer => authServer.SupportsPreAuthFlow)
748-
?? throw new InvalidOperationException("No suitable Authorization Server found"),
747+
Some: preAuth =>
748+
{
749+
return preAuth.AuthorizationServer.Match(
750+
Some: requestedAuthServer =>
751+
authorizationServerMetadatas.Find(authServer =>
752+
authServer.Issuer == requestedAuthServer.ToString())
753+
?? throw new InvalidOperationException("No suitable Authorization Server found"),
754+
None: () => authorizationServerMetadatas.Find(authServer => authServer.SupportsPreAuthFlow));
755+
},
749756
None: () => authorizationServerMetadatas.First());
750757
});
751758
},

0 commit comments

Comments
 (0)