2021-05-28 18:29:06 +02:00
|
|
|
package org.gcube.common.keycloak;
|
|
|
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
|
|
|
import org.gcube.common.keycloak.model.ModelUtils;
|
2022-05-20 10:14:34 +02:00
|
|
|
import org.gcube.common.keycloak.model.TokenIntrospectionResponse;
|
2021-05-28 18:29:06 +02:00
|
|
|
import org.gcube.common.keycloak.model.TokenResponse;
|
|
|
|
import org.junit.After;
|
|
|
|
import org.junit.Assert;
|
|
|
|
import org.junit.Before;
|
2021-12-09 15:05:26 +01:00
|
|
|
import org.junit.FixMethodOrder;
|
2021-05-28 18:29:06 +02:00
|
|
|
import org.junit.Test;
|
2021-12-09 15:05:26 +01:00
|
|
|
import org.junit.runners.MethodSorters;
|
2021-05-28 18:29:06 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2021-12-09 15:05:26 +01:00
|
|
|
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
2021-05-28 18:29:06 +02:00
|
|
|
public class TestKeycloakClient {
|
|
|
|
|
|
|
|
protected static final Logger logger = LoggerFactory.getLogger(TestKeycloakClient.class);
|
|
|
|
|
2022-06-10 13:48:48 +02:00
|
|
|
protected static final String DEV_BASE_URL = "https://accounts.dev.d4science.org/auth/realms/d4science";
|
|
|
|
protected static final String DEV_TOKEN_ENDPOINT = DEV_BASE_URL + "/protocol/openid-connect/token";
|
2022-06-08 19:05:40 +02:00
|
|
|
protected static final String DEV_INTROSPECTION_ENDPOINT = DEV_TOKEN_ENDPOINT + "/introspect";
|
2022-05-20 10:14:34 +02:00
|
|
|
|
2022-06-08 19:05:40 +02:00
|
|
|
protected static final String CLIENT_ID = "keycloak-client-unit-test";
|
|
|
|
protected static final String CLIENT_SECRET = "ebf6f82e-9511-408e-8321-203081e472d8";
|
|
|
|
protected static final String TEST_AUDIENCE = "conductor-server";
|
|
|
|
protected static final String OLD_OIDC_ACCESS_TOKEN = "eyJhbGciOiJSU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Khy2cmUm1KF2k0zlzP8MxV9wTxNrpovMr-PnbtEPZ_IlVQIzHwjHfwQ";
|
2021-05-28 18:29:06 +02:00
|
|
|
|
2022-06-08 19:05:40 +02:00
|
|
|
protected static TokenResponse oidcTR = null;
|
|
|
|
protected static TokenResponse umaTR = null;
|
2021-12-09 15:05:26 +01:00
|
|
|
|
2021-05-28 18:29:06 +02:00
|
|
|
@Before
|
|
|
|
public void setUp() throws Exception {
|
|
|
|
}
|
|
|
|
|
|
|
|
@After
|
|
|
|
public void tearDown() throws Exception {
|
|
|
|
}
|
|
|
|
|
2022-06-10 13:48:48 +02:00
|
|
|
@Test
|
|
|
|
public void test00TokenEndpointConstruction() throws Exception {
|
|
|
|
logger.info("*** [0.0] Start testing Keycloak token endpoint construction from base URL...");
|
|
|
|
URL url = KeycloakClientFactory.newInstance().getTokenEndpointURL(new URL(DEV_BASE_URL));
|
|
|
|
Assert.assertNotNull(url);
|
|
|
|
Assert.assertTrue(url.getProtocol().equals("https"));
|
|
|
|
Assert.assertEquals(new URL(DEV_TOKEN_ENDPOINT), url);
|
|
|
|
logger.info("Constructed URL is: {}", url);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void test00ATokenEndpointConstructionWithTrailingSlash() throws Exception {
|
|
|
|
logger.info("*** [0.0] Start testing Keycloak token endpoint construction from base URL with trailing slash...");
|
|
|
|
URL url = KeycloakClientFactory.newInstance().getTokenEndpointURL(new URL(DEV_BASE_URL + "/"));
|
|
|
|
Assert.assertNotNull(url);
|
|
|
|
Assert.assertTrue(url.getProtocol().equals("https"));
|
|
|
|
Assert.assertEquals(new URL(DEV_TOKEN_ENDPOINT), url);
|
|
|
|
logger.info("Constructed URL is: {}", url);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void test01IntrospectionEndpointConstruction() throws Exception {
|
|
|
|
logger.info("*** [0.0] Start testing Keycloak introspection endpoint construction from base URL...");
|
|
|
|
URL url = KeycloakClientFactory.newInstance().getIntrospectionEndpointURL(new URL(DEV_BASE_URL));
|
|
|
|
Assert.assertNotNull(url);
|
|
|
|
Assert.assertTrue(url.getProtocol().equals("https"));
|
|
|
|
Assert.assertEquals(new URL(DEV_INTROSPECTION_ENDPOINT), url);
|
|
|
|
logger.info("Constructed URL is: {}", url);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void test01AIntrospectionEndpointConstructionWithTrailingSlash() throws Exception {
|
|
|
|
logger.info("*** [0.0] Start testing Keycloak introspection endpoint construction from base URL with trailing slash...");
|
|
|
|
URL url = KeycloakClientFactory.newInstance().getIntrospectionEndpointURL(new URL(DEV_BASE_URL + "/"));
|
|
|
|
Assert.assertNotNull(url);
|
|
|
|
Assert.assertTrue(url.getProtocol().equals("https"));
|
|
|
|
Assert.assertEquals(new URL(DEV_INTROSPECTION_ENDPOINT), url);
|
|
|
|
logger.info("Constructed URL is: {}", url);
|
|
|
|
}
|
|
|
|
|
2021-05-28 18:29:06 +02:00
|
|
|
@Test
|
2022-06-08 19:05:40 +02:00
|
|
|
public void test02IntrospectEndpointCompute() throws Exception {
|
2022-05-20 10:14:34 +02:00
|
|
|
logger.info("*** [0.2] Start testing Keycloak userinfo endpoint computed from provided URL...");
|
|
|
|
URL url = KeycloakClientFactory.newInstance().computeIntrospectionEndpointURL(new URL(DEV_TOKEN_ENDPOINT));
|
|
|
|
Assert.assertNotNull(url);
|
|
|
|
Assert.assertTrue(url.getProtocol().equals("https"));
|
|
|
|
Assert.assertEquals(new URL(DEV_INTROSPECTION_ENDPOINT), url);
|
2022-06-10 13:48:48 +02:00
|
|
|
logger.info("Computed URL is: {}", url);
|
2021-05-28 18:29:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2022-03-30 12:02:23 +02:00
|
|
|
public void test12QueryOIDCToken() throws Exception {
|
|
|
|
logger.info("*** [1.2] Start testing query OIDC token from Keycloak with URL...");
|
2022-05-20 10:14:34 +02:00
|
|
|
oidcTR = KeycloakClientFactory.newInstance().queryOIDCToken(new URL(DEV_TOKEN_ENDPOINT), CLIENT_ID,
|
|
|
|
CLIENT_SECRET);
|
|
|
|
|
|
|
|
logger.info("*** [1.2] OIDC access token: {}", oidcTR.getAccessToken());
|
|
|
|
logger.info("*** [1.2] OIDC refresh token: {}", oidcTR.getRefreshToken());
|
2022-03-30 12:02:23 +02:00
|
|
|
TestModels.checkTokenResponse(oidcTR);
|
2022-06-08 19:05:40 +02:00
|
|
|
TestModels.checkAccessToken(ModelUtils.getAccessTokenFrom(oidcTR), "service-account-" + CLIENT_ID, false);
|
2022-03-30 12:02:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void test24QueryUMAToken() throws Exception {
|
|
|
|
logger.info("*** [2.4] Start testing query UMA token from Keycloak with URL...");
|
2022-05-20 10:14:34 +02:00
|
|
|
umaTR = KeycloakClientFactory.newInstance().queryUMAToken(new URL(DEV_TOKEN_ENDPOINT), CLIENT_ID, CLIENT_SECRET,
|
2021-12-09 15:05:26 +01:00
|
|
|
TEST_AUDIENCE, null);
|
2021-05-28 18:29:06 +02:00
|
|
|
|
2022-05-20 10:14:34 +02:00
|
|
|
logger.info("*** [2.4] UMA access token: {}", umaTR.getAccessToken());
|
|
|
|
logger.info("*** [2.4] UMA refresh token: {}", umaTR.getRefreshToken());
|
|
|
|
|
2022-03-30 12:02:23 +02:00
|
|
|
TestModels.checkTokenResponse(umaTR);
|
2022-06-08 19:05:40 +02:00
|
|
|
TestModels.checkAccessToken(ModelUtils.getAccessTokenFrom(umaTR), "service-account-" + CLIENT_ID, true);
|
2022-05-20 10:14:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void test302IntrospectOIDCAccessToken() throws Exception {
|
|
|
|
logger.info("*** [3.2] Start testing introspect OIDC access token...");
|
|
|
|
TokenIntrospectionResponse tir = KeycloakClientFactory.newInstance().introspectAccessToken(
|
|
|
|
new URL(DEV_INTROSPECTION_ENDPOINT), CLIENT_ID, CLIENT_SECRET, oidcTR.getAccessToken());
|
|
|
|
|
|
|
|
TestModels.checkTokenIntrospectionResponse(tir);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void test304IntrospectUMAAccessToken() throws Exception {
|
|
|
|
logger.info("*** [3.4] Start testing introspect UMA access token...");
|
|
|
|
TokenIntrospectionResponse tir = KeycloakClientFactory.newInstance().introspectAccessToken(
|
|
|
|
new URL(DEV_INTROSPECTION_ENDPOINT), CLIENT_ID, CLIENT_SECRET, umaTR.getAccessToken());
|
|
|
|
|
|
|
|
TestModels.checkTokenIntrospectionResponse(tir);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void test306OIDCAccessTokenVerification() throws Exception {
|
|
|
|
logger.info("*** [3.6] Start OIDC access token verification...");
|
|
|
|
Assert.assertTrue(KeycloakClientFactory.newInstance().isAccessTokenVerified(
|
|
|
|
new URL(DEV_INTROSPECTION_ENDPOINT), CLIENT_ID, CLIENT_SECRET, oidcTR.getAccessToken()));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void test307OIDCAccessTokenNonVerification() throws Exception {
|
|
|
|
logger.info("*** [3.7] Start OIDC access token NON verification...");
|
|
|
|
Assert.assertFalse(KeycloakClientFactory.newInstance().isAccessTokenVerified(
|
|
|
|
new URL(DEV_INTROSPECTION_ENDPOINT), CLIENT_ID, CLIENT_SECRET, OLD_OIDC_ACCESS_TOKEN));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void test309UMAAccessTokenVerification() throws Exception {
|
|
|
|
logger.info("*** [3.9] Start UMA access token verification...");
|
|
|
|
Assert.assertTrue(KeycloakClientFactory.newInstance().isAccessTokenVerified(
|
|
|
|
new URL(DEV_INTROSPECTION_ENDPOINT), CLIENT_ID, CLIENT_SECRET, umaTR.getAccessToken()));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void test310UMAAccessTokenNonVerification() throws Exception {
|
|
|
|
logger.info("*** [3.10] Start UMA access token NON verification...");
|
|
|
|
Assert.assertFalse(KeycloakClientFactory.newInstance().isAccessTokenVerified(
|
|
|
|
new URL(DEV_INTROSPECTION_ENDPOINT), CLIENT_ID, CLIENT_SECRET, OLD_UMA_ACCESS_TOKEN));
|
|
|
|
}
|
2021-05-28 18:29:06 +02:00
|
|
|
}
|