From c3c444d81fea172bea13f286e61a2207e8e2d044 Mon Sep 17 00:00:00 2001 From: jnguyen Date: Mon, 14 Aug 2017 15:50:46 -0500 Subject: [PATCH] #292 enable token authentication --- .../core/AuthenticationCredentials.java | 4 ++ .../client/core/JasperserverRestClient.java | 50 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/core/AuthenticationCredentials.java b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/core/AuthenticationCredentials.java index cd372161..64ccd147 100644 --- a/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/core/AuthenticationCredentials.java +++ b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/core/AuthenticationCredentials.java @@ -30,6 +30,10 @@ public AuthenticationCredentials(String username, String password) { this.password = password; } + public AuthenticationCredentials(String username) { + this.username = username; + } + public String getUsername() { return username; } diff --git a/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/core/JasperserverRestClient.java b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/core/JasperserverRestClient.java index 63f932d1..10b4552b 100644 --- a/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/core/JasperserverRestClient.java +++ b/src/main/java/com/jaspersoft/jasperserver/jaxrs/client/core/JasperserverRestClient.java @@ -22,16 +22,20 @@ import com.jaspersoft.jasperserver.jaxrs.client.core.enums.AuthenticationType; import com.jaspersoft.jasperserver.jaxrs.client.core.exceptions.AuthenticationFailedException; +import com.jaspersoft.jasperserver.jaxrs.client.core.exceptions.BadRequestException; import com.jaspersoft.jasperserver.jaxrs.client.core.exceptions.JSClientWebException; import com.jaspersoft.jasperserver.jaxrs.client.core.exceptions.ResourceNotFoundException; import com.jaspersoft.jasperserver.jaxrs.client.filters.BasicAuthenticationFilter; import com.jaspersoft.jasperserver.jaxrs.client.filters.SessionOutputFilter; import java.util.Locale; +import java.util.Map; import java.util.TimeZone; import javax.ws.rs.client.Entity; +import javax.ws.rs.client.Invocation; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.Form; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.NewCookie; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; import org.glassfish.jersey.client.ClientProperties; @@ -113,5 +117,51 @@ protected void login(SessionStorage storage) throws JSClientWebException { } } + public Session getTokenSession (Map tokenHeaders) { + SessionStorage sessionStorage = new SessionStorage(configuration, + null, + Locale.getDefault(), + TimeZone.getDefault()); + getToken(sessionStorage, tokenHeaders); + return new Session(sessionStorage); + } + + protected void getToken(SessionStorage sessionStorage, Map tokenHeaders) { + AuthenticationCredentials credentials = sessionStorage.getCredentials(); + + WebTarget rootTarget = sessionStorage.getRootTarget(); + if (!tokenHeaders.containsKey("principalParameter")) { + throw new BadRequestException("prinipalParameter header not found in the request"); + } + String pp = tokenHeaders.get("principalParameter"); + tokenHeaders.remove("principalParameter"); + StringBuilder headerValues = new StringBuilder(); + for (Map.Entry tokenHeader : tokenHeaders.entrySet()) { + String key; + if (headerValues.length() == 0) { + key = tokenHeader.getKey() + "="; + } else { + key = "|" + tokenHeader.getKey() + "="; + } + + String value = tokenHeader.getValue(); + headerValues.append(key).append(value); + } + + WebTarget queryParam = rootTarget.queryParam(pp, headerValues.toString()); + Invocation.Builder acceptTarget = queryParam.request().accept(MediaType.APPLICATION_FORM_URLENCODED_TYPE); + Response response = acceptTarget.get(Response.class); + + if (response.getStatus() == Status.OK.getStatusCode()) { + Map cookies = response.getCookies(); + NewCookie sessionCookie = cookies.get("JSESSIONID"); + + String sessionId = sessionCookie.getValue(); + sessionStorage.setSessionId(sessionId); + rootTarget.register(new SessionOutputFilter(sessionId)); + } else { + throw new ResourceNotFoundException("Server was not found"); + } + } }