Skip to content

Commit

Permalink
fix(jans-auth-server): ssa get endpoint (#2719)
Browse files Browse the repository at this point in the history
  • Loading branch information
Milton-Ch committed Oct 26, 2022
1 parent e3cb908 commit 35ffbf0
Show file tree
Hide file tree
Showing 41 changed files with 1,669 additions and 538 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,6 @@ public String getRequestAsString() {
String accessToken = ((UserInfoRequest) request).getAccessToken();
sb.append("\n");
sb.append(Constants.AUTHORIZATION_BEARER).append(accessToken);
} else if (request.getAuthorizationMethod() == AuthorizationMethod.AUTHORIZATION_REQUEST_HEADER_FIELD && request instanceof SsaRequest) {
String accessToken = ((SsaRequest) request).getAccessToken();
sb.append("\n");
sb.append(Constants.AUTHORIZATION_BEARER).append(accessToken);
}

sb.append("\n");
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
* Copyright (c) 2020, Janssen Project
*/

package io.jans.as.client;
package io.jans.as.client.ssa.create;

import io.jans.as.client.BaseClient;
import io.jans.as.model.config.Constants;
import jakarta.ws.rs.HttpMethod;
import jakarta.ws.rs.client.Entity;
Expand All @@ -16,11 +17,11 @@

import java.util.List;

public class SsaClient extends BaseClient<SsaRequest, SsaResponse> {
public class SsaCreateClient extends BaseClient<SsaCreateRequest, SsaCreateResponse> {

private static final Logger LOG = Logger.getLogger(SsaClient.class);
private static final Logger LOG = Logger.getLogger(SsaCreateClient.class);

public SsaClient(String url) {
public SsaCreateClient(String url) {
super(url);
}

Expand All @@ -29,19 +30,21 @@ public String getHttpMethod() {
return HttpMethod.POST;
}

public SsaResponse execSsaCreate(String accessToken, Long orgId, Long expirationDate, String description, String softwareId, List<String> softwareRoles, List<String> grantTypes) {
setRequest(new SsaRequest());
getRequest().setAccessToken(accessToken);
getRequest().setOrgId(orgId);
getRequest().setExpiration(expirationDate);
getRequest().setDescription(description);
getRequest().setSoftwareId(softwareId);
getRequest().setSoftwareRoles(softwareRoles);
getRequest().setGrantTypes(grantTypes);
public SsaCreateResponse execSsaCreate(String accessToken, Long orgId, Long expirationDate, String description,
String softwareId, List<String> softwareRoles, List<String> grantTypes) {
SsaCreateRequest ssaCreateRequest = new SsaCreateRequest();
ssaCreateRequest.setAccessToken(accessToken);
ssaCreateRequest.setOrgId(orgId);
ssaCreateRequest.setExpiration(expirationDate);
ssaCreateRequest.setDescription(description);
ssaCreateRequest.setSoftwareId(softwareId);
ssaCreateRequest.setSoftwareRoles(softwareRoles);
ssaCreateRequest.setGrantTypes(grantTypes);
setRequest(ssaCreateRequest);
return exec();
}

public SsaResponse exec() {
public SsaCreateResponse exec() {
try {
initClient();

Expand All @@ -55,9 +58,9 @@ public SsaResponse exec() {

JSONObject requestBody = getRequest().getJSONParameters();
clientResponse = clientRequest.buildPost(Entity.json(requestBody.toString(4))).invoke();
final SsaResponse ssaResponse = new SsaResponse(clientResponse);
ssaResponse.injectDataFromJson();
setResponse(ssaResponse);
final SsaCreateResponse ssaCreateResponse = new SsaCreateResponse(clientResponse);
ssaCreateResponse.injectDataFromJson();
setResponse(ssaCreateResponse);

} catch (Exception e) {
LOG.error(e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
* Copyright (c) 2020, Janssen Project
*/

package io.jans.as.client;
package io.jans.as.client.ssa.create;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.jans.as.client.BaseRequest;
import io.jans.as.client.util.ClientUtil;
import io.jans.as.model.common.AuthorizationMethod;
import io.jans.as.model.json.JsonApplier;
Expand All @@ -19,12 +20,13 @@
import java.util.ArrayList;
import java.util.List;

import static io.jans.as.client.util.ClientUtil.*;
import static io.jans.as.client.util.ClientUtil.extractListByKey;
import static io.jans.as.client.util.ClientUtil.longOrNull;
import static io.jans.as.model.ssa.SsaRequestParam.*;

public class SsaRequest extends BaseRequest {
public class SsaCreateRequest extends BaseRequest {

private static final Logger log = Logger.getLogger(SsaRequest.class);
private static final Logger log = Logger.getLogger(SsaCreateRequest.class);

@JsonProperty(value = "org_id")
private Long orgId;
Expand All @@ -50,7 +52,7 @@ public class SsaRequest extends BaseRequest {

private String accessToken;

public SsaRequest() {
public SsaCreateRequest() {
setContentType(MediaType.APPLICATION_JSON);
setMediaType(MediaType.APPLICATION_JSON);
setAuthorizationMethod(AuthorizationMethod.AUTHORIZATION_REQUEST_HEADER_FIELD);
Expand Down Expand Up @@ -129,21 +131,21 @@ public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}

public static SsaRequest fromJson(String json) throws JSONException {
public static SsaCreateRequest fromJson(String json) throws JSONException {
return fromJson(new JSONObject(json));
}

public static SsaRequest fromJson(JSONObject requestObject) throws JSONException {
final SsaRequest result = new SsaRequest();
public static SsaCreateRequest fromJson(JSONObject requestObject) throws JSONException {
final SsaCreateRequest result = new SsaCreateRequest();
JsonApplier.getInstance().apply(requestObject, result);
result.setOrgId(requestObject.getLong(ORG_ID.toString()));
result.setExpiration(longOrNull(requestObject, EXPIRATION.toString()));
result.setDescription(requestObject.optString(DESCRIPTION.toString()));
result.setSoftwareId(requestObject.optString(SOFTWARE_ID.toString()));
result.setSoftwareRoles(extractListByKey(requestObject, SOFTWARE_ROLES.toString()));
result.setGrantTypes(extractListByKey(requestObject, GRANT_TYPES.toString()));
result.setOneTimeUse(booleanOrNull(requestObject, ONE_TIME_USE.toString()));
result.setRotateSsa(booleanOrNull(requestObject, ROTATE_SSA.toString()));
result.setOrgId(requestObject.getLong(ORG_ID.getName()));
result.setExpiration(longOrNull(requestObject, EXPIRATION.getName()));
result.setDescription(requestObject.optString(DESCRIPTION.getName()));
result.setSoftwareId(requestObject.optString(SOFTWARE_ID.getName()));
result.setSoftwareRoles(extractListByKey(requestObject, SOFTWARE_ROLES.getName()));
result.setGrantTypes(extractListByKey(requestObject, GRANT_TYPES.getName()));
result.setOneTimeUse(requestObject.optBoolean(ONE_TIME_USE.getName(), true));
result.setRotateSsa(requestObject.optBoolean(ROTATE_SSA.getName(), true));
return result;
}

Expand Down Expand Up @@ -182,7 +184,6 @@ public String toString() {
", grantTypes=" + grantTypes +
", oneTimeUse=" + oneTimeUse +
", rotateSsa=" + rotateSsa +
", accessToken='" + accessToken + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Janssen Project software is available under the Apache License (2004). See http://www.apache.org/licenses/ for full text.
*
* Copyright (c) 2020, Janssen Project
*/

package io.jans.as.client.ssa.create;

import io.jans.as.client.BaseResponseWithErrors;
import io.jans.as.model.jwt.Jwt;
import io.jans.as.model.jwt.JwtClaims;
import io.jans.as.model.ssa.SsaErrorResponseType;
import jakarta.ws.rs.core.Response;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Objects;

import static io.jans.as.model.ssa.SsaRequestParam.JTI;
import static io.jans.as.model.ssa.SsaRequestParam.SSA;

public class SsaCreateResponse extends BaseResponseWithErrors<SsaErrorResponseType> {

private static final Logger LOG = Logger.getLogger(SsaCreateResponse.class);

private String ssa;

private String jti;

public SsaCreateResponse() {
}

public SsaCreateResponse(Response clientResponse) {
super(clientResponse);
}

@Override
public SsaErrorResponseType fromString(String p_str) {
return SsaErrorResponseType.fromString(p_str);
}

public void injectDataFromJson() {
injectDataFromJson(entity);
}

@Override
public void injectDataFromJson(String json) {
if (StringUtils.isNotBlank(entity)) {
try {
JSONObject jsonObj = new JSONObject(entity);
if (jsonObj.has(SSA.getName())) {
ssa = jsonObj.getString(SSA.getName());
if (StringUtils.isNotBlank(ssa)) {
JwtClaims jwtClaims = Objects.requireNonNull(Jwt.parseSilently(ssa)).getClaims();
if (jwtClaims.hasClaim(JTI.getName())) {
jti = jwtClaims.getClaimAsString(JTI.getName());
}
}
}
} catch (JSONException e) {
LOG.error(e.getMessage(), e);
}
}
}

public String getSsa() {
return ssa;
}

public void setSsa(String ssa) {
this.ssa = ssa;
}

public String getJti() {
return jti;
}

public void setJti(String jti) {
this.jti = jti;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Janssen Project software is available under the Apache License (2004). See http://www.apache.org/licenses/ for full text.
*
* Copyright (c) 2020, Janssen Project
*/

package io.jans.as.client.ssa.get;

import io.jans.as.client.BaseClient;
import io.jans.as.model.config.Constants;
import jakarta.ws.rs.HttpMethod;
import jakarta.ws.rs.client.Invocation.Builder;
import org.apache.commons.lang.StringUtils;
import org.apache.http.client.utils.URIBuilder;
import org.apache.log4j.Logger;

public class SsaGetClient extends BaseClient<SsaGetRequest, SsaGetResponse> {

private static final Logger LOG = Logger.getLogger(SsaGetClient.class);

public SsaGetClient(String url) {
super(url);
}

@Override
public String getHttpMethod() {
return HttpMethod.GET;
}

public SsaGetResponse execSsaGet(String accessToken, String jti, Long orgId, Boolean softwareRoles) {
SsaGetRequest ssaGetRequest = new SsaGetRequest();
ssaGetRequest.setAccessToken(accessToken);
setRequest(ssaGetRequest);

URIBuilder uriBuilder = new URIBuilder();
if (StringUtils.isNotBlank(jti)) {
uriBuilder.addParameter("jti", jti);
}
if (orgId != null && orgId > 0) {
uriBuilder.addParameter("org_id", orgId.toString());
}
if (softwareRoles != null) {
uriBuilder.addParameter("software_roles", softwareRoles.toString());
}
setUrl(getUrl() + uriBuilder);
return exec();
}

public SsaGetResponse exec() {
try {
initClient();

Builder clientRequest = webTarget.request();
applyCookies(clientRequest);

clientRequest.header("Content-Type", request.getContentType());
if (StringUtils.isNotBlank(request.getAccessToken())) {
clientRequest.header(Constants.AUTHORIZATION, "Bearer ".concat(request.getAccessToken()));
}

clientResponse = clientRequest.buildGet().invoke();
final SsaGetResponse ssaGetResponse = new SsaGetResponse(clientResponse);
ssaGetResponse.injectDataFromJson();
setResponse(ssaGetResponse);

} catch (Exception e) {
LOG.error(e.getMessage(), e);
} finally {
closeConnection();
}

return getResponse();
}
}

Loading

0 comments on commit 35ffbf0

Please sign in to comment.