Search in sources :

Example 1 with SignatureValidator

use of org.apache.geronimo.microprofile.impl.jwtauth.jwt.SignatureValidator in project meecrowave by apache.

the class OAuth2Test method validateJwt.

private void validateJwt(final ClientAccessToken token) {
    final JwtParser parser = new JwtParser();
    final KidMapper kidMapper = new KidMapper();
    final DateValidator dateValidator = new DateValidator();
    final SignatureValidator signatureValidator = new SignatureValidator();
    final GeronimoJwtAuthConfig config = (value, def) -> {
        switch(value) {
            case "issuer.default":
                return "myissuer";
            case "jwt.header.kid.default":
                return "defaultkid";
            case "public-key.default":
                return Base64.getEncoder().encodeToString(PUBLIC_KEY.getEncoded());
            default:
                return def;
        }
    };
    setField(kidMapper, "config", config);
    setField(dateValidator, "config", config);
    setField(parser, "config", config);
    setField(signatureValidator, "config", config);
    setField(parser, "kidMapper", kidMapper);
    setField(parser, "dateValidator", dateValidator);
    setField(parser, "signatureValidator", signatureValidator);
    Stream.of(dateValidator, signatureValidator, kidMapper, parser).forEach(this::init);
    final JsonWebToken jsonWebToken = parser.parse(token.getTokenKey());
    assertNotNull(jsonWebToken);
    assertEquals("myissuer", jsonWebToken.getIssuer());
    assertEquals("test", JsonString.class.cast(jsonWebToken.getClaim("username")).getString());
}
Also used : JwtParser(org.apache.geronimo.microprofile.impl.jwtauth.jwt.JwtParser) KidMapper(org.apache.geronimo.microprofile.impl.jwtauth.jwt.KidMapper) DateValidator(org.apache.geronimo.microprofile.impl.jwtauth.jwt.DateValidator) URISyntaxException(java.net.URISyntaxException) BiFunction(java.util.function.BiFunction) ClientAccessToken(org.apache.cxf.rs.security.oauth2.common.ClientAccessToken) Collections.singletonList(java.util.Collections.singletonList) Cache(javax.cache.Cache) JCacheCodeDataProvider(org.apache.meecrowave.oauth2.provider.JCacheCodeDataProvider) MutableConfiguration(javax.cache.configuration.MutableConfiguration) Assert.fail(org.junit.Assert.fail) ClassRule(org.junit.ClassRule) Method(java.lang.reflect.Method) JsonObject(javax.json.JsonObject) JsonbBuilder(javax.json.bind.JsonbBuilder) APPLICATION_FORM_URLENCODED_TYPE(javax.ws.rs.core.MediaType.APPLICATION_FORM_URLENCODED_TYPE) MeecrowaveRule(org.apache.meecrowave.junit.MeecrowaveRule) OAuthJSONProvider(org.apache.cxf.rs.security.oauth2.provider.OAuthJSONProvider) CachingProvider(javax.cache.spi.CachingProvider) StandardCharsets(java.nio.charset.StandardCharsets) DateValidator(org.apache.geronimo.microprofile.impl.jwtauth.jwt.DateValidator) Base64(java.util.Base64) Stream(java.util.stream.Stream) Response(javax.ws.rs.core.Response) GeronimoJwtAuthConfig(org.apache.geronimo.microprofile.impl.jwtauth.config.GeronimoJwtAuthConfig) CacheManager(javax.cache.CacheManager) Meecrowave(org.apache.meecrowave.Meecrowave) JwtParser(org.apache.geronimo.microprofile.impl.jwtauth.jwt.JwtParser) BeforeClass(org.junit.BeforeClass) Form(javax.ws.rs.core.Form) Client(javax.ws.rs.client.Client) Entity.entity(javax.ws.rs.client.Entity.entity) ClientBuilder(javax.ws.rs.client.ClientBuilder) OAuthAuthorizationData(org.apache.cxf.rs.security.oauth2.common.OAuthAuthorizationData) SignatureValidator(org.apache.geronimo.microprofile.impl.jwtauth.jwt.SignatureValidator) Message(org.apache.cxf.message.Message) Assert.assertNotNull(org.junit.Assert.assertNotNull) Caching(javax.cache.Caching) KidMapper(org.apache.geronimo.microprofile.impl.jwtauth.jwt.KidMapper) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) PublicKey(java.security.PublicKey) ClassLoaderUtils(org.apache.cxf.common.classloader.ClassLoaderUtils) Field(java.lang.reflect.Field) File(java.io.File) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) JsonString(javax.json.JsonString) JsonWebToken(org.eclipse.microprofile.jwt.JsonWebToken) OAuthConstants(org.apache.cxf.rs.security.oauth2.utils.OAuthConstants) APPLICATION_JSON_TYPE(javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE) Jsonb(javax.json.bind.Jsonb) WebTarget(javax.ws.rs.client.WebTarget) Assert.assertEquals(org.junit.Assert.assertEquals) SignatureValidator(org.apache.geronimo.microprofile.impl.jwtauth.jwt.SignatureValidator) GeronimoJwtAuthConfig(org.apache.geronimo.microprofile.impl.jwtauth.config.GeronimoJwtAuthConfig) JsonWebToken(org.eclipse.microprofile.jwt.JsonWebToken)

Aggregations

File (java.io.File)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 URISyntaxException (java.net.URISyntaxException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 PublicKey (java.security.PublicKey)1 Base64 (java.util.Base64)1 Collections.singletonList (java.util.Collections.singletonList)1 BiFunction (java.util.function.BiFunction)1 Stream (java.util.stream.Stream)1 Cache (javax.cache.Cache)1 CacheManager (javax.cache.CacheManager)1 Caching (javax.cache.Caching)1 MutableConfiguration (javax.cache.configuration.MutableConfiguration)1 CachingProvider (javax.cache.spi.CachingProvider)1 JsonObject (javax.json.JsonObject)1 JsonString (javax.json.JsonString)1 Jsonb (javax.json.bind.Jsonb)1 JsonbBuilder (javax.json.bind.JsonbBuilder)1 Client (javax.ws.rs.client.Client)1