Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public JsonWebToken parse(final String jwt) {

final String kid = getAttribute(header, "kid", defaultKid);

final JsonObject payload = patcher.patch(defaultKid.equals(kid) ? null : kid, loadJson(jwt.substring(firstDot + 1, secondDot)));
final JsonObject payload = patcher.patch(kid.equals(defaultKid) ? null : kid, loadJson(jwt.substring(firstDot + 1, secondDot)));
dateValidator.checkInterval(payload);

final String alg = getAttribute(header, "alg", defaultAlg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class JwtPatcher {
@PostConstruct
private void init() {
readerFactory = Json.createReaderFactory(emptyMap());
defaultPatch = ofNullable(config.read("jwt.header.jwt.payload.patch.default", null))
defaultPatch = ofNullable(config.read("jwt.payload.patch.default", null))
.map(it -> {
try (final JsonReader reader = readerFactory.createReader(new StringReader(it))) {
return reader.readArray();
Expand Down Expand Up @@ -75,9 +75,7 @@ public JsonObject patch(final String kid, final JsonObject raw) {
}

protected /*can be overriden to be lazy*/ JsonPatch getPatch(final String kid) {
if (kid == null) {
return defaultPatch;
}
return kid == null ? defaultPatch : patches.get(kid);
JsonPatch jsonPatch = patches.get(kid);
return jsonPatch == null ? defaultPatch : jsonPatch;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.geronimo.microprofile.impl.jwtauth.tck.jaxrs;

import org.eclipse.microprofile.jwt.tck.container.jaxrs.TCKApplication;
import org.eclipse.microprofile.jwt.tck.util.TokenUtils;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.arquillian.testng.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.testng.annotations.Test;

import javax.json.Json;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Cookie;
import java.io.File;
import java.net.URL;

import static javax.ws.rs.core.MediaType.TEXT_PLAIN_TYPE;
import static org.testng.Assert.assertEquals;

// NOTE: reuses tck resources and token generation
public class JsonPatchTest extends Arquillian {
@Deployment(testable = false)
public static Archive<?> war() {
System.setProperty("geronimo.jwt-auth.jwt.payload.patch.default", "[ { \"op\": \"copy\", \"from\":\"/resource_access/service-C/groups\", \"path\": \"/groups\" } ]");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use microprofile properties as a webapp asset instead? Goal is to have each test able to run in the same suite. TCK have a bug about it but we shouldnt prevent ourself to do it IMHO.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, I try.
btw, you agree, that the defaultPatch is always used when no mapping is found (regardless off default kid value)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If kid is not in the mapping yes.
That said wonder if we shoulf support "none" value in the mapping to ease skipping that.
Wdyt?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any real benefit in a 'none' value currently.
btw, I couldn't manage to get microprofile-config.properties to work. Either the tck tests fail or the projects tests fail when I add/remove a mp-config impl.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we dont support none then we only use default if kid is not set, not in all other cases to respect mapping config.
Not configured in mapping means "none" in this case.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently, the patch.mapping has higher priority then patch.default. So, if no patch.mapping is found then always apply the default jsonpatch if defined (regardless of geronimo.jwt-auth.jwt.header.kid.default is set, or not). This seems fine by me.
What is your use case, to be sure I understand you correctly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one ;).
Point is that if you want default to be used if there is no mapping there is the need of a "noop" mapping (skipping an empty jsonpatch array for perf) so i proposed none.

return ShrinkWrap.create(WebArchive.class, JsonPatchTest.class.getSimpleName() + ".war")
.addClasses(TCKApplication.class, PassthroughEndpoint.class)
.addAsResource(JsonPatchTest.class.getResource("/publicKey.pem"), "/publicKey.pem");
}

@ArquillianResource
private URL base;

@Test
public void test() throws Exception {
final Client client = ClientBuilder.newClient();
try {
final String token = TokenUtils.generateTokenString("/Token2.json");
final String serverToken = client.target(base.toExternalForm())
.path("jsonpatch")
.request(TEXT_PLAIN_TYPE)
.cookie(new Cookie("Bearer", token))
.get(String.class);
assertEquals(serverToken, token);
} finally {
client.close();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.geronimo.microprofile.impl.jwtauth.tck.jaxrs;

import org.eclipse.microprofile.jwt.JsonWebToken;

import javax.annotation.security.RolesAllowed;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("jsonpatch")
@ApplicationScoped
public class RolesAllowedEndpoint {
@Inject
private JsonWebToken token;

@GET
@Produces(MediaType.TEXT_PLAIN)
@RolesAllowed("groupC")
public String passthrough() {
return token.getRawToken();
}
}