-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
45 changed files
with
2,530 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
jans-auth-server/client/src/main/java/io/jans/as/client/SsaClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
188 changes: 188 additions & 0 deletions
188
jans-auth-server/client/src/main/java/io/jans/as/client/SsaRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + '\'' + | ||
'}'; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
jans-auth-server/client/src/main/java/io/jans/as/client/SsaResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.