@@ -13,7 +13,9 @@ describe("AuthHelper for Cognito", () => {
13
13
const region = "us-west-2" ;
14
14
const cognitoIdentityPoolId = `${ region } :TEST-IDENTITY-POOL-ID` ;
15
15
const url = "https://maps.geo.us-west-2.amazonaws.com/" ;
16
+ const govCloudUrl = "https://maps.geo-fips.us-gov-west-1.amazonaws.com/" ;
16
17
const nonAWSUrl = "https://example.com/" ;
18
+ const nonLocationAWSUrl = "https://my.cool.service.us-west-2.amazonaws.com/" ;
17
19
const mockedCredentials = {
18
20
identityId : "identityId" ,
19
21
accessKeyId : "accessKeyId" ,
@@ -162,6 +164,35 @@ describe("AuthHelper for Cognito", () => {
162
164
expect ( credential ) . toContain ( mockedCredentials . accessKeyId ) ;
163
165
} ) ;
164
166
167
+ it ( "getMapAuthenticationOptions should contain transformRequest function to sign the AWS GovCloud Urls using our custom signer" , async ( ) => {
168
+ const authHelper = await withIdentityPoolId ( cognitoIdentityPoolId ) ;
169
+ const transformRequest = authHelper . getMapAuthenticationOptions ( ) . transformRequest ;
170
+ const originalUrl = new URL ( govCloudUrl ) ;
171
+ const signedUrl = new URL ( transformRequest ( govCloudUrl ) . url ) ;
172
+
173
+ // Host and pathname should still be the same
174
+ expect ( signedUrl . hostname ) . toStrictEqual ( originalUrl . hostname ) ;
175
+ expect ( signedUrl . pathname ) . toStrictEqual ( originalUrl . pathname ) ;
176
+
177
+ const searchParams = signedUrl . searchParams ;
178
+ expect ( searchParams . size ) . toStrictEqual ( 6 ) ;
179
+
180
+ // Verify these search params exist on the signed url
181
+ // We don't need to test the actual values since they are non-deterministic or constants
182
+ const expectedSearchParams = [ "X-Amz-Algorithm" , "X-Amz-Date" , "X-Amz-SignedHeaders" , "X-Amz-Signature" ] ;
183
+ expectedSearchParams . forEach ( ( value ) => {
184
+ expect ( searchParams . has ( value ) ) . toStrictEqual ( true ) ;
185
+ } ) ;
186
+
187
+ // We can expect the session token to match exactly as passed in
188
+ const securityToken = searchParams . get ( "X-Amz-Security-Token" ) ;
189
+ expect ( securityToken ) . toStrictEqual ( mockedCredentials . sessionToken ) ;
190
+
191
+ // The credential starts with our access key, the rest is generated
192
+ const credential = searchParams . get ( "X-Amz-Credential" ) ;
193
+ expect ( credential ) . toContain ( mockedCredentials . accessKeyId ) ;
194
+ } ) ;
195
+
165
196
it ( "getMapAuthenticationOptions transformRequest function should pass-through non AWS Urls unchanged" , async ( ) => {
166
197
const authHelper = await withIdentityPoolId ( cognitoIdentityPoolId ) ;
167
198
const transformRequest = authHelper . getMapAuthenticationOptions ( ) . transformRequest ;
@@ -171,6 +202,15 @@ describe("AuthHelper for Cognito", () => {
171
202
} ) ;
172
203
} ) ;
173
204
205
+ it ( "getMapAuthenticationOptions transformRequest function should pass-through AWS Urls that aren't for the Amazon Location Service unchanged" , async ( ) => {
206
+ const authHelper = await withIdentityPoolId ( cognitoIdentityPoolId ) ;
207
+ const transformRequest = authHelper . getMapAuthenticationOptions ( ) . transformRequest ;
208
+
209
+ expect ( transformRequest ( nonLocationAWSUrl ) ) . toStrictEqual ( {
210
+ url : nonLocationAWSUrl ,
211
+ } ) ;
212
+ } ) ;
213
+
174
214
it ( "getLocationClientConfig should provide credentials from cognito" , async ( ) => {
175
215
const authHelper = await withIdentityPoolId ( cognitoIdentityPoolId ) ;
176
216
const additionalLocationClientConfig = authHelper . getLocationClientConfig ( ) ;
0 commit comments