@Test(expected = InvalidAuthenticationException.class) public void testBadSignedJwt() throws Exception { final String subject = "user-1"; final Date expiration = new Date(System.currentTimeMillis() + TimeUnit.MILLISECONDS.convert(5, TimeUnit.SECONDS)); final KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); final KeyPair pair1 = keyGen.generateKeyPair(); final RSAPrivateKey privateKey1 = (RSAPrivateKey) pair1.getPrivate(); final KeyPair pair2 = keyGen.generateKeyPair(); final RSAPublicKey publicKey2 = (RSAPublicKey) pair2.getPublic(); final JWTAuthenticationClaimsSet claimsSet = getAuthenticationClaimsSet(subject, AUDIENCE, expiration); final PrivateKeyJWT privateKeyJWT = new PrivateKeyJWT(claimsSet, JWSAlgorithm.RS256, privateKey1, null, null); final KnoxConfiguration configuration = getConfiguration(publicKey2); final KnoxService service = new KnoxService(configuration); service.getAuthenticationFromToken(privateKeyJWT.getClientAssertion().serialize()); }
public String getAuthenticationFromToken(final String jwt) throws ParseException, JOSEException { if (!configuration.isKnoxEnabled()) { throw new IllegalStateException("Apache Knox SSO is not enabled."); } final SignedJWT signedJwt = SignedJWT.parse(jwt); if (validateToken(signedJwt)) { final JWTClaimsSet claimsSet = signedJwt.getJWTClaimsSet(); if (claimsSet == null) { logger.info("Claims set is missing from Knox JWT."); throw new InvalidAuthenticationException("The Knox JWT token is not valid."); } return claimsSet.getSubject(); } else { throw new InvalidAuthenticationException("The Knox JWT token is not valid."); } }
KnoxService { public String getAuthenticationFromToken(final String jwt) throws ParseException, JOSEException { if (!configuration.isKnoxEnabled()) { throw new IllegalStateException("Apache Knox SSO is not enabled."); } final SignedJWT signedJwt = SignedJWT.parse(jwt); if (validateToken(signedJwt)) { final JWTClaimsSet claimsSet = signedJwt.getJWTClaimsSet(); if (claimsSet == null) { logger.info("Claims set is missing from Knox JWT."); throw new InvalidAuthenticationException("The Knox JWT token is not valid."); } return claimsSet.getSubject(); } else { throw new InvalidAuthenticationException("The Knox JWT token is not valid."); } } }
KnoxService { public String getAuthenticationFromToken(final String jwt) throws ParseException, JOSEException { if (!configuration.isKnoxEnabled()) { throw new IllegalStateException("Apache Knox SSO is not enabled."); } final SignedJWT signedJwt = SignedJWT.parse(jwt); if (validateToken(signedJwt)) { final JWTClaimsSet claimsSet = signedJwt.getJWTClaimsSet(); if (claimsSet == null) { logger.info("Claims set is missing from Knox JWT."); throw new InvalidAuthenticationException("The Knox JWT token is not valid."); } return claimsSet.getSubject(); } else { throw new InvalidAuthenticationException("The Knox JWT token is not valid."); } } KnoxService(final KnoxConfiguration configuration); }
KnoxService { public String getAuthenticationFromToken(final String jwt) throws ParseException, JOSEException { if (!configuration.isKnoxEnabled()) { throw new IllegalStateException("Apache Knox SSO is not enabled."); } final SignedJWT signedJwt = SignedJWT.parse(jwt); if (validateToken(signedJwt)) { final JWTClaimsSet claimsSet = signedJwt.getJWTClaimsSet(); if (claimsSet == null) { logger.info("Claims set is missing from Knox JWT."); throw new InvalidAuthenticationException("The Knox JWT token is not valid."); } return claimsSet.getSubject(); } else { throw new InvalidAuthenticationException("The Knox JWT token is not valid."); } } KnoxService(final KnoxConfiguration configuration); boolean isKnoxEnabled(); String getKnoxUrl(); String getAuthenticationFromToken(final String jwt); }
KnoxService { public String getAuthenticationFromToken(final String jwt) throws ParseException, JOSEException { if (!configuration.isKnoxEnabled()) { throw new IllegalStateException("Apache Knox SSO is not enabled."); } final SignedJWT signedJwt = SignedJWT.parse(jwt); if (validateToken(signedJwt)) { final JWTClaimsSet claimsSet = signedJwt.getJWTClaimsSet(); if (claimsSet == null) { logger.info("Claims set is missing from Knox JWT."); throw new InvalidAuthenticationException("The Knox JWT token is not valid."); } return claimsSet.getSubject(); } else { throw new InvalidAuthenticationException("The Knox JWT token is not valid."); } } KnoxService(final KnoxConfiguration configuration); boolean isKnoxEnabled(); String getKnoxUrl(); String getAuthenticationFromToken(final String jwt);  }