Now audience can be provided in both encoded and not encoded form (starts with "/" check is performed). Ugly test added.

This commit is contained in:
Mauro Mugnaini 2021-05-06 17:51:46 +02:00
parent 17e503c786
commit c5a9dca54c
2 changed files with 33 additions and 15 deletions

View File

@ -166,7 +166,7 @@ public class OpenIdConnectRESTHelper {
/**
* Queries from the OIDC server an UMA token, by using provided clientId and client secret for the given audience
* (context) in URLEncoded form and optionally a list of permissions.
* (context), in URLEncoded form or not, and optionally a list of permissions.
*
* @param tokenUrl the token endpoint {@link URL} of the OIDC server
* @param clientId the client id
@ -179,13 +179,14 @@ public class OpenIdConnectRESTHelper {
public static JWTToken queryUMAToken(URL tokenUrl, String clientId, String clientSecret, String audience,
List<String> permissions) throws OpenIdConnectRESTHelperException {
return queryUMAToken(tokenUrl, "Basic " + Base64.getEncoder().encodeToString((clientId + ":" + clientSecret).getBytes()),
return queryUMAToken(tokenUrl,
"Basic " + Base64.getEncoder().encodeToString((clientId + ":" + clientSecret).getBytes()),
audience, permissions);
}
/**
* Queries from the OIDC server an UMA token, by using provided access token, for the given audience (context)
* in URLEncoded form and optionally a list of permissions.
* Queries from the OIDC server an UMA token, by using provided access token, for the given audience (context),
* in URLEncoded form or not, and optionally a list of permissions.
*
* @param tokenUrl the token endpoint {@link URL} of the OIDC server
* @param authorization the auth token (the access token URLEncoded by the "Bearer " string)
@ -199,6 +200,14 @@ public class OpenIdConnectRESTHelper {
Map<String, List<String>> params = new HashMap<>();
params.put("grant_type", Arrays.asList("urn:ietf:params:oauth:grant-type:uma-ticket"));
if (audience.startsWith("/")) {
try {
logger.trace("Audience was provided in non URL encoded form, encoding it");
audience = URLEncoder.encode(audience, "UTF-8");
} catch (UnsupportedEncodingException e) {
logger.error("Cannot URL encode 'audience'", e);
}
}
try {
params.put("audience", Arrays.asList(URLEncoder.encode(audience, "UTF-8")));
} catch (UnsupportedEncodingException e) {

View File

@ -8,34 +8,43 @@ public class RestHelperTest {
public RestHelperTest() {
}
/**
* To be re-enabled when the token is took programmatically
*/
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oMy5nb29nbGV1c2VyY29udGVudC5jb20vYS0vQU9oMTRHaUFxUzZRc00xaDNXV1BqMU15VUdlRFA2VnFaQlhtOTZ4cHhZLXNndyIsImVtYWlsIjoidGhlbWF4eDc2QGdtYWlsLmNvbSJ9.WMddlUQujlpmzW07Lrk50vOyWpiT1Tp_RsBWRbzyrQnu5EQQSCq1uGOuSf7Z3VZFv8fbnzWekMZRNzhngEddzOQHgAlsgRdqNI_-ucjmb_8SfR2I5PkYJLTG0jF-Urqi-GvfJtLr2B8dBDnMDO6FLFsg1e5qb-5HkV60eEtY2Wult1PGxlkD05w-K2w513IOMkVIl25ZxKbP61-Iu1qfV_q3QFvUHl_pdqL7uKC5bkl1lqTVeuCwrXrKubHnKc-UzpHtHp8XY0Iao7LdLtON7SODYhU8EkZ860ZlFTSCszmLUpSH4t_shSk9Fiqd8wBKAet5ngmyAPzKx9TT2FK65g";
URL avatarURL = new URL("https://accounts.dev.d4science.org/auth/realms/d4science/avatar-provider/");
byte[] avatarBytes = OpenIdConnectRESTHelper.getUserAvatar(avatarURL, accessTokenBearer);
// assertNotNull(avatarBytes);
// assertNotNull(avatarBytes);
}
// @Test
// @Test
public void getUMATokenWithBasicAuth()
throws MalformedURLException, OpenIdConnectRESTHelperException, InterruptedException {
URL tokenURL = new URL(
"https://accounts.dev.d4science.org/auth/realms/d4science/protocol/openid-connect/token");
JWTToken token = OpenIdConnectRESTHelper.queryUMAToken(tokenURL, "robcomp",
"0fec31cb-23c3-44e2-9359-d6db6784b7d3",
"/gcube/devNext/NextNext", null);
System.out.println(token.getAccessTokenString());
}
// @Test
public void getExp() throws MalformedURLException, OpenIdConnectRESTHelperException, InterruptedException {
URL tokenURL = new URL("https://accounts.dev.d4science.org/auth/realms/d4science/protocol/openid-connect/token");
JWTToken token = OpenIdConnectRESTHelper.queryClientToken("lr62_portal", "28726d01-9f24-4ef4-a057-3d208d96aaa0",
URL tokenURL = new URL(
"https://accounts.dev.d4science.org/auth/realms/d4science/protocol/openid-connect/token");
JWTToken token = OpenIdConnectRESTHelper.queryClientToken("robcomp", "0fec31cb-23c3-44e2-9359-d6db6784b7d3",
tokenURL);
System.out.println(token.getExpAsDate());
System.out.println(token.getAzp());
Thread.sleep((token.getExp() * 1000 - System.currentTimeMillis() + 5000));
// Thread.sleep((token.getExp() * 1000 - System.currentTimeMillis() + 5000));
System.out.println(token.isExpired());
}
public static void main(String[] args) throws Exception {
RestHelperTest rht = new RestHelperTest();
// rht.getAvatar();
// rht.getAvatar();
try {
rht.getExp();
rht.getUMATokenWithBasicAuth();
} catch (OpenIdConnectRESTHelperException e) {
if (e.hasJSONPayload()) {
System.out.println("JSON response: " + e.getResponseString());