Skip to content

Commit

Permalink
Jaspersoft#292 enable token authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
jnguyen committed Aug 16, 2017
1 parent 9eb3d11 commit 0bae731
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@
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;
Expand Down Expand Up @@ -113,5 +116,49 @@ protected void login(SessionStorage storage) throws JSClientWebException {
}
}

public Session getTokenSession (String username, String role, String organization, String expTime, Map<String, String> pAttributes) {
if (username != null && username.length() > 0 && role != null && role.length() > 0 && organization != null && organization.length() > 0) {
AuthenticationCredentials credentials = new AuthenticationCredentials(username);
SessionStorage sessionStorage = new SessionStorage(configuration,
credentials,
Locale.getDefault(),
TimeZone.getDefault());
getToken(sessionStorage, role, organization, expTime, pAttributes);
return new Session(sessionStorage);
}

return null;
}

protected void getToken(SessionStorage sessionStorage, String role, String organization, String expTime, Map<String, String> pAttributes) {
AuthenticationCredentials credentials = sessionStorage.getCredentials();

WebTarget rootTarget = sessionStorage.getRootTarget();
StringBuilder pAttributesHeader = new StringBuilder();
if (pAttributes != null && !pAttributes.isEmpty()) {
for (Map.Entry<String, String> pAttribute : pAttributes.entrySet()) {
String value = pAttribute.getValue();
String key = "|" + pAttribute.getKey() + "=";
pAttributesHeader.append(key).append(value);
}
}

String exp = expTime.isEmpty() ? "" : ("|exp=" + expTime);

WebTarget queryParam = rootTarget.queryParam("pp", "u=" + credentials.getUsername() + "|r=" + role
+ "|o=" + organization + "|exp=" + exp + pAttributesHeader.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<String, NewCookie> 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");
}
}
}

0 comments on commit 0bae731

Please sign in to comment.