JWTAuthenticator implements WebappAuthenticator { @Override public AuthenticationInfo authenticate(Request request, Response response) { String requestUri = request.getRequestURI(); SignedJWT jwsObject; String username; String tenantDomain; int tenantId; String issuer; AuthenticationInfo authenticationInfo = new AuthenticationInfo(); if (requestUri == null || "".equals(requestUri)) { authenticationInfo.setStatus(Status.CONTINUE); } if (requestUri == null) { requestUri = ""; } StringTokenizer tokenizer = new StringTokenizer(requestUri, "/"); String context = tokenizer.hasMoreTokens() ? tokenizer.nextToken() : null; if (context == null || "".equals(context)) { authenticationInfo.setStatus(Status.CONTINUE); } try { String authorizationHeader = request.getHeader(JWT_ASSERTION_HEADER); jwsObject = SignedJWT.parse(authorizationHeader); username = jwsObject.getJWTClaimsSet().getStringClaim(SIGNED_JWT_AUTH_USERNAME); tenantDomain = MultitenantUtils.getTenantDomain(username); tenantId = Integer.parseInt(jwsObject.getJWTClaimsSet().getStringClaim(SIGNED_JWT_AUTH_TENANT_ID)); issuer = jwsObject.getJWTClaimsSet().getIssuer(); } catch (ParseException e) { log.error("Error occurred while parsing JWT header.", e); authenticationInfo.setMessage("Error occurred while parsing JWT header"); return authenticationInfo; } try { PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId); IssuerAlias issuerAlias = new IssuerAlias(issuer, tenantDomain); PublicKey publicKey = publicKeyHolder.get(issuerAlias); if (publicKey == null) { loadTenantRegistry(tenantId); KeyStoreManager keyStoreManager = KeyStoreManager.getInstance(tenantId); if (MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) { String alias = properties == null ? null : properties.getProperty(issuer); if (alias != null && !alias.isEmpty()) { ServerConfiguration serverConfig = CarbonUtils.getServerConfiguration(); KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); String trustStorePath = serverConfig.getFirstProperty(DEFAULT_TRUST_STORE_LOCATION); String trustStorePassword = serverConfig.getFirstProperty( DEFAULT_TRUST_STORE_PASSWORD); keyStore.load(new FileInputStream(trustStorePath), trustStorePassword.toCharArray()); java.security.cert.Certificate certificate = keyStore.getCertificate(alias); publicKey = certificate == null ? null : certificate.getPublicKey(); } else { authenticationInfo.setStatus(Status.FAILURE); return authenticationInfo; } } else { String ksName = tenantDomain.trim().replace('.', '-'); String jksName = ksName + ".jks"; publicKey = keyStoreManager.getKeyStore(jksName).getCertificate(tenantDomain).getPublicKey(); } if (publicKey != null) { issuerAlias = new IssuerAlias(tenantDomain); publicKeyHolder.put(issuerAlias, publicKey); } } JWSVerifier verifier = null; if (publicKey != null) { verifier = new RSASSAVerifier((RSAPublicKey) publicKey); } if (verifier != null && jwsObject.verify(verifier)) { username = MultitenantUtils.getTenantAwareUsername(username); UserStoreManager userStore = AuthenticatorFrameworkDataHolder.getInstance().getRealmService(). getTenantUserRealm(tenantId).getUserStoreManager(); if (userStore.isExistingUser(username)) { authenticationInfo.setTenantId(tenantId); authenticationInfo.setUsername(username); authenticationInfo.setTenantDomain(tenantDomain); authenticationInfo.setStatus(Status.CONTINUE); } else { authenticationInfo.setStatus(Status.FAILURE); } } else { authenticationInfo.setStatus(Status.FAILURE); } } catch (UserStoreException e) { log.error("Error occurred while obtaining the user.", e); authenticationInfo.setStatus(Status.FAILURE); } catch (Exception e) { log.error("Error occurred while verifying the JWT header.", e); authenticationInfo.setStatus(Status.FAILURE); } finally { PrivilegedCarbonContext.endTenantFlow(); } return authenticationInfo; }  @Override void init(); @Override boolean canHandle(Request request); @Override AuthenticationInfo authenticate(Request request, Response response); @Override String getName(); @Override Properties getProperties(); @Override void setProperties(Properties properties); @Override String getProperty(String name);  }
@Test(description = "This method tests authenticate method under the successful condition", dependsOnMethods = { "testAuthenticateFailureScenarios" }) public void testAuthenticate() throws IllegalAccessException, NoSuchFieldException { Request request = createJWTRequest(jwtToken, "test"); AuthenticationInfo authenticationInfo = jwtAuthenticator.authenticate(request, null); Assert.assertNotNull(authenticationInfo.getUsername(), "Proper authentication request is not properly " + "authenticated by the JWTAuthenticator"); }