-
Notifications
You must be signed in to change notification settings - Fork 693
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TOMEE-4284 - Implement tomee.mp.jwt.allow.no-exp property over mp.jwt.tomee.allow.no-exp #990
Closed
tichovz
wants to merge
6
commits into
apache:main
from
tichovz:mp-jwt-tomee.mp.jwt.allow.no-exp-property
Closed
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
20e6d5a
Implement tomee.mp.jwt.allow.no-exp property over mp.jwt.tomee.allow.…
tichovz b361c5d
Merge branch 'apache:main' into mp-jwt-tomee.mp.jwt.allow.no-exp-prop…
tichovz cb3a231
Merge branch 'apache:main' into mp-jwt-tomee.mp.jwt.allow.no-exp-prop…
tichovz 1be9dc7
Merge branch 'apache:main' into mp-jwt-tomee.mp.jwt.allow.no-exp-prop…
tichovz 36ecd39
Merge branch 'apache:main' into mp-jwt-tomee.mp.jwt.allow.no-exp-prop…
tichovz 681df83
Merge branch 'apache:main' into mp-jwt-tomee.mp.jwt.allow.no-exp-prop…
tichovz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
253 changes: 253 additions & 0 deletions
253
...-itests/src/test/java/org/apache/tomee/microprofile/jwt/itest/AllowNoExpPropertyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,253 @@ | ||
/* | ||
* 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.tomee.microprofile.jwt.itest; | ||
|
||
import jakarta.annotation.security.RolesAllowed; | ||
import jakarta.enterprise.context.RequestScoped; | ||
import jakarta.ws.rs.ApplicationPath; | ||
import jakarta.ws.rs.Consumes; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.core.Application; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.Response; | ||
import java.io.File; | ||
import java.net.URL; | ||
import java.util.ArrayList; | ||
import java.util.Base64; | ||
import static java.util.Collections.singletonList; | ||
import java.util.Optional; | ||
import org.apache.cxf.feature.LoggingFeature; | ||
import org.apache.cxf.jaxrs.client.WebClient; | ||
import org.apache.johnzon.jaxrs.JohnzonProvider; | ||
import org.apache.tomee.server.composer.Archive; | ||
import org.apache.tomee.server.composer.TomEE; | ||
import org.eclipse.microprofile.auth.LoginConfig; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
import org.junit.Test; | ||
|
||
|
||
public class AllowNoExpPropertyTest { | ||
|
||
@Test | ||
public void testNewPropertyOverridesOld1() throws Exception { | ||
final Tokens tokens = Tokens.rsa(2048, 256); | ||
final File appJar = Archive.archive() | ||
.add(AllowNoExpPropertyTest.class) | ||
.add(ColorService.class) | ||
.add(Api.class) | ||
.add("META-INF/microprofile-config.properties", "#\n" + | ||
"mp.jwt.verify.publickey=" + Base64.getEncoder().encodeToString(tokens.getPublicKey().getEncoded()) | ||
+ "\n" + "mp.jwt.tomee.allow.no-exp=false" | ||
+ "\n" + "tomee.mp.jwt.allow.no-exp=true") | ||
.asJar(); | ||
|
||
final ArrayList<String> output = new ArrayList<>(); | ||
final TomEE tomee = TomEE.microprofile() | ||
.add("webapps/test/WEB-INF/beans.xml", "") | ||
.add("webapps/test/WEB-INF/lib/app.jar", appJar) | ||
.watch("org.apache.tomee.microprofile.jwt.", "\n", output::add) | ||
.build(); | ||
|
||
final WebClient webClient = createWebClient(tomee.toURI().resolve("/test").toURL()); | ||
|
||
final String claims = "{" + | ||
" \"sub\":\"Jane Awesome\"" + | ||
"}"; | ||
|
||
{// invalid token | ||
final String token = tokens.asToken(claims); | ||
final Response response = webClient.reset() | ||
.path("/movies") | ||
.header("Content-Type", "application/json") | ||
.header("Authorization", "Bearer " + token) | ||
.get(); | ||
assertEquals(403, response.getStatus()); | ||
} | ||
|
||
assertPresent(output , "mp.jwt.tomee.allow.no-exp property is deprecated"); | ||
assertNotPresent(output, "rejected due to invalid claims"); | ||
assertNotPresent(output, "No Expiration Time (exp) claim present."); | ||
assertNotPresent(output, "\tat org."); // no stack traces | ||
} | ||
|
||
@Test | ||
public void testNewPropertyOverridesOld2() throws Exception { | ||
final Tokens tokens = Tokens.rsa(2048, 256); | ||
final File appJar = Archive.archive() | ||
.add(AllowNoExpPropertyTest.class) | ||
.add(ColorService.class) | ||
.add(Api.class) | ||
.add("META-INF/microprofile-config.properties", "#\n" + | ||
"mp.jwt.verify.publickey=" + Base64.getEncoder().encodeToString(tokens.getPublicKey().getEncoded()) | ||
+ "\n" + "mp.jwt.tomee.allow.no-exp=true" | ||
+ "\n" + "tomee.mp.jwt.allow.no-exp=false") | ||
.asJar(); | ||
|
||
final ArrayList<String> output = new ArrayList<>(); | ||
final TomEE tomee = TomEE.microprofile() | ||
.add("webapps/test/WEB-INF/beans.xml", "") | ||
.add("webapps/test/WEB-INF/lib/app.jar", appJar) | ||
.watch("org.apache.tomee.microprofile.jwt.", "\n", output::add) | ||
.build(); | ||
|
||
final WebClient webClient = createWebClient(tomee.toURI().resolve("/test").toURL()); | ||
|
||
final String claims = "{" + | ||
" \"sub\":\"Jane Awesome\"" + | ||
"}"; | ||
|
||
{// invalid token | ||
final String token = tokens.asToken(claims); | ||
final Response response = webClient.reset() | ||
.path("/movies") | ||
.header("Content-Type", "application/json") | ||
.header("Authorization", "Bearer " + token) | ||
.get(); | ||
assertEquals(401, response.getStatus()); | ||
} | ||
|
||
assertPresent(output , "mp.jwt.tomee.allow.no-exp property is deprecated"); | ||
assertPresent(output, "rejected due to invalid claims"); | ||
assertPresent(output, "No Expiration Time (exp) claim present."); | ||
assertNotPresent(output, "\tat org."); // no stack traces | ||
} | ||
|
||
@Test | ||
public void testNewProperty() throws Exception { | ||
final Tokens tokens = Tokens.rsa(2048, 256); | ||
final File appJar = Archive.archive() | ||
.add(AllowNoExpPropertyTest.class) | ||
.add(ColorService.class) | ||
.add(Api.class) | ||
.add("META-INF/microprofile-config.properties", "#\n" + | ||
"mp.jwt.verify.publickey=" + Base64.getEncoder().encodeToString(tokens.getPublicKey().getEncoded()) | ||
+ "\n" + "tomee.mp.jwt.allow.no-exp=true") | ||
.asJar(); | ||
|
||
final ArrayList<String> output = new ArrayList<>(); | ||
final TomEE tomee = TomEE.microprofile() | ||
.add("webapps/test/WEB-INF/beans.xml", "") | ||
.add("webapps/test/WEB-INF/lib/app.jar", appJar) | ||
.watch("org.apache.tomee.microprofile.jwt.", "\n", output::add) | ||
.build(); | ||
|
||
final WebClient webClient = createWebClient(tomee.toURI().resolve("/test").toURL()); | ||
|
||
final String claims = "{" + | ||
" \"sub\":\"Jane Awesome\"" + | ||
"}"; | ||
|
||
{// invalid token | ||
final String token = tokens.asToken(claims); | ||
final Response response = webClient.reset() | ||
.path("/movies") | ||
.header("Content-Type", "application/json") | ||
.header("Authorization", "Bearer " + token) | ||
.get(); | ||
assertEquals(403, response.getStatus()); | ||
} | ||
|
||
assertNotPresent(output , "mp.jwt.tomee.allow.no-exp property is deprecated"); | ||
assertNotPresent(output, "rejected due to invalid claims"); | ||
assertNotPresent(output, "No Expiration Time (exp) claim present."); | ||
assertNotPresent(output, "\tat org."); // no stack traces | ||
} | ||
|
||
@Test | ||
public void testOldProperty() throws Exception { | ||
final Tokens tokens = Tokens.rsa(2048, 256); | ||
final File appJar = Archive.archive() | ||
.add(AllowNoExpPropertyTest.class) | ||
.add(ColorService.class) | ||
.add(Api.class) | ||
.add("META-INF/microprofile-config.properties", "#\n" + | ||
"mp.jwt.verify.publickey=" + Base64.getEncoder().encodeToString(tokens.getPublicKey().getEncoded()) | ||
+ "\n" + "mp.jwt.tomee.allow.no-exp=true") | ||
.asJar(); | ||
|
||
final ArrayList<String> output = new ArrayList<>(); | ||
final TomEE tomee = TomEE.microprofile() | ||
.add("webapps/test/WEB-INF/beans.xml", "") | ||
.add("webapps/test/WEB-INF/lib/app.jar", appJar) | ||
.watch("org.apache.tomee.microprofile.jwt.", "\n", output::add) | ||
.build(); | ||
|
||
final WebClient webClient = createWebClient(tomee.toURI().resolve("/test").toURL()); | ||
|
||
final String claims = "{" + | ||
" \"sub\":\"Jane Awesome\"" + | ||
"}"; | ||
|
||
{// invalid token | ||
final String token = tokens.asToken(claims); | ||
final Response response = webClient.reset() | ||
.path("/movies") | ||
.header("Content-Type", "application/json") | ||
.header("Authorization", "Bearer " + token) | ||
.get(); | ||
assertEquals(403, response.getStatus()); | ||
} | ||
|
||
assertPresent(output , "mp.jwt.tomee.allow.no-exp property is deprecated"); | ||
assertNotPresent(output, "rejected due to invalid claims"); | ||
assertNotPresent(output, "No Expiration Time (exp) claim present."); | ||
assertNotPresent(output, "\tat org."); // no stack traces | ||
} | ||
|
||
public void assertPresent(final ArrayList<String> output, final String s) { | ||
final Optional<String> actual = output.stream() | ||
.filter(line -> line.contains(s)) | ||
.findFirst(); | ||
|
||
assertTrue(actual.isPresent()); | ||
} | ||
public void assertNotPresent(final ArrayList<String> output, final String s) { | ||
final Optional<String> actual = output.stream() | ||
.filter(line -> line.contains(s)) | ||
.findFirst(); | ||
|
||
assertTrue(!actual.isPresent()); | ||
} | ||
|
||
private static WebClient createWebClient(final URL base) { | ||
return WebClient.create(base.toExternalForm(), singletonList(new JohnzonProvider<>()), | ||
singletonList(new LoggingFeature()), null); | ||
} | ||
|
||
@ApplicationPath("/api") | ||
@LoginConfig(authMethod = "MP-JWT") | ||
public class Api extends Application { | ||
} | ||
|
||
@Path("/movies") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
@RequestScoped | ||
public static class ColorService { | ||
|
||
@GET | ||
@RolesAllowed({"manager", "user"}) | ||
public String getAllMovies() { | ||
return "Green"; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to avoid to read both entries all the time (
Config
can be slow depending theConfigSource
) and to avoid theAtomicBoolean
which is not needed?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the improvement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tichovz Can you include the feedback provided by Romain? :)