Skip to content

Commit

Permalink
feat: ssa creation endpoint (#2495)
Browse files Browse the repository at this point in the history
  • Loading branch information
Milton-Ch authored Sep 28, 2022
1 parent 31fb464 commit 61c83e3
Show file tree
Hide file tree
Showing 45 changed files with 2,530 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.jans.as.model.common.AuthorizationMethod;
import io.jans.as.model.util.Util;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.json.JSONException;
import org.json.JSONObject;

Expand All @@ -18,6 +19,8 @@
import java.util.HashMap;
import java.util.Map;

import static io.jans.as.model.ciba.PushTokenDeliveryRequestParam.AUTHORIZATION_REQUEST_ID;

/**
* @author Javier Rojas Blum
* @version April 25. 2022
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ public static void parse(String json, OpenIdConfigurationResponse response) {
if (jsonObj.has(BACKCHANNEL_USER_CODE_PAREMETER_SUPPORTED)) {
response.setBackchannelUserCodeParameterSupported(jsonObj.getBoolean(BACKCHANNEL_USER_CODE_PAREMETER_SUPPORTED));
}

// SSA
if (jsonObj.has(SSA_ENDPOINT)) {
response.setSsaEndpoint(jsonObj.optString(SSA_ENDPOINT));
}
}

public static OpenIdConfigurationResponse parse(String json) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ public class OpenIdConfigurationResponse extends BaseResponse implements Seriali
private List<String> backchannelAuthenticationRequestSigningAlgValuesSupported;
private Boolean backchannelUserCodeParameterSupported;

// SSA
private String ssaEndpoint;

public OpenIdConfigurationResponse() {
}

Expand Down Expand Up @@ -1186,6 +1189,14 @@ public void setMltsAliases(Map<String, Serializable> mltsAliases) {
this.mltsAliases = mltsAliases;
}

public String getSsaEndpoint() {
return ssaEndpoint;
}

public void setSsaEndpoint(String ssaEndpoint) {
this.ssaEndpoint = ssaEndpoint;
}

@Override
public String toString() {
return "OpenIdConfigurationResponse{" +
Expand Down Expand Up @@ -1246,6 +1257,7 @@ public String toString() {
", backchannelAuthenticationRequestSigningAlgValuesSupported=" + backchannelAuthenticationRequestSigningAlgValuesSupported + '\'' +
", backchannelUserCodeParameterSupported=" + backchannelUserCodeParameterSupported + '\'' +
", mltsAliases=" + mltsAliases + '\'' +
", ssaEndpoint=" + ssaEndpoint + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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;

import io.jans.as.model.config.Constants;
import jakarta.ws.rs.HttpMethod;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.client.Invocation.Builder;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.json.JSONObject;

import java.util.List;

public class SsaClient extends BaseClient<SsaRequest, SsaResponse> {

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

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

@Override
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);
return exec();
}

public SsaResponse 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()));
}

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

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

return getResponse();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
/*
* 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;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.jans.as.client.util.ClientUtil;
import io.jans.as.model.common.AuthorizationMethod;
import io.jans.as.model.json.JsonApplier;
import jakarta.ws.rs.core.MediaType;
import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

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

public class SsaRequest extends BaseRequest {

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

@JsonProperty(value = "org_id")
private Long orgId;

private Long expiration;

private String description;

@JsonProperty(value = "software_id")
private String softwareId;

@JsonProperty(value = "software_roles")
private List<String> softwareRoles;

@JsonProperty(value = "grant_types")
private List<String> grantTypes;

@JsonProperty(value = "one_time_use")
private Boolean oneTimeUse;

@JsonProperty(value = "rotate_ssa")
private Boolean rotateSsa;

private String accessToken;

public SsaRequest() {
setContentType(MediaType.APPLICATION_JSON);
setMediaType(MediaType.APPLICATION_JSON);
setAuthorizationMethod(AuthorizationMethod.AUTHORIZATION_REQUEST_HEADER_FIELD);
this.softwareRoles = new ArrayList<>();
}

public Long getOrgId() {
return orgId;
}

public void setOrgId(Long orgId) {
this.orgId = orgId;
}

public Long getExpiration() {
return expiration;
}

public void setExpiration(Long expiration) {
this.expiration = expiration;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getSoftwareId() {
return softwareId;
}

public void setSoftwareId(String softwareId) {
this.softwareId = softwareId;
}

public List<String> getSoftwareRoles() {
return softwareRoles;
}

public void setSoftwareRoles(List<String> softwareRoles) {
this.softwareRoles = softwareRoles;
}

public List<String> getGrantTypes() {
return grantTypes;
}

public void setGrantTypes(List<String> grantTypes) {
this.grantTypes = grantTypes;
}

public Boolean getOneTimeUse() {
return oneTimeUse;
}

public void setOneTimeUse(Boolean oneTimeUse) {
this.oneTimeUse = oneTimeUse;
}

public Boolean getRotateSsa() {
return rotateSsa;
}

public void setRotateSsa(Boolean rotateSsa) {
this.rotateSsa = rotateSsa;
}

public String getAccessToken() {
return accessToken;
}

public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}

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

public static SsaRequest fromJson(JSONObject requestObject) throws JSONException {
final SsaRequest result = new SsaRequest();
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()));
return result;
}

@Override
public String getQueryString() {
try {
return ClientUtil.toPrettyJson(getJSONParameters()).replace("\\/", "/");
} catch (JSONException | JsonProcessingException e) {
log.error(e.getMessage(), e);
return null;
}
}

@Override
public JSONObject getJSONParameters() throws JSONException {
JSONObject parameters = new JSONObject();
parameters.put(ORG_ID.getName(), orgId);
parameters.put(EXPIRATION.getName(), expiration);
parameters.put(DESCRIPTION.getName(), description);
parameters.put(SOFTWARE_ID.getName(), softwareId);
parameters.put(SOFTWARE_ROLES.getName(), softwareRoles);
parameters.put(GRANT_TYPES.getName(), grantTypes);
parameters.put(ONE_TIME_USE.getName(), oneTimeUse);
parameters.put(ROTATE_SSA.getName(), rotateSsa);
return parameters;
}

@Override
public String toString() {
return "SsaRequest{" +
"orgId=" + orgId +
", expiration=" + expiration +
", description='" + description + '\'' +
", softwareId='" + softwareId + '\'' +
", softwareRoles=" + softwareRoles +
", grantTypes=" + grantTypes +
", oneTimeUse=" + oneTimeUse +
", rotateSsa=" + rotateSsa +
", accessToken='" + accessToken + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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;

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;

public class SsaResponse extends BaseResponseWithErrors<SsaErrorResponseType> {

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

private String ssa;

public SsaResponse() {
}

public SsaResponse(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")) {
setSsa(jsonObj.getString("ssa"));
}
} catch (JSONException e) {
LOG.error(e.getMessage(), e);
}
}
}

public String getSsa() {
return ssa;
}

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

0 comments on commit 61c83e3

Please sign in to comment.