1
1
using System . Net . Http . Headers ;
2
2
using System . Web ;
3
+ using Hyperledger . Aries . Utils ;
3
4
using LanguageExt ;
4
5
using Newtonsoft . Json ;
5
6
using Newtonsoft . Json . Linq ;
@@ -18,6 +19,9 @@ public class AuthorizationRequestService(
18
19
IHttpClientFactory httpClientFactory ,
19
20
IRpAuthService rpAuthService ) : IAuthorizationRequestService
20
21
{
22
+ private const string RequestUriMethodGet = "get" ;
23
+ private const string RequestUriMethodPost = "post" ;
24
+
21
25
public async Task < Validation < AuthorizationRequestCancellation , AuthorizationRequest > > GetAuthorizationRequest (
22
26
AuthorizationRequestUri authorizationRequestUri ) =>
23
27
await authorizationRequestUri . Value . Match (
@@ -40,29 +44,22 @@ await authorizationRequestUri.Value.Match(
40
44
seq => seq
41
45
) ;
42
46
} ,
43
- async value =>
44
- {
45
- return await GetAuthRequestByValue ( value ) ;
46
- }
47
- ) ;
47
+ async value => await GetAuthRequestByValue ( value ) ) ;
48
48
49
49
private async Task < Validation < AuthorizationRequestCancellation , RequestObject > > GetRequestObject (
50
50
AuthorizationRequestByReference authRequestByReference )
51
51
{
52
- var httpClient = httpClientFactory . CreateClient ( ) ;
53
- httpClient . DefaultRequestHeaders . Clear ( ) ;
54
-
55
- var jsonString = await httpClient . GetStringAsync ( authRequestByReference . RequestUri ) ;
56
- var requestObjectValidation = FromStr ( jsonString ) ;
52
+ var requestObjectValidation = await FetchRequestObject ( authRequestByReference ) ;
57
53
58
- return await requestObjectValidation . MatchAsync ( async requestObject =>
54
+ return await requestObjectValidation . MatchAsync (
55
+ async requestObject =>
59
56
{
60
57
var authRequest = requestObject . ToAuthorizationRequest ( ) ;
61
58
var clientMetadataOption =
62
59
await FetchClientMetadata ( authRequest ) . OnException ( _ => Option < ClientMetadata > . None ) ;
63
-
60
+
64
61
var error = new InvalidRequestError ( $ "Client ID Scheme { requestObject . ClientIdScheme } is not supported") ;
65
-
62
+
66
63
Validation < AuthorizationRequestCancellation , RequestObject > result =
67
64
requestObject . ClientIdScheme . Value switch
68
65
{
@@ -79,7 +76,7 @@ private async Task<Validation<AuthorizationRequestCancellation, RequestObject>>
79
76
. WithClientMetadata ( clientMetadataOption ) ,
80
77
_ => new AuthorizationRequestCancellation ( authRequest . GetResponseUriMaybe ( ) , [ error ] )
81
78
} ;
82
-
79
+
83
80
return result ;
84
81
} ,
85
82
seq => seq ) ;
@@ -137,6 +134,61 @@ private async Task<Validation<AuthorizationRequestCancellation, AuthorizationReq
137
134
} ,
138
135
seq => seq ) ;
139
136
}
137
+
138
+ private async Task < Validation < AuthorizationRequestCancellation , RequestObject > > FetchRequestObject ( AuthorizationRequestByReference authRequestByReference )
139
+ {
140
+ return await authRequestByReference . RequestUriMethod . Match < Task < Validation < AuthorizationRequestCancellation , RequestObject > > > (
141
+ async method =>
142
+ {
143
+ return method . ToLowerInvariant ( ) switch
144
+ {
145
+ RequestUriMethodGet => await FetchRequestObjectViaGet ( authRequestByReference ) ,
146
+ RequestUriMethodPost => await FetchRequestObjectViaPost ( authRequestByReference ) ,
147
+ _ => new AuthorizationRequestCancellation ( Option < Uri > . None , [ new InvalidRequestUriMethodError ( $ "Unsupported request_uri_method: '{ method } '.") ] )
148
+ } ;
149
+ } ,
150
+ async ( ) => await FetchRequestObjectViaGet ( authRequestByReference ) ) ;
151
+ }
152
+
153
+ private async Task < Validation < AuthorizationRequestCancellation , RequestObject > > FetchRequestObjectViaPost ( AuthorizationRequestByReference authRequestByReference )
154
+ {
155
+ var httpClient = httpClientFactory . CreateClient ( ) ;
156
+ httpClient . DefaultRequestHeaders . Clear ( ) ;
157
+
158
+ var walletNonce = Base64UrlEncoder . Encode ( Guid . NewGuid ( ) . ToString ( ) ) ;
159
+ var keyValuePairs = new List < KeyValuePair < string , string > > ( ) ;
160
+ keyValuePairs . Add ( new KeyValuePair < string , string > ( "wallet_nonce" , walletNonce ) ) ;
161
+ keyValuePairs . Add ( new KeyValuePair < string , string > ( "wallet_metadata" , new JObject ( )
162
+ {
163
+ [ "vp_formats_supported" ] = new JObject ( )
164
+ {
165
+ [ "dc+sd-jwt" ] = new JObject ( )
166
+ {
167
+ [ "sd-jwt_alg_values" ] = new JArray ( ) { "ES256" , "ES384" , "ES512" , "RS256" } ,
168
+ [ "kb-jwt_alg_values" ] = new JArray ( ) { "ES256" }
169
+ } ,
170
+ [ "mso_mdoc" ] = new JObject ( )
171
+ {
172
+ [ "issuerauth_alg_values" ] = new JArray ( ) { "ES256" } ,
173
+ [ "deviceauth_alg_values" ] = new JArray ( ) { "ES256" }
174
+ }
175
+ }
176
+ } . ToString ( ) ) ) ;
177
+
178
+ var response = await httpClient . PostAsync ( authRequestByReference . RequestUri , new FormUrlEncodedContent ( keyValuePairs ) ) ;
179
+ response . EnsureSuccessStatusCode ( ) ;
180
+ var stringContent = await response . Content . ReadAsStringAsync ( ) ;
181
+
182
+ return FromStr ( stringContent , walletNonce ) ;
183
+ }
184
+
185
+ private async Task < Validation < AuthorizationRequestCancellation , RequestObject > > FetchRequestObjectViaGet ( AuthorizationRequestByReference authRequestByReference )
186
+ {
187
+ var httpClient = httpClientFactory . CreateClient ( ) ;
188
+ httpClient . DefaultRequestHeaders . Clear ( ) ;
189
+
190
+ return FromStr ( await httpClient . GetStringAsync ( authRequestByReference . RequestUri ) , Option < string > . None ) ;
191
+ }
140
192
141
193
private async Task < Option < ClientMetadata > > FetchClientMetadata ( AuthorizationRequest authorizationRequest )
142
194
{
0 commit comments