Skip to content

Commit

Permalink
Add RolesAllowedSignEncryptRsaOaepTest and move negative RSA-OAEP-256…
Browse files Browse the repository at this point in the history
… test to it from RolesAllowedSignEncryptTest
  • Loading branch information
sberyozkin committed Sep 21, 2022
1 parent 09ba972 commit 8e5f54e
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 22 deletions.
Expand Up @@ -50,7 +50,8 @@
import jakarta.ws.rs.core.Response;

/**
* Tests of the MP-JWT auth method authorization behavior as expected by the MP-JWT RBAC 1.0 spec
* Test that a decryption of an inner signed JWT token encrypted using RSA-OAEP-256 algorithm succeeds with
* `RSA-OAEP-256` but fails with `RSA-OAEP` if `mp.jwt.decrypt.key.algorithm=RSA-OAEP-256` is configured.
*/
public class RolesAllowedSignEncryptRsaOaep256Test extends Arquillian {

Expand Down
@@ -0,0 +1,147 @@
/*
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* Licensed 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.eclipse.microprofile.jwt.tck.container.jaxrs.jwe;

import static jakarta.ws.rs.core.MediaType.TEXT_PLAIN;
import static org.eclipse.microprofile.jwt.tck.TCKConstants.TEST_GROUP_JAXRS;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.PrivateKey;
import java.security.PublicKey;

import org.eclipse.microprofile.jwt.tck.container.jaxrs.RolesEndpoint;
import org.eclipse.microprofile.jwt.tck.container.jaxrs.TCKApplication;
import org.eclipse.microprofile.jwt.tck.util.KeyManagementAlgorithm;
import org.eclipse.microprofile.jwt.tck.util.MpJwtTestVersion;
import org.eclipse.microprofile.jwt.tck.util.TokenUtils;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.arquillian.testng.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.Response;

/**
* Test that decryption of an inner signed JWT token encrypted using RSA-OAEP algorithm succeeds with `RSA-OAEP` but
* fails with `RSA-OAEP-256` if `mp.jwt.decrypt.key.algorithm=RSA-OAEP` is configured.
*/
public class RolesAllowedSignEncryptRsaOaepTest extends Arquillian {

/**
* The test generated JWT token string
*/
private static String token;

/**
* The base URL for the container under test
*/
@ArquillianResource
private URL baseURL;

/**
* Create a CDI aware base web application archive
*
* @return the base base web application archive
* @throws IOException
* - on resource failure
*/
@Deployment(testable = true)
public static WebArchive createDeployment() throws IOException {
URL config = RolesAllowedSignEncryptRsaOaepTest.class
.getResource("/META-INF/microprofile-config-verify-decrypt-rsa-oaep.properties");
URL verifyKey = RolesAllowedSignEncryptRsaOaepTest.class.getResource("/publicKey4k.pem");
URL decryptKey = RolesAllowedSignEncryptRsaOaepTest.class.getResource("/privateKey.pem");
WebArchive webArchive = ShrinkWrap
.create(WebArchive.class, "RolesAllowedSignEncryptRsaOaepTest.war")
.addAsManifestResource(new StringAsset(MpJwtTestVersion.MPJWT_V_2_1.name()),
MpJwtTestVersion.MANIFEST_NAME)
.addAsResource(decryptKey, "/privateKey.pem")
.addAsResource(verifyKey, "/publicKey4k.pem")
.addClass(RolesEndpoint.class)
.addClass(TCKApplication.class)
.addAsWebInfResource("beans.xml", "beans.xml")
.addAsManifestResource(config, "microprofile-config.properties");
return webArchive;
}

@BeforeClass(alwaysRun = true)
public static void generateToken() throws Exception {
token = signEncryptClaims("/Token1.json");
}

private static String signEncryptClaims(String jsonResName) throws Exception {
return signEncryptClaimsWithOptionalCty(jsonResName, true);
}

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);
}


@RunAsClient
@Test(groups = TEST_GROUP_JAXRS, description = "Validate a request with RSA-OAEP encrypted token succeeds")
public void callEchoRsaOaep() {
Reporter.log("callEcho with RSA-OAEP encrypted token, expect HTTP_OK");

String uri = baseURL.toExternalForm() + "endp/echo";
WebTarget echoEndpointTarget = ClientBuilder.newClient()
.target(uri)
.queryParam("input", "hello");
Response response =
echoEndpointTarget.request(TEXT_PLAIN).header(HttpHeaders.AUTHORIZATION, "Bearer " + token).get();
Assert.assertEquals(response.getStatus(), HttpURLConnection.HTTP_OK);
String reply = response.readEntity(String.class);
// Must return hello, user={token upn claim}
Assert.assertEquals(reply, "hello, user=jdoe@example.com");
}

@RunAsClient
@Test(groups = TEST_GROUP_JAXRS, description = "Validate a request with RSA-OAEP-256 encrypted token fails with HTTP_UNAUTHORIZED")
public void callEchoRsaOaep256() throws Exception {
Reporter.log("callEcho with RSA-OAEP-356 encrypted token, expect HTTP_UNAUTHORIZED");

PrivateKey signingKey = TokenUtils.readPrivateKey("/privateKey4k.pem");
PublicKey encryptionKey = TokenUtils.readPublicKey("/publicKey.pem");
String token =
TokenUtils.signEncryptClaims(signingKey, null, encryptionKey, KeyManagementAlgorithm.RSA_OAEP_256, null,
"/Token1.json", true);
String uri = baseURL.toExternalForm() + "endp/echo";
WebTarget echoEndpointTarget = ClientBuilder.newClient()
.target(uri)
.queryParam("input", "hello");
Response response =
echoEndpointTarget.request(TEXT_PLAIN).header(HttpHeaders.AUTHORIZATION, "Bearer " + token).get();
Assert.assertEquals(response.getStatus(), HttpURLConnection.HTTP_UNAUTHORIZED);
}
}
Expand Up @@ -34,7 +34,6 @@
import org.eclipse.microprofile.jwt.tck.TCKConstants;
import org.eclipse.microprofile.jwt.tck.container.jaxrs.RolesEndpoint;
import org.eclipse.microprofile.jwt.tck.container.jaxrs.TCKApplication;
import org.eclipse.microprofile.jwt.tck.util.KeyManagementAlgorithm;
import org.eclipse.microprofile.jwt.tck.util.MpJwtTestVersion;
import org.eclipse.microprofile.jwt.tck.util.TokenUtils;
import org.jboss.arquillian.container.test.api.Deployment;
Expand All @@ -55,7 +54,8 @@
import jakarta.ws.rs.core.Response;

/**
* Tests of the MP-JWT auth method authorization behavior as expected by the MP-JWT RBAC 1.0 spec
* Test that decryption of an inner signed JWT token encrypted using RSA-OAEP algorithm succeeds without having to
* configure `mp.jwt.decrypt.key.algorithm=RSA-OAEP`.
*/
public class RolesAllowedSignEncryptTest extends Arquillian {

Expand Down Expand Up @@ -156,25 +156,6 @@ public void callEchoRsaOaep() {
Assert.assertEquals(reply, "hello, user=jdoe@example.com");
}

@RunAsClient
@Test(groups = TEST_GROUP_JAXRS, description = "Validate a request with RSA-OAEP-256 encrypted token fails with HTTP_UNAUTHORIZED")
public void callEchoRsaOaep256() throws Exception {
Reporter.log("callEcho with RSA-OAEP-356 encrypted token, expect HTTP_UNAUTHORIZED");

PrivateKey signingKey = TokenUtils.readPrivateKey("/privateKey4k.pem");
PublicKey encryptionKey = TokenUtils.readPublicKey("/publicKey.pem");
String token =
TokenUtils.signEncryptClaims(signingKey, null, encryptionKey, KeyManagementAlgorithm.RSA_OAEP_256, null,
"/Token1.json", true);
String uri = baseURL.toExternalForm() + "endp/echo";
WebTarget echoEndpointTarget = ClientBuilder.newClient()
.target(uri)
.queryParam("input", "hello");
Response response =
echoEndpointTarget.request(TEXT_PLAIN).header(HttpHeaders.AUTHORIZATION, "Bearer " + token).get();
Assert.assertEquals(response.getStatus(), HttpURLConnection.HTTP_UNAUTHORIZED);
}

@RunAsClient
@Test(groups = TEST_GROUP_JAXRS, description = "Validate a request with MP-JWT fail with HTTP_UNAUTHORIZED if no 'cty' header is set")
public void callEchoWithoutCty() throws Exception {
Expand Down
@@ -0,0 +1,25 @@
#
# Copyright (c) 2020 Contributors to the Eclipse Foundation
#
# See the NOTICE file(s) distributed with this work for additional
# information regarding copyright ownership.
#
# Licensed 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.
#

# A reference to the decryption privateKey.pem location
mp.jwt.decrypt.key.location=/privateKey.pem
mp.jwt.decrypt.key.algorithm=RSA-OAEP
# A reference to the verification publicKey.pem location
mp.jwt.verify.publickey.location=/publicKey4k.pem
mp.jwt.verify.issuer=https://server.example.com
1 change: 1 addition & 0 deletions tck/src/test/resources/suites/tck-base-suite.xml
Expand Up @@ -78,6 +78,7 @@
<class name="org.eclipse.microprofile.jwt.tck.config.TokenAsCookieTest" />
<class name="org.eclipse.microprofile.jwt.tck.container.jaxrs.EmptyTokenTest" />
<class name="org.eclipse.microprofile.jwt.tck.container.jaxrs.jwe.RolesAllowedSignEncryptTest" />
<class name="org.eclipse.microprofile.jwt.tck.container.jaxrs.jwe.RolesAllowedSignEncryptRsaOaepTest" />
<class name="org.eclipse.microprofile.jwt.tck.container.jaxrs.jwe.RolesAllowedSignEncryptRsaOaep256Test" />
<class name="org.eclipse.microprofile.jwt.tck.config.jwe.PrivateKeyAsPEMClasspathTest" />
<class name="org.eclipse.microprofile.jwt.tck.config.jwe.PrivateKeyAsJWKClasspathTest" />
Expand Down

0 comments on commit 8e5f54e

Please sign in to comment.