@@ -97,9 +97,13 @@ class FetchAuthSessionCognitoActionsTest {
9797 val originalRefreshToken = " original_refresh_token"
9898 val newRefreshToken = " new_refresh_token"
9999
100- coEvery { cognitoIdentityProviderClientMock.getTokensFromRefreshToken(any()) } returns GetTokensFromRefreshTokenResponse {
100+ coEvery {
101+ cognitoIdentityProviderClientMock.getTokensFromRefreshToken(any())
102+ } returns GetTokensFromRefreshTokenResponse {
101103 authenticationResult = AuthenticationResultType {
102- this .accessToken = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ.XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o"
104+ this .accessToken = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." +
105+ " eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ." +
106+ " XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o"
103107 this .idToken = " id_token"
104108 this .refreshToken = newRefreshToken
105109 this .expiresIn = 3600
@@ -109,14 +113,17 @@ class FetchAuthSessionCognitoActionsTest {
109113 val signedInData = mockSignedInData(
110114 username = " username" ,
111115 cognitoUserPoolTokens = CognitoUserPoolTokens (
112- accessToken = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ.XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o" ,
116+ accessToken = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." +
117+ " eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ." +
118+ " XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o" ,
113119 idToken = " old_id" ,
114120 refreshToken = originalRefreshToken,
115121 expiration = 0
116122 )
117123 )
118124
119- FetchAuthSessionCognitoActions .refreshUserPoolTokensAction(signedInData).execute(dispatcher, authEnvironment)
125+ FetchAuthSessionCognitoActions .refreshUserPoolTokensAction(signedInData)
126+ .execute(dispatcher, authEnvironment)
120127
121128 val event = capturedEvent.captured.shouldBeInstanceOf<RefreshSessionEvent >()
122129 val refreshedData = event.eventType.shouldBeInstanceOf<RefreshSessionEvent .EventType .Refreshed >().signedInData
@@ -127,9 +134,13 @@ class FetchAuthSessionCognitoActionsTest {
127134 fun `refreshUserPoolTokensAction falls back to original refresh token when rotation is not enabled` () = runTest {
128135 val originalRefreshToken = " original_refresh_token"
129136
130- coEvery { cognitoIdentityProviderClientMock.getTokensFromRefreshToken(any()) } returns GetTokensFromRefreshTokenResponse {
137+ coEvery {
138+ cognitoIdentityProviderClientMock.getTokensFromRefreshToken(any())
139+ } returns GetTokensFromRefreshTokenResponse {
131140 authenticationResult = AuthenticationResultType {
132- this .accessToken = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ.XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o"
141+ this .accessToken = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." +
142+ " eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ." +
143+ " XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o"
133144 this .idToken = " id_token"
134145 this .refreshToken = null
135146 this .expiresIn = 3600
@@ -139,14 +150,17 @@ class FetchAuthSessionCognitoActionsTest {
139150 val signedInData = mockSignedInData(
140151 username = " username" ,
141152 cognitoUserPoolTokens = CognitoUserPoolTokens (
142- accessToken = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ.XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o" ,
153+ accessToken = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." +
154+ " eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ." +
155+ " XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o" ,
143156 idToken = " old_id" ,
144157 refreshToken = originalRefreshToken,
145158 expiration = 0
146159 )
147160 )
148161
149- FetchAuthSessionCognitoActions .refreshUserPoolTokensAction(signedInData).execute(dispatcher, authEnvironment)
162+ FetchAuthSessionCognitoActions .refreshUserPoolTokensAction(signedInData)
163+ .execute(dispatcher, authEnvironment)
150164
151165 val event = capturedEvent.captured.shouldBeInstanceOf<RefreshSessionEvent >()
152166 val refreshedData = event.eventType.shouldBeInstanceOf<RefreshSessionEvent .EventType .Refreshed >().signedInData
@@ -155,40 +169,50 @@ class FetchAuthSessionCognitoActionsTest {
155169
156170 @Test
157171 fun `refreshUserPoolTokensAction handles NotAuthorizedException` () = runTest {
158- coEvery { cognitoIdentityProviderClientMock.getTokensFromRefreshToken(any()) } throws NotAuthorizedException {
159- message = " Token expired"
172+ coEvery {
173+ cognitoIdentityProviderClientMock.getTokensFromRefreshToken(any())
174+ } throws NotAuthorizedException {
175+ message = " Token expired"
160176 }
161177
162178 val signedInData = mockSignedInData(
163179 username = " username" ,
164180 cognitoUserPoolTokens = CognitoUserPoolTokens (
165- accessToken = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ.XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o" ,
181+ accessToken = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." +
182+ " eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ." +
183+ " XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o" ,
166184 idToken = " old_id" ,
167185 refreshToken = " refresh_token" ,
168186 expiration = 0
169187 )
170188 )
171189
172- FetchAuthSessionCognitoActions .refreshUserPoolTokensAction(signedInData).execute(dispatcher, authEnvironment)
190+ FetchAuthSessionCognitoActions .refreshUserPoolTokensAction(signedInData)
191+ .execute(dispatcher, authEnvironment)
173192
174193 capturedEvent.captured.shouldBeInstanceOf<AuthorizationEvent >()
175194 }
176195
177196 @Test
178197 fun `refreshUserPoolTokensAction handles generic exception` () = runTest {
179- coEvery { cognitoIdentityProviderClientMock.getTokensFromRefreshToken(any()) } throws RuntimeException (" Network error" )
198+ coEvery {
199+ cognitoIdentityProviderClientMock.getTokensFromRefreshToken(any())
200+ } throws RuntimeException (" Network error" )
180201
181202 val signedInData = mockSignedInData(
182203 username = " username" ,
183204 cognitoUserPoolTokens = CognitoUserPoolTokens (
184- accessToken = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ.XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o" ,
205+ accessToken = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." +
206+ " eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ." +
207+ " XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o" ,
185208 idToken = " old_id" ,
186209 refreshToken = " refresh_token" ,
187210 expiration = 0
188211 )
189212 )
190213
191- FetchAuthSessionCognitoActions .refreshUserPoolTokensAction(signedInData).execute(dispatcher, authEnvironment)
214+ FetchAuthSessionCognitoActions .refreshUserPoolTokensAction(signedInData)
215+ .execute(dispatcher, authEnvironment)
192216
193217 capturedEvent.captured.shouldBeInstanceOf<AuthorizationEvent >()
194218 }
@@ -198,32 +222,42 @@ class FetchAuthSessionCognitoActionsTest {
198222 every { configuration.identityPool } returns mockk {
199223 every { poolId } returns " identity_pool_id"
200224 }
201-
202- coEvery { cognitoIdentityProviderClientMock.getTokensFromRefreshToken(any()) } returns GetTokensFromRefreshTokenResponse {
225+
226+ coEvery {
227+ cognitoIdentityProviderClientMock.getTokensFromRefreshToken(any())
228+ } returns GetTokensFromRefreshTokenResponse {
203229 authenticationResult = null
204230 }
205231
206232 val signedInData = mockSignedInData(
207233 username = " username" ,
208234 cognitoUserPoolTokens = CognitoUserPoolTokens (
209- accessToken = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ.XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o" ,
235+ accessToken = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." +
236+ " eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ." +
237+ " XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o" ,
210238 idToken = " old_id" ,
211239 refreshToken = " refresh_token" ,
212240 expiration = 0
213241 )
214242 )
215243
216- FetchAuthSessionCognitoActions .refreshUserPoolTokensAction(signedInData).execute(dispatcher, authEnvironment)
244+ FetchAuthSessionCognitoActions .refreshUserPoolTokensAction(signedInData)
245+ .execute(dispatcher, authEnvironment)
217246
218247 capturedEvent.captured.shouldBeInstanceOf<AuthorizationEvent >()
219248 }
220249
221250 @Test
222251 fun `refreshUserPoolTokensAction includes device key when available` () = runTest {
223- val requestSlot = slot< aws.sdk.kotlin.services.cognitoidentityprovider.model.GetTokensFromRefreshTokenRequest > ()
224- coEvery { cognitoIdentityProviderClientMock.getTokensFromRefreshToken(capture(requestSlot)) } returns GetTokensFromRefreshTokenResponse {
252+ val requestSlot =
253+ slot< aws.sdk.kotlin.services.cognitoidentityprovider.model.GetTokensFromRefreshTokenRequest > ()
254+ coEvery {
255+ cognitoIdentityProviderClientMock.getTokensFromRefreshToken(capture(requestSlot))
256+ } returns GetTokensFromRefreshTokenResponse {
225257 authenticationResult = AuthenticationResultType {
226- this .accessToken = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ.XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o"
258+ this .accessToken = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." +
259+ " eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ." +
260+ " XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o"
227261 this .idToken = " id_token"
228262 this .refreshToken = " new_refresh_token"
229263 this .expiresIn = 3600
@@ -233,26 +267,36 @@ class FetchAuthSessionCognitoActionsTest {
233267 val signedInData = mockSignedInData(
234268 username = " username" ,
235269 cognitoUserPoolTokens = CognitoUserPoolTokens (
236- accessToken = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ.XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o" ,
270+ accessToken = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." +
271+ " eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ." +
272+ " XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o" ,
237273 idToken = " old_id" ,
238274 refreshToken = " refresh_token" ,
239275 expiration = 0
240276 )
241277 )
242278
243- FetchAuthSessionCognitoActions .refreshUserPoolTokensAction(signedInData).execute(dispatcher, authEnvironment)
279+ FetchAuthSessionCognitoActions .refreshUserPoolTokensAction(signedInData)
280+ .execute(dispatcher, authEnvironment)
244281
245282 requestSlot.captured.deviceKey shouldBe " device_key"
246283 }
247284
248285 @Test
249286 fun `refreshUserPoolTokensAction works without device metadata` () = runTest {
250- coEvery { credentialStoreClient.loadCredentials(any<CredentialType .Device >()) } returns AmplifyCredential .DeviceData (DeviceMetadata .Empty )
251-
252- val requestSlot = slot< aws.sdk.kotlin.services.cognitoidentityprovider.model.GetTokensFromRefreshTokenRequest > ()
253- coEvery { cognitoIdentityProviderClientMock.getTokensFromRefreshToken(capture(requestSlot)) } returns GetTokensFromRefreshTokenResponse {
287+ coEvery {
288+ credentialStoreClient.loadCredentials(any<CredentialType .Device >())
289+ } returns AmplifyCredential .DeviceData (DeviceMetadata .Empty )
290+
291+ val requestSlot =
292+ slot< aws.sdk.kotlin.services.cognitoidentityprovider.model.GetTokensFromRefreshTokenRequest > ()
293+ coEvery {
294+ cognitoIdentityProviderClientMock.getTokensFromRefreshToken(capture(requestSlot))
295+ } returns GetTokensFromRefreshTokenResponse {
254296 authenticationResult = AuthenticationResultType {
255- this .accessToken = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ.XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o"
297+ this .accessToken = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." +
298+ " eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ." +
299+ " XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o"
256300 this .idToken = " id_token"
257301 this .refreshToken = " new_refresh_token"
258302 this .expiresIn = 3600
@@ -262,16 +306,111 @@ class FetchAuthSessionCognitoActionsTest {
262306 val signedInData = mockSignedInData(
263307 username = " username" ,
264308 cognitoUserPoolTokens = CognitoUserPoolTokens (
265- accessToken = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ.XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o" ,
309+ accessToken = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." +
310+ " eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ." +
311+ " XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o" ,
266312 idToken = " old_id" ,
267313 refreshToken = " refresh_token" ,
268314 expiration = 0
269315 )
270316 )
271317
272- FetchAuthSessionCognitoActions .refreshUserPoolTokensAction(signedInData).execute(dispatcher, authEnvironment)
318+ FetchAuthSessionCognitoActions .refreshUserPoolTokensAction(signedInData)
319+ .execute(dispatcher, authEnvironment)
273320
274321 requestSlot.captured.deviceKey shouldBe null
275322 capturedEvent.captured.shouldBeInstanceOf<RefreshSessionEvent >()
276323 }
277- }
324+
325+ @Test
326+ fun `refreshUserPoolTokensAction works without client secret` () = runTest {
327+ val poolWithoutSecret = mockk<UserPoolConfiguration > {
328+ every { appClient } returns " client"
329+ every { appClientSecret } returns null
330+ every { region } returns " us-east-1"
331+ every { poolId } returns " pool_id"
332+ }
333+ val configWithoutSecret = mockk<AuthConfiguration > {
334+ every { userPool } returns poolWithoutSecret
335+ every { identityPool } returns null
336+ }
337+ val envWithoutSecret = AuthEnvironment (
338+ ApplicationProvider .getApplicationContext(),
339+ configWithoutSecret,
340+ cognitoAuthService,
341+ credentialStoreClient,
342+ null ,
343+ null ,
344+ logger
345+ )
346+
347+ val requestSlot =
348+ slot< aws.sdk.kotlin.services.cognitoidentityprovider.model.GetTokensFromRefreshTokenRequest > ()
349+ coEvery {
350+ cognitoIdentityProviderClientMock.getTokensFromRefreshToken(capture(requestSlot))
351+ } returns GetTokensFromRefreshTokenResponse {
352+ authenticationResult = AuthenticationResultType {
353+ this .accessToken = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." +
354+ " eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ." +
355+ " XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o"
356+ this .idToken = " id_token"
357+ this .refreshToken = " new_refresh_token"
358+ this .expiresIn = 3600
359+ }
360+ }
361+
362+ val signedInData = mockSignedInData(
363+ username = " username" ,
364+ cognitoUserPoolTokens = CognitoUserPoolTokens (
365+ accessToken = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." +
366+ " eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ." +
367+ " XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o" ,
368+ idToken = " old_id" ,
369+ refreshToken = " refresh_token" ,
370+ expiration = 0
371+ )
372+ )
373+
374+ FetchAuthSessionCognitoActions .refreshUserPoolTokensAction(signedInData)
375+ .execute(dispatcher, envWithoutSecret)
376+
377+ requestSlot.captured.clientSecret shouldBe null
378+ capturedEvent.captured.shouldBeInstanceOf<RefreshSessionEvent >()
379+ }
380+
381+ @Test
382+ fun `refreshUserPoolTokensAction includes client secret when available` () = runTest {
383+ val requestSlot =
384+ slot< aws.sdk.kotlin.services.cognitoidentityprovider.model.GetTokensFromRefreshTokenRequest > ()
385+ coEvery {
386+ cognitoIdentityProviderClientMock.getTokensFromRefreshToken(capture(requestSlot))
387+ } returns GetTokensFromRefreshTokenResponse {
388+ authenticationResult = AuthenticationResultType {
389+ this .accessToken = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." +
390+ " eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ." +
391+ " XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o"
392+ this .idToken = " id_token"
393+ this .refreshToken = " new_refresh_token"
394+ this .expiresIn = 3600
395+ }
396+ }
397+
398+ val signedInData = mockSignedInData(
399+ username = " username" ,
400+ cognitoUserPoolTokens = CognitoUserPoolTokens (
401+ accessToken = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." +
402+ " eyJzdWIiOiJ1c2VySWQiLCJ1c2VybmFtZSI6InVzZXJuYW1lIiwiZXhwIjoxNTE2MjM5MDIyfQ." +
403+ " XbPfbIHMI6arZ3Y922BhjWgQzWXcXNrz0ogtVhfEd2o" ,
404+ idToken = " old_id" ,
405+ refreshToken = " refresh_token" ,
406+ expiration = 0
407+ )
408+ )
409+
410+ FetchAuthSessionCognitoActions .refreshUserPoolTokensAction(signedInData)
411+ .execute(dispatcher, authEnvironment)
412+
413+ requestSlot.captured.clientSecret shouldBe " secret"
414+ capturedEvent.captured.shouldBeInstanceOf<RefreshSessionEvent >()
415+ }
416+ }
0 commit comments