66import static org .hamcrest .Matchers .equalTo ;
77import static org .hamcrest .Matchers .is ;
88import static org .junit .jupiter .api .Assertions .assertEquals ;
9+ import static org .junit .jupiter .api .Assertions .assertFalse ;
910import static org .junit .jupiter .api .Assertions .assertNotEquals ;
1011import static org .junit .jupiter .api .Assertions .assertNotNull ;
1112import static org .junit .jupiter .api .Assertions .assertNull ;
@@ -202,7 +203,7 @@ private void testTenantWebApp2(String webApp2SubPath, String expectedResult) thr
202203 }
203204
204205 @ Test
205- public void testCodeFlowRefreshTokens () throws IOException , InterruptedException {
206+ public void testCodeFlowRefreshTokensWhenIdTokenIsExpired () throws Exception {
206207 try (final WebClient webClient = createWebClient ()) {
207208 HtmlPage page = webClient .getPage ("http://localhost:8081/tenant-refresh/tenant-web-app-refresh/api/user" );
208209 assertEquals ("Sign in to quarkus-webapp" , page .getTitleText ());
@@ -213,6 +214,8 @@ public void testCodeFlowRefreshTokens() throws IOException, InterruptedException
213214
214215 Cookie sessionCookie = getSessionCookie (page .getWebClient (), "tenant-web-app-refresh" );
215216 assertNotNull (sessionCookie );
217+ JsonObject jwtHeaders = getIdTokenHeaders (sessionCookie .getValue ());
218+ assertFalse (jwtHeaders .getBoolean ("internal" , false ));
216219
217220 Set <Cookie > atSessionCookies = getSessionAtCookie (page .getWebClient (), "tenant-web-app-refresh" );
218221 assertEquals (3 , atSessionCookies .size ());
@@ -225,10 +228,66 @@ public void testCodeFlowRefreshTokens() throws IOException, InterruptedException
225228 + ", refreshToken: true" ,
226229 page .getBody ().asNormalizedText ());
227230
228- // Wait till the session expires - which should cause the first and also last token refresh request,
229- // id and access tokens should have new values, refresh token value should remain the same.
230- // No new sign-in process is required.
231- //await().atLeast(6, TimeUnit.SECONDS);
231+ Thread .sleep (6 * 1000 );
232+
233+ webClient .getOptions ().setRedirectEnabled (false );
234+ WebResponse webResponse = webClient
235+ .loadWebResponse (new WebRequest (
236+ URI .create ("http://localhost:8081/tenant-refresh/tenant-web-app-refresh/api/user" )
237+ .toURL ()));
238+
239+ Cookie sessionCookie2 = getSessionCookie (webClient , "tenant-web-app-refresh" );
240+ assertNotNull (sessionCookie2 );
241+ assertNotEquals (sessionCookie2 .getValue (), sessionCookie .getValue ());
242+ JsonObject jwtHeaders2 = getIdTokenHeaders (sessionCookie2 .getValue ());
243+ assertTrue (jwtHeaders2 .getBoolean ("internal" ));
244+
245+ atSessionCookies = getSessionAtCookie (page .getWebClient (), "tenant-web-app-refresh" );
246+ assertEquals (3 , atSessionCookies .size ());
247+ Cookie rtCookie2 = getSessionRtCookie (webClient , "tenant-web-app-refresh" );
248+ assertNotNull (rtCookie2 );
249+ assertEquals (rtCookie2 .getValue (), rtCookie .getValue ());
250+
251+ assertEquals ("userName: alice, idToken: true, accessToken: true, accessTokenLongStringClaim: "
252+ + getAccessTokenLongStringClaim (atSessionCookies )
253+ + ", refreshToken: true" ,
254+ webResponse .getContentAsString ());
255+
256+ webClient .getCookieManager ().clearCookies ();
257+ }
258+ }
259+
260+ private static JsonObject getIdTokenHeaders (String value ) throws Exception {
261+ return OidcUtils .decodeJwtHeaders (value );
262+ }
263+
264+ @ Test
265+ public void testCodeFlowRefreshTokensWhileIdTokenIsValid () throws Exception {
266+ try (final WebClient webClient = createWebClient ()) {
267+ HtmlPage page = webClient .getPage ("http://localhost:8081/tenant-refresh/tenant-web-app-refresh/api/user" );
268+ assertEquals ("Sign in to quarkus-webapp" , page .getTitleText ());
269+ HtmlForm loginForm = page .getForms ().get (0 );
270+ loginForm .getInputByName ("username" ).setValueAttribute ("alice" );
271+ loginForm .getInputByName ("password" ).setValueAttribute ("alice" );
272+ page = loginForm .getButtonByName ("login" ).click ();
273+
274+ Cookie sessionCookie = getSessionCookie (page .getWebClient (), "tenant-web-app-refresh" );
275+ assertNotNull (sessionCookie );
276+ JsonObject jwtHeaders = getIdTokenHeaders (sessionCookie .getValue ());
277+ assertFalse (jwtHeaders .getBoolean ("internal" , false ));
278+
279+ Set <Cookie > atSessionCookies = getSessionAtCookie (page .getWebClient (), "tenant-web-app-refresh" );
280+ assertEquals (3 , atSessionCookies .size ());
281+
282+ Cookie rtCookie = getSessionRtCookie (page .getWebClient (), "tenant-web-app-refresh" );
283+ assertNotNull (rtCookie );
284+
285+ assertEquals ("userName: alice, idToken: true, accessToken: true, accessTokenLongStringClaim: "
286+ + getAccessTokenLongStringClaim (atSessionCookies )
287+ + ", refreshToken: true" ,
288+ page .getBody ().asNormalizedText ());
289+
290+ // Wait till a valid ID token is within the refresh token skew
232291 Thread .sleep (2 * 1000 );
233292
234293 webClient .getOptions ().setRedirectEnabled (false );
@@ -240,6 +299,10 @@ public void testCodeFlowRefreshTokens() throws IOException, InterruptedException
240299 Cookie sessionCookie2 = getSessionCookie (webClient , "tenant-web-app-refresh" );
241300 assertNotNull (sessionCookie2 );
242301 assertEquals (sessionCookie2 .getValue (), sessionCookie .getValue ());
302+
303+ JsonObject jwtHeaders2 = getIdTokenHeaders (sessionCookie2 .getValue ());
304+ assertFalse (jwtHeaders2 .getBoolean ("internal" , false ));
305+
243306 atSessionCookies = getSessionAtCookie (page .getWebClient (), "tenant-web-app-refresh" );
244307 assertEquals (3 , atSessionCookies .size ());
245308 Cookie rtCookie2 = getSessionRtCookie (webClient , "tenant-web-app-refresh" );
0 commit comments