Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Newtonsoft.Json.Linq;
using WalletFramework.Core.Functional;
using WalletFramework.Core.Json;
using WalletFramework.Oid4Vc.Oid4Vci.Authorization.Models;
using static WalletFramework.Oid4Vc.Oid4Vci.CredOffer.GrantTypes.TransactionCode;

namespace WalletFramework.Oid4Vc.Oid4Vci.CredOffer.GrantTypes;
Expand All @@ -24,11 +25,18 @@ public record PreAuthorizedCode
/// </summary>
[JsonProperty("tx_code")]
public Option<TransactionCode> TransactionCode { get; }

/// <summary>
/// Specifying whether the user must send a Transaction Code along with the Token Request in a Pre-Authorized Code Flow.
/// </summary>
[JsonProperty("authorization_server")]
public Option<AuthorizationServerId> AuthorizationServer { get; }

private PreAuthorizedCode(string value, Option<TransactionCode> transactionCode)
private PreAuthorizedCode(string value, Option<TransactionCode> transactionCode, Option<AuthorizationServerId> authorizationServer)
{
Value = value;
TransactionCode = transactionCode;
AuthorizationServer = authorizationServer;
}

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

var authorizationServer = preAuthCode
.GetByKey("authorization_server")
.OnSuccess(AuthorizationServerId.ValidAuthorizationServerId)
.ToOption();

return preAuthCode
.GetByKey("pre-authorized_code")
.OnSuccess(token =>
{
var value = token.ToString();
return new PreAuthorizedCode(value, transactionCode);
return new PreAuthorizedCode(value, transactionCode, authorizationServer);
})
.ToOption();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -711,9 +711,7 @@ from server in code.AuthorizationServer
var getAuthServerResponse = await _httpClient.GetAsync(authServerUrl);

if (!getAuthServerResponse.IsSuccessStatusCode)
throw new HttpRequestException(
$"Failed to get authorization server metadata. Status Code is: {getAuthServerResponse.StatusCode}"
);
continue;

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

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

if (authorizationServerMetadatas.Count == 1)
return authorizationServerMetadatas.First();

return credentialOffer.Match(
Some: offer =>
{
var credentialOfferAuthCodeGrantType = from grants in offer.Grants
from code in grants.AuthorizationCode
select code;

return credentialOfferAuthCodeGrantType.Match(
Some: code => authorizationServerMetadatas.Find(authServer => authServer.SupportsAuthCodeFlow) ??
throw new InvalidOperationException("No suitable Authorization Server found"),
return credentialOfferAuthCodeGrantType.Match(
Some: code => code.AuthorizationServer.Match(
Some: requestedAuthServer =>
authorizationServerMetadatas.Find(authServer =>
authServer.Issuer == requestedAuthServer.ToString())
?? throw new InvalidOperationException("No suitable Authorization Server found"),
None: () => authorizationServerMetadatas.Find(authServer => authServer.SupportsAuthCodeFlow) ??
throw new InvalidOperationException("No suitable Authorization Server found")),
None: () =>
{
var credentialOfferPreAuthGrantType = from grants in offer.Grants
from code in grants.AuthorizationCode
from code in grants.PreAuthorizedCode
select code;

return credentialOfferPreAuthGrantType.Match(
Some: preAuth => authorizationServerMetadatas.Find(authServer => authServer.SupportsPreAuthFlow)
?? throw new InvalidOperationException("No suitable Authorization Server found"),
Some: preAuth =>
{
return preAuth.AuthorizationServer.Match(
Some: requestedAuthServer =>
authorizationServerMetadatas.Find(authServer =>
authServer.Issuer == requestedAuthServer.ToString())
?? throw new InvalidOperationException("No suitable Authorization Server found"),
None: () => authorizationServerMetadatas.Find(authServer => authServer.SupportsPreAuthFlow));
},
None: () => authorizationServerMetadatas.First());
});
},
Expand Down
Loading