@@ -6,123 +6,20 @@ namespace Thirdweb.EWS
6
6
{
7
7
internal class AWS
8
8
{
9
- private const string awsRegion = "us-west-2" ;
10
- private const string cognitoAppClientId = "2e02ha2ce6du13ldk8pai4h3d0" ;
11
- private static readonly string cognitoIdentityPoolId = $ "{ awsRegion } :2ad7ab1e-f48b-48a6-adfa-ac1090689c26";
12
- private static readonly string cognitoUserPoolId = $ "{ awsRegion } _UFwLcZIpq";
13
- private static readonly string recoverySharePasswordLambdaFunctionName = $ "arn:aws:lambda:{ awsRegion } :324457261097:function:recovery-share-password-GenerateRecoverySharePassw-bbE5ZbVAToil";
14
- private static readonly string recoverySharePasswordLambdaFunctionNameV2 = "arn:aws:lambda:us-west-2:324457261097:function:lambda-thirdweb-auth-enc-key-prod-ThirdwebAuthEncKeyFunction" ;
15
-
16
- internal static async Task SignUpCognitoUserAsync ( string emailAddress , string userName , Type thirdwebHttpClientType )
17
- {
18
- emailAddress ??= "[email protected] " ;
19
-
20
- var client = thirdwebHttpClientType . GetConstructor ( Type . EmptyTypes ) . Invoke ( null ) as IThirdwebHttpClient ;
21
- var endpoint = $ "https://cognito-idp.{ awsRegion } .amazonaws.com/";
22
- var payload = new
23
- {
24
- ClientId = cognitoAppClientId ,
25
- Username = userName ,
26
- Password = Secrets . Random ( 12 ) ,
27
- UserAttributes = new [ ] { new { Name = "email" , Value = emailAddress } }
28
- } ;
29
-
30
- var content = new StringContent ( JsonConvert . SerializeObject ( payload ) , Encoding . UTF8 , "application/x-amz-json-1.1" ) ;
31
-
32
- client . AddHeader ( "X-Amz-Target" , "AWSCognitoIdentityProviderService.SignUp" ) ;
33
-
34
- var response = await client . PostAsync ( endpoint , content ) . ConfigureAwait ( false ) ;
35
-
36
- if ( ! response . IsSuccessStatusCode )
37
- {
38
- var responseBody = await response . Content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ;
39
- throw new Exception ( $ "Sign-up failed: { responseBody } ") ;
40
- }
41
- }
42
-
43
- internal static async Task < string > StartCognitoUserAuth ( string userName , Type thirdwebHttpClientType )
44
- {
45
- var client = thirdwebHttpClientType . GetConstructor ( Type . EmptyTypes ) . Invoke ( null ) as IThirdwebHttpClient ;
46
- var endpoint = $ "https://cognito-idp.{ awsRegion } .amazonaws.com/";
47
- var payload = new
48
- {
49
- AuthFlow = "CUSTOM_AUTH" ,
50
- ClientId = cognitoAppClientId ,
51
- AuthParameters = new Dictionary < string , string > { { "USERNAME" , userName } } ,
52
- ClientMetadata = new Dictionary < string , string > ( )
53
- } ;
54
-
55
- var content = new StringContent ( JsonConvert . SerializeObject ( payload ) , Encoding . UTF8 , "application/x-amz-json-1.1" ) ;
56
-
57
- client . AddHeader ( "X-Amz-Target" , "AWSCognitoIdentityProviderService.InitiateAuth" ) ;
58
-
59
- var response = await client . PostAsync ( endpoint , content ) . ConfigureAwait ( false ) ;
60
-
61
- var responseContent = await response . Content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ;
62
-
63
- if ( ! response . IsSuccessStatusCode )
64
- {
65
- var errorResponse = JsonConvert . DeserializeObject < ErrorResponse > ( responseContent ) ;
66
- if ( errorResponse . Type == "UserNotFoundException" )
67
- {
68
- return null ;
69
- }
70
- throw new Exception ( $ "Authentication initiation failed: { responseContent } ") ;
71
- }
72
-
73
- var jsonResponse = JsonConvert . DeserializeObject < StartAuthResponse > ( responseContent ) ;
74
- return jsonResponse . Session ;
75
- }
76
-
77
- internal static async Task < TokenCollection > FinishCognitoUserAuth ( string userName , string otp , string sessionId , Type thirdwebHttpClientType )
78
- {
79
- var client = thirdwebHttpClientType . GetConstructor ( Type . EmptyTypes ) . Invoke ( null ) as IThirdwebHttpClient ;
80
- var endpoint = $ "https://cognito-idp.{ awsRegion } .amazonaws.com/";
81
- var payload = new
82
- {
83
- ChallengeName = "CUSTOM_CHALLENGE" ,
84
- ClientId = cognitoAppClientId ,
85
- ChallengeResponses = new Dictionary < string , string > { { "USERNAME" , userName } , { "ANSWER" , otp } } ,
86
- Session = sessionId
87
- } ;
88
-
89
- var content = new StringContent ( JsonConvert . SerializeObject ( payload ) , Encoding . UTF8 , "application/x-amz-json-1.1" ) ;
90
-
91
- client . AddHeader ( "X-Amz-Target" , "AWSCognitoIdentityProviderService.RespondToAuthChallenge" ) ;
92
-
93
- var response = await client . PostAsync ( endpoint , content ) . ConfigureAwait ( false ) ;
94
-
95
- var responseContent = await response . Content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ;
96
-
97
- if ( ! response . IsSuccessStatusCode )
98
- {
99
- var errorResponse = JsonConvert . DeserializeObject < ErrorResponse > ( responseContent ) ;
100
- if ( errorResponse . Type == "NotAuthorizedException" )
101
- {
102
- throw new VerificationException ( "The session expired" , false ) ;
103
- }
104
- if ( errorResponse . Type == "UserNotFoundException" )
105
- {
106
- throw new InvalidOperationException ( "The user was not found" ) ;
107
- }
108
- throw new Exception ( $ "Challenge response failed: { responseContent } ") ;
109
- }
9
+ private const string AWS_REGION = "us-west-2" ;
110
10
111
- var jsonResponse = JsonConvert . DeserializeObject < FinishAuthResponse > ( responseContent ) ;
112
- var result = jsonResponse . AuthenticationResult ?? throw new VerificationException ( "The OTP is incorrect" , true ) ;
113
- return new TokenCollection ( result . AccessToken . ToString ( ) , result . IdToken . ToString ( ) , result . RefreshToken . ToString ( ) ) ;
114
- }
11
+ private static readonly string recoverySharePasswordLambdaFunctionNameV2 = $ "arn:aws:lambda:{ AWS_REGION } :324457261097:function:lambda-thirdweb-auth-enc-key-prod-ThirdwebAuthEncKeyFunction";
115
12
116
- internal static async Task < MemoryStream > InvokeRecoverySharePasswordLambdaV2Async ( string identityId , string token , string invokePayload , Type thirdwebHttpClientType )
13
+ internal static async Task < MemoryStream > InvokeRecoverySharePasswordLambdaAsync ( string identityId , string token , string invokePayload , Type thirdwebHttpClientType )
117
14
{
118
- var credentials = await GetTemporaryCredentialsV2Async ( identityId , token , thirdwebHttpClientType ) . ConfigureAwait ( false ) ;
15
+ var credentials = await GetTemporaryCredentialsAsync ( identityId , token , thirdwebHttpClientType ) . ConfigureAwait ( false ) ;
119
16
return await InvokeLambdaWithTemporaryCredentialsAsync ( credentials , invokePayload , thirdwebHttpClientType , recoverySharePasswordLambdaFunctionNameV2 ) . ConfigureAwait ( false ) ;
120
17
}
121
18
122
- private static async Task < AwsCredentials > GetTemporaryCredentialsV2Async ( string identityId , string token , Type thirdwebHttpClientType )
19
+ private static async Task < AwsCredentials > GetTemporaryCredentialsAsync ( string identityId , string token , Type thirdwebHttpClientType )
123
20
{
124
21
var client = thirdwebHttpClientType . GetConstructor ( Type . EmptyTypes ) . Invoke ( null ) as IThirdwebHttpClient ;
125
- var endpoint = $ "https://cognito-identity.{ awsRegion } .amazonaws.com/";
22
+ var endpoint = $ "https://cognito-identity.{ AWS_REGION } .amazonaws.com/";
126
23
127
24
var payloadForGetCredentials = new { IdentityId = identityId , Logins = new Dictionary < string , string > { { "cognito-identity.amazonaws.com" , token } } } ;
128
25
@@ -148,67 +45,9 @@ private static async Task<AwsCredentials> GetTemporaryCredentialsV2Async(string
148
45
} ;
149
46
}
150
47
151
- internal static async Task < MemoryStream > InvokeRecoverySharePasswordLambdaAsync ( string idToken , string invokePayload , Type thirdwebHttpClientType )
152
- {
153
- var credentials = await GetTemporaryCredentialsAsync ( idToken , thirdwebHttpClientType ) . ConfigureAwait ( false ) ;
154
- return await InvokeLambdaWithTemporaryCredentialsAsync ( credentials , invokePayload , thirdwebHttpClientType , recoverySharePasswordLambdaFunctionName ) . ConfigureAwait ( false ) ;
155
- }
156
-
157
- private static async Task < AwsCredentials > GetTemporaryCredentialsAsync ( string idToken , Type thirdwebHttpClientType )
158
- {
159
- var client = thirdwebHttpClientType . GetConstructor ( Type . EmptyTypes ) . Invoke ( null ) as IThirdwebHttpClient ;
160
- var endpoint = $ "https://cognito-identity.{ awsRegion } .amazonaws.com/";
161
-
162
- var payloadForGetId = new { IdentityPoolId = cognitoIdentityPoolId , Logins = new Dictionary < string , string > { { $ "cognito-idp.{ awsRegion } .amazonaws.com/{ cognitoUserPoolId } ", idToken } } } ;
163
-
164
- var content = new StringContent ( JsonConvert . SerializeObject ( payloadForGetId ) , Encoding . UTF8 , "application/x-amz-json-1.1" ) ;
165
-
166
- client . AddHeader ( "X-Amz-Target" , "AWSCognitoIdentityService.GetId" ) ;
167
-
168
- var response = await client . PostAsync ( endpoint , content ) . ConfigureAwait ( false ) ;
169
-
170
- var responseContent = await response . Content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ;
171
-
172
- if ( ! response . IsSuccessStatusCode )
173
- {
174
- throw new Exception ( $ "Failed to get identity ID: { responseContent } ") ;
175
- }
176
-
177
- var identityIdResponse = JsonConvert . DeserializeObject < GetIdResponse > ( responseContent ) ;
178
-
179
- var payloadForGetCredentials = new
180
- {
181
- IdentityId = identityIdResponse . IdentityId ,
182
- Logins = new Dictionary < string , string > { { $ "cognito-idp.{ awsRegion } .amazonaws.com/{ cognitoUserPoolId } ", idToken } }
183
- } ;
184
-
185
- content = new StringContent ( JsonConvert . SerializeObject ( payloadForGetCredentials ) , Encoding . UTF8 , "application/x-amz-json-1.1" ) ;
186
-
187
- client . RemoveHeader ( "X-Amz-Target" ) ;
188
- client . AddHeader ( "X-Amz-Target" , "AWSCognitoIdentityService.GetCredentialsForIdentity" ) ;
189
-
190
- response = await client . PostAsync ( endpoint , content ) . ConfigureAwait ( false ) ;
191
-
192
- responseContent = await response . Content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ;
193
-
194
- if ( ! response . IsSuccessStatusCode )
195
- {
196
- throw new Exception ( $ "Failed to get credentials: { responseContent } ") ;
197
- }
198
-
199
- var credentialsResponse = JsonConvert . DeserializeObject < GetCredentialsForIdentityResponse > ( responseContent ) ;
200
-
201
- return new AwsCredentials
202
- {
203
- AccessKeyId = credentialsResponse . Credentials . AccessKeyId ,
204
- SecretAccessKey = credentialsResponse . Credentials . SecretKey ,
205
- SessionToken = credentialsResponse . Credentials . SessionToken
206
- } ;
207
- }
208
-
209
48
private static async Task < MemoryStream > InvokeLambdaWithTemporaryCredentialsAsync ( AwsCredentials credentials , string invokePayload , Type thirdwebHttpClientType , string lambdaFunction )
210
49
{
211
- var endpoint = $ "https://lambda.{ awsRegion } .amazonaws.com/2015-03-31/functions/{ lambdaFunction } /invocations";
50
+ var endpoint = $ "https://lambda.{ AWS_REGION } .amazonaws.com/2015-03-31/functions/{ lambdaFunction } /invocations";
212
51
var requestBody = new StringContent ( invokePayload , Encoding . UTF8 , "application/json" ) ;
213
52
214
53
var client = thirdwebHttpClientType . GetConstructor ( Type . EmptyTypes ) . Invoke ( null ) as IThirdwebHttpClient ;
@@ -219,18 +58,18 @@ private static async Task<MemoryStream> InvokeLambdaWithTemporaryCredentialsAsyn
219
58
220
59
var canonicalUri = "/2015-03-31/functions/" + Uri . EscapeDataString ( lambdaFunction ) + "/invocations" ;
221
60
var canonicalQueryString = "" ;
222
- var canonicalHeaders = $ "host:lambda.{ awsRegion } .amazonaws.com\n x-amz-date:{ amzDate } \n ";
61
+ var canonicalHeaders = $ "host:lambda.{ AWS_REGION } .amazonaws.com\n x-amz-date:{ amzDate } \n ";
223
62
var signedHeaders = "host;x-amz-date" ;
224
63
225
64
using var sha256 = SHA256 . Create ( ) ;
226
65
var payloadHash = ToHexString ( sha256 . ComputeHash ( Encoding . UTF8 . GetBytes ( invokePayload ) ) ) ;
227
66
var canonicalRequest = $ "POST\n { canonicalUri } \n { canonicalQueryString } \n { canonicalHeaders } \n { signedHeaders } \n { payloadHash } ";
228
67
229
68
var algorithm = "AWS4-HMAC-SHA256" ;
230
- var credentialScope = $ "{ dateStamp } /{ awsRegion } /lambda/aws4_request";
69
+ var credentialScope = $ "{ dateStamp } /{ AWS_REGION } /lambda/aws4_request";
231
70
var stringToSign = $ "{ algorithm } \n { amzDate } \n { credentialScope } \n { ToHexString ( sha256 . ComputeHash ( Encoding . UTF8 . GetBytes ( canonicalRequest ) ) ) } ";
232
71
233
- var signingKey = GetSignatureKey ( credentials . SecretAccessKey , dateStamp , awsRegion , "lambda" ) ;
72
+ var signingKey = GetSignatureKey ( credentials . SecretAccessKey , dateStamp , AWS_REGION , "lambda" ) ;
234
73
var signature = ToHexString ( HMACSHA256 ( signingKey , stringToSign ) ) ;
235
74
236
75
var authorizationHeader = $ "{ algorithm } Credential={ credentials . AccessKeyId } /{ credentialScope } , SignedHeaders={ signedHeaders } , Signature={ signature } ";
0 commit comments