Java Code Examples for org.eclipse.microprofile.jwt.tck.util.TokenUtils#readPublicKey()

The following examples show how to use org.eclipse.microprofile.jwt.tck.util.TokenUtils#readPublicKey() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestTokenRequireSub.java    From smallrye-jwt with Apache License 2.0 6 votes vote down vote up
@Test(groups = TEST_GROUP_JWT, description = "no sub validation")
public void noSubValidation() throws Exception {
    HashMap<String, Long> timeClaims = new HashMap<>();
    String token = TokenUtils.generateTokenString("/TokenSubPath.json", null, timeClaims);
    PublicKey publicKey = TokenUtils.readPublicKey("/publicKey.pem");
    if (publicKey == null) {
        throw new IllegalStateException("Failed to load /publicKey.pem resource");
    }

    JWTAuthContextInfo contextInfo = new JWTAuthContextInfo((RSAPublicKey) publicKey, TEST_ISSUER);
    contextInfo.setRequireNamedPrincipal(false);
    JWTCallerPrincipalFactory factory = JWTCallerPrincipalFactory.instance();
    JsonWebToken jwt = factory.parse(token, contextInfo);
    String sub = jwt.getSubject();
    Assert.assertNull(sub);
}
 
Example 2
Source File: TestTokenClaimTypes.java    From smallrye-jwt with Apache License 2.0 6 votes vote down vote up
@BeforeClass(alwaysRun = true)
public static void generateToken() throws Exception {
    HashMap<String, Long> timeClaims = new HashMap<>();
    token = TokenUtils.generateTokenString("/Token1.json", null, timeClaims);
    iatClaim = timeClaims.get(Claims.iat.name());
    authTimeClaim = timeClaims.get(Claims.auth_time.name());
    expClaim = timeClaims.get(Claims.exp.name());

    System.out.printf("TokenValidationTest.initClass\n");
    publicKey = TokenUtils.readPublicKey("/publicKey.pem");
    if (publicKey == null) {
        throw new IllegalStateException("Failed to load /publicKey.pem resource");
    }

    JWTAuthContextInfo contextInfo = new JWTAuthContextInfo((RSAPublicKey) publicKey, TEST_ISSUER);
    JWTCallerPrincipalFactory factory = JWTCallerPrincipalFactory.instance();
    jwt = factory.parse(token, contextInfo);
}
 
Example 3
Source File: TestTokenWithGroupsPath2.java    From smallrye-jwt with Apache License 2.0 5 votes vote down vote up
@BeforeClass(alwaysRun = true)
public static void generateToken() throws Exception {
    HashMap<String, Long> timeClaims = new HashMap<>();
    token = TokenUtils.generateTokenString("/TokenGroupsPath2.json", null, timeClaims);
    publicKey = TokenUtils.readPublicKey("/publicKey.pem");
    if (publicKey == null) {
        throw new IllegalStateException("Failed to load /publicKey.pem resource");
    }
}
 
Example 4
Source File: AbstractVerifierTest.java    From microprofile-jwt-auth with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidToken() throws Exception {
    String token = TokenUtils.generateTokenString("/Token1.json");
    RSAPublicKey publicKey = (RSAPublicKey) TokenUtils.readPublicKey("/publicKey.pem");
    int expGracePeriodSecs = 60;
    validateToken(token, publicKey, TEST_ISSUER, expGracePeriodSecs);
}
 
Example 5
Source File: AbstractVerifierTest.java    From microprofile-jwt-auth with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = {BadJWSException.class, SignatureVerificationException.class,
    InvalidJwtSignatureException.class, SignatureException.class},
    description = "Illustrate validation of signer")
public void testFailSignature() throws Exception {
    HashSet<TokenUtils.InvalidClaims> invalidFields = new HashSet<>();
    invalidFields.add(TokenUtils.InvalidClaims.SIGNER);
    String token = TokenUtils.generateTokenString("/Token1.json", invalidFields);
    RSAPublicKey publicKey = (RSAPublicKey) TokenUtils.readPublicKey("/publicKey.pem");
    int expGracePeriodSecs = 60;
    validateToken(token, publicKey, TEST_ISSUER, expGracePeriodSecs);
}
 
Example 6
Source File: TestTokenWithGroupsString.java    From smallrye-jwt with Apache License 2.0 5 votes vote down vote up
@BeforeClass(alwaysRun = true)
public static void generateToken() throws Exception {
    HashMap<String, Long> timeClaims = new HashMap<>();
    token = TokenUtils.generateTokenString("/TokenGroupsString.json", null, timeClaims);
    publicKey = TokenUtils.readPublicKey("/publicKey.pem");
    if (publicKey == null) {
        throw new IllegalStateException("Failed to load /publicKey.pem resource");
    }
}
 
Example 7
Source File: AbstractVerifierTest.java    From microprofile-jwt-auth with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = {JOSEException.class, AlgorithmMismatchException.class, InvalidJwtException.class, UnsupportedJwtException.class},
    description = "Illustrate validation of signature algorithm")
public void testFailSignatureAlgorithm() throws Exception {
    HashSet<TokenUtils.InvalidClaims> invalidFields = new HashSet<>();
    invalidFields.add(TokenUtils.InvalidClaims.ALG);
    String token = TokenUtils.generateTokenString("/Token1.json", invalidFields);
    RSAPublicKey publicKey = (RSAPublicKey) TokenUtils.readPublicKey("/publicKey.pem");
    int expGracePeriodSecs = 60;
    validateToken(token, publicKey, TEST_ISSUER, expGracePeriodSecs);
}
 
Example 8
Source File: TestJsonWebToken.java    From smallrye-jwt with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = { ParseException.class }, description = "Illustrate validation of signer")
public void testNimbusFailSignature() throws Exception {
    HashSet<TokenUtils.InvalidClaims> invalidFields = new HashSet<>();
    invalidFields.add(TokenUtils.InvalidClaims.SIGNER);
    String token = TokenUtils.generateTokenString("/Token1.json", invalidFields);
    RSAPublicKey publicKey = (RSAPublicKey) TokenUtils.readPublicKey("/publicKey.pem");
    JWTAuthContextInfo contextInfo = new JWTAuthContextInfo(publicKey, "https://server.example.com");
    contextInfo.setExpGracePeriodSecs(60);
    JsonWebToken jwt = validateToken(token, contextInfo);
}
 
Example 9
Source File: TestJsonWebToken.java    From smallrye-jwt with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = { ParseException.class }, description = "Illustrate validation of issuer")
public void testFailIssuer() throws Exception {
    HashSet<TokenUtils.InvalidClaims> invalidFields = new HashSet<>();
    invalidFields.add(TokenUtils.InvalidClaims.ISSUER);
    String token = TokenUtils.generateTokenString("/Token1.json", invalidFields);
    RSAPublicKey publicKey = (RSAPublicKey) TokenUtils.readPublicKey("/publicKey.pem");
    JWTAuthContextInfo contextInfo = new JWTAuthContextInfo(publicKey, "https://server.example.com");
    contextInfo.setExpGracePeriodSecs(60);
    JsonWebToken jwt = validateToken(token, contextInfo);
}
 
Example 10
Source File: TestJsonWebToken.java    From smallrye-jwt with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidation() throws Exception {
    String token = TokenUtils.generateTokenString("/Token1.json");
    RSAPublicKey publicKey = (RSAPublicKey) TokenUtils.readPublicKey("/publicKey.pem");
    JWTAuthContextInfo contextInfo = new JWTAuthContextInfo(publicKey, "https://server.example.com");
    contextInfo.setExpGracePeriodSecs(60);
    JsonWebToken jwt = validateToken(token, contextInfo);
}
 
Example 11
Source File: DefaultJWTCallerPrincipalTest.java    From smallrye-jwt with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    publicKey = (RSAPublicKey) TokenUtils.readPublicKey("/publicKey.pem");
    parser = new DefaultJWTTokenParser();
    config = new JWTAuthContextInfo(publicKey, "https://server.example.com");
    context = parser.parse(TokenUtils.generateTokenString("/Token1.json"), config);
}
 
Example 12
Source File: AbstractVerifierTest.java    From microprofile-jwt-auth with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = {BadJWTException.class, InvalidJwtException.class, TokenExpiredException.class, ExpiredJwtException.class},
    description = "Illustrate validation of exp")
public void testFailExpired() throws Exception {
    HashMap<String, Long> timeClaims = new HashMap<>();
    HashSet<TokenUtils.InvalidClaims> invalidFields = new HashSet<>();
    invalidFields.add(TokenUtils.InvalidClaims.EXP);
    String token = TokenUtils.generateTokenString("/Token1.json", invalidFields, timeClaims);
    RSAPublicKey publicKey = (RSAPublicKey) TokenUtils.readPublicKey("/publicKey.pem");
    int expGracePeriodSecs = 60;
    validateToken(token, publicKey, TEST_ISSUER, expGracePeriodSecs);
}
 
Example 13
Source File: TestTokenRequiredClaims.java    From smallrye-jwt with Apache License 2.0 5 votes vote down vote up
@Test
public void requiredAndMissingClaims() throws Exception {
    String token = TokenUtils.generateTokenString("/Token1.json");
    PublicKey publicKey = TokenUtils.readPublicKey("/publicKey.pem");
    JWTAuthContextInfo contextInfo = new JWTAuthContextInfo((RSAPublicKey) publicKey, TEST_ISSUER);
    contextInfo.setRequiredClaims(
            Stream.of("roles", "customObject", "customDoubleArray", "something").collect(toSet()));
    JWTCallerPrincipalFactory factory = JWTCallerPrincipalFactory.instance();

    final ParseException exception = assertThrows(ParseException.class, () -> factory.parse(token, contextInfo));
    assertTrue(exception.getCause() instanceof InvalidJwtException);
}
 
Example 14
Source File: TestTokenRequiredClaims.java    From smallrye-jwt with Apache License 2.0 5 votes vote down vote up
@Test
public void requiredClaims() throws Exception {
    String token = TokenUtils.generateTokenString("/Token1.json");
    PublicKey publicKey = TokenUtils.readPublicKey("/publicKey.pem");
    JWTAuthContextInfo contextInfo = new JWTAuthContextInfo((RSAPublicKey) publicKey, TEST_ISSUER);
    contextInfo.setRequiredClaims(Stream.of("roles", "customObject", "customDoubleArray").collect(toSet()));
    JWTCallerPrincipalFactory factory = JWTCallerPrincipalFactory.instance();
    factory.parse(token, contextInfo);
}
 
Example 15
Source File: TestTokenRequireSub.java    From smallrye-jwt with Apache License 2.0 5 votes vote down vote up
@Test(groups = TEST_GROUP_JWT, description = "validate sub fail", expectedExceptions = ParseException.class)
public void defaultSubNotAvailable() throws Exception {
    HashMap<String, Long> timeClaims = new HashMap<>();
    String token = TokenUtils.generateTokenString("/TokenSubPath.json", null, timeClaims);
    PublicKey publicKey = TokenUtils.readPublicKey("/publicKey.pem");
    if (publicKey == null) {
        throw new IllegalStateException("Failed to load /publicKey.pem resource");
    }

    JWTAuthContextInfo contextInfo = new JWTAuthContextInfo((RSAPublicKey) publicKey, TEST_ISSUER);
    JWTCallerPrincipalFactory factory = JWTCallerPrincipalFactory.instance();
    factory.parse(token, contextInfo);
}
 
Example 16
Source File: TestTokenRequiredClaims.java    From smallrye-jwt with Apache License 2.0 5 votes vote down vote up
@Test
public void missingRequiredClaim() throws Exception {
    String token = TokenUtils.generateTokenString("/Token1.json");
    PublicKey publicKey = TokenUtils.readPublicKey("/publicKey.pem");
    JWTAuthContextInfo contextInfo = new JWTAuthContextInfo((RSAPublicKey) publicKey, TEST_ISSUER);
    contextInfo.setRequiredClaims(Collections.singleton("something"));
    JWTCallerPrincipalFactory factory = JWTCallerPrincipalFactory.instance();

    final ParseException exception = assertThrows(ParseException.class, () -> factory.parse(token, contextInfo));
    assertTrue(exception.getCause() instanceof InvalidJwtException);
}
 
Example 17
Source File: TestTokenRequiredClaims.java    From smallrye-jwt with Apache License 2.0 5 votes vote down vote up
@Test
public void base() throws Exception {
    String token = TokenUtils.generateTokenString("/Token1.json");
    PublicKey publicKey = TokenUtils.readPublicKey("/publicKey.pem");
    JWTAuthContextInfo contextInfo = new JWTAuthContextInfo((RSAPublicKey) publicKey, TEST_ISSUER);
    JWTCallerPrincipalFactory factory = JWTCallerPrincipalFactory.instance();
    factory.parse(token, contextInfo);
}
 
Example 18
Source File: TestTokenWithSubPath.java    From smallrye-jwt with Apache License 2.0 5 votes vote down vote up
@BeforeClass(alwaysRun = true)
public static void generateToken() throws Exception {
    HashMap<String, Long> timeClaims = new HashMap<>();
    token = TokenUtils.generateTokenString("/TokenSubPath.json", null, timeClaims);
    publicKey = TokenUtils.readPublicKey("/publicKey.pem");
    if (publicKey == null) {
        throw new IllegalStateException("Failed to load /publicKey.pem resource");
    }
}
 
Example 19
Source File: DefaultJWTTokenParserTest.java    From smallrye-jwt with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() throws Exception {
    publicKey = (RSAPublicKey) TokenUtils.readPublicKey("/publicKey.pem");
    parser = new DefaultJWTTokenParser();
    config = new JWTAuthContextInfo(publicKey, "https://server.example.com");
}
 
Example 20
Source File: RolesAllowedSignEncryptTest.java    From microprofile-jwt-auth with Apache License 2.0 4 votes vote down vote up
private static String signEncryptClaimsWithOptionalCty(String jsonResName, boolean cty) throws Exception {
    PrivateKey signingKey = TokenUtils.readPrivateKey("/privateKey4k.pem");
    PublicKey encryptionKey = TokenUtils.readPublicKey("/publicKey.pem");
    return TokenUtils.signEncryptClaims(signingKey, null, encryptionKey, null, jsonResName, cty);
}