AbstractBearerTokenExtractor { public String getBearerToken() { final String tokenHeaderName = authContextInfo.getTokenHeader(); final boolean fallbackToHeader = authContextInfo.isAlwaysCheckAuthorization(); AuthLogging.log.tokenHeaderName(tokenHeaderName); final String bearerValue; if (COOKIE_HEADER.equals(tokenHeaderName)) { String intermediateBearerValue = getBearerTokenCookie(); if (intermediateBearerValue == null && fallbackToHeader) { bearerValue = getBearerTokenAuthHeader(); } else { bearerValue = intermediateBearerValue; } } else if (AUTHORIZATION_HEADER.equals(tokenHeaderName)) { bearerValue = getBearerTokenAuthHeader(); } else { String customHeaderValue = getHeaderValue(tokenHeaderName); if (customHeaderValue == null) { AuthLogging.log.headerIsNull(tokenHeaderName); } else { String customHeaderSchemeValue = getTokenWithConfiguredScheme(customHeaderValue); if (customHeaderSchemeValue != null) { customHeaderValue = customHeaderSchemeValue; } } bearerValue = customHeaderValue; } return bearerValue; } protected  AbstractBearerTokenExtractor(JWTAuthContextInfo authContextInfo); String getBearerToken();  }
@Test public void testGetBearerTokenAuthorizationHeader() { when(authContextInfo.getTokenHeader()).thenReturn(AUTHORIZATION); when(authContextInfo.getTokenSchemes()).thenReturn(BEARER_SCHEME); AbstractBearerTokenExtractor target = newTarget(h -> "Bearer THE_TOKEN", c -> null); String bearerToken = target.getBearerToken(); assertEquals("THE_TOKEN", bearerToken); }
@Test public void testGetBearerTokenCustomCookieHeader() { when(authContextInfo.getTokenHeader()).thenReturn(COOKIE); when(authContextInfo.getTokenCookie()).thenReturn("CustomCookie"); AbstractBearerTokenExtractor target = newTarget(h -> null, c -> { if ("CustomCookie".equals(c)) { return "THE_TOKEN"; } return null; }); String bearerToken = target.getBearerToken(); assertEquals("THE_TOKEN", bearerToken); }
@Test public void testGetBearerTokenMissingCookieHeader() { when(authContextInfo.getTokenHeader()).thenReturn(COOKIE); AbstractBearerTokenExtractor target = newTarget(h -> null, c -> null); String bearerToken = target.getBearerToken(); assertNull(bearerToken); }
@Test public void testGetBearerTokenFallbackToHeaderWithCookieHeader() { when(authContextInfo.getTokenHeader()).thenReturn(COOKIE); when(authContextInfo.isAlwaysCheckAuthorization()).thenReturn(true); AbstractBearerTokenExtractor target = newTarget(h -> "Bearer THE_HEADER_TOKEN", c -> "THE_COOKIE_TOKEN"); String bearerToken = target.getBearerToken(); assertEquals("THE_COOKIE_TOKEN", bearerToken); }
@Test public void testGetBearerTokenFallbackToHeaderWithEmptyCookie() { when(authContextInfo.getTokenHeader()).thenReturn(COOKIE); when(authContextInfo.getTokenSchemes()).thenReturn(BEARER_SCHEME); when(authContextInfo.isAlwaysCheckAuthorization()).thenReturn(true); AbstractBearerTokenExtractor target = newTarget(h -> "Bearer THE_TOKEN", c -> null); String bearerToken = target.getBearerToken(); assertEquals("THE_TOKEN", bearerToken); }
@Test public void testGetBearerTokenAuthorizationHeaderMixedCase() { when(authContextInfo.getTokenHeader()).thenReturn(AUTHORIZATION); when(authContextInfo.getTokenSchemes()).thenReturn(BEARER_SCHEME); AbstractBearerTokenExtractor target = newTarget(h -> "bEaReR THE_TOKEN", c -> null); String bearerToken = target.getBearerToken(); assertEquals("THE_TOKEN", bearerToken); }
@Test public void testGetBearerTokenAuthorizationHeaderBlank() { when(authContextInfo.getTokenHeader()).thenReturn(AUTHORIZATION); when(authContextInfo.getTokenSchemes()).thenReturn(BEARER_SCHEME); AbstractBearerTokenExtractor target = newTarget(h -> "BEARER ", c -> null); String bearerToken = target.getBearerToken(); assertEquals("", bearerToken); }
@Test public void testGetBearerTokenAuthorizationHeaderInvalidSchemePrefix() { when(authContextInfo.getTokenHeader()).thenReturn(AUTHORIZATION); when(authContextInfo.getTokenSchemes()).thenReturn(BEARER_SCHEME); AbstractBearerTokenExtractor target = newTarget(h -> "BEARER", c -> null); String bearerToken = target.getBearerToken(); assertNull(bearerToken); }
@Test public void testGetBearerTokenMissingAuthorizationHeader() { when(authContextInfo.getTokenHeader()).thenReturn(AUTHORIZATION); when(authContextInfo.getTokenSchemes()).thenReturn(BEARER_SCHEME); AbstractBearerTokenExtractor target = newTarget(h -> null, c -> null); String bearerToken = target.getBearerToken(); assertNull(bearerToken); }
@Test public void testGetBearerTokenOtherSchemeAuthorizationHeader() { when(authContextInfo.getTokenHeader()).thenReturn(AUTHORIZATION); when(authContextInfo.getTokenSchemes()).thenReturn(BEARER_SCHEME); AbstractBearerTokenExtractor target = newTarget(h -> "Basic Not_a_JWT", c -> null); String bearerToken = target.getBearerToken(); assertNull(bearerToken); }
@Test public void testGetBearerTokenCustomSchemeAuthorizationHeader() { when(authContextInfo.getTokenHeader()).thenReturn(AUTHORIZATION); when(authContextInfo.getTokenSchemes()).thenReturn(Arrays.asList("Bearer", "DPoP")); AbstractBearerTokenExtractor target = newTarget(h -> "DPoP THE_TOKEN", c -> null); String bearerToken = target.getBearerToken(); assertEquals("THE_TOKEN", bearerToken); }
@Test public void testGetBearerTokenCustomHeader() { when(authContextInfo.getTokenHeader()).thenReturn("MyHeader"); AbstractBearerTokenExtractor target = newTarget(h -> { if ("MyHeader".equals(h)) { return "THE_CUSTOM_TOKEN"; } return null; }, c -> null); String bearerToken = target.getBearerToken(); assertEquals("THE_CUSTOM_TOKEN", bearerToken); }
@Test public void testGetBearerTokenCustomHeaderAndScheme() { when(authContextInfo.getTokenHeader()).thenReturn("MyHeader"); when(authContextInfo.getTokenSchemes()).thenReturn(Arrays.asList("DPoP")); AbstractBearerTokenExtractor target = newTarget(h -> { if ("MyHeader".equals(h)) { return "DPoP THE_CUSTOM_TOKEN"; } return null; }, c -> null); String bearerToken = target.getBearerToken(); assertEquals("THE_CUSTOM_TOKEN", bearerToken); }
@Test public void testGetBearerTokenDefaultCookieHeader() { when(authContextInfo.getTokenHeader()).thenReturn(COOKIE); AbstractBearerTokenExtractor target = newTarget(h -> null, c -> "THE_TOKEN"); String bearerToken = target.getBearerToken(); assertEquals("THE_TOKEN", bearerToken); }