Skip to content

Commit

Permalink
Merge pull request #87 from exAphex/patch
Browse files Browse the repository at this point in the history
Patch
  • Loading branch information
exAphex authored Sep 16, 2021
2 parents 68af73d + 649b9b4 commit 9132cfb
Show file tree
Hide file tree
Showing 18 changed files with 677 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
import com.asena.scimgateway.model.Attribute;
import com.asena.scimgateway.model.ConnectionProperty;
import com.asena.scimgateway.model.EntryTypeMapping;
import com.asena.scimgateway.model.ModificationStep;
import com.asena.scimgateway.model.RemoteSystem;
import com.asena.scimgateway.model.Script;
import com.asena.scimgateway.model.ConnectionProperty.ConnectionPropertyType;
import com.asena.scimgateway.utils.ConnectorUtil;
import com.asena.scimgateway.utils.JSONUtil;
import com.asena.scimgateway.utils.ModificationUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
Expand All @@ -25,6 +27,8 @@ public class AzureConnector implements IConnector {
private String oauthURL;
private String oauthUser;
private String oauthPassword;
private String userExpandProperties;
private String groupExpandProperties;

@Override
public RemoteSystem getRemoteSystemTemplate() {
Expand All @@ -40,6 +44,8 @@ public RemoteSystem getRemoteSystemTemplate() {
"OAuth user name", true, ConnectionPropertyType.STRING));
retSystem.addProperty(new ConnectionProperty("oauth.password", "adminpassword", "Oauth user password", false,
ConnectionPropertyType.STRING));
retSystem.addProperty(new ConnectionProperty("azure.user.expand", "memberOf", "Attributes for Graph API expand (user)", false, ConnectionPropertyType.STRING));
retSystem.addProperty(new ConnectionProperty("azure.group.expand", "memberOf", "Attributes for Graph API expand (group)", false, ConnectionPropertyType.STRING));
retSystem.setType("Microsoft Azure Active Directory");

retSystem.addAttribute(new Attribute("displayName", "displayName", "Displayname"));
Expand Down Expand Up @@ -112,6 +118,12 @@ public void setupConnector(RemoteSystem rs) {
case "oauth.password":
this.oauthPassword = cp.getValue();
break;
case "azure.user.expand":
this.userExpandProperties = cp.getValue();
break;
case "azure.group.expand":
this.groupExpandProperties = cp.getValue();
break;
}
}
}
Expand All @@ -134,12 +146,12 @@ public String createEntity(String entity, HashMap<String, Object> data) throws E
}

@Override
public String updateEntity(String entity, HashMap<String, Object> data) throws Exception {
public String updateEntity(String entity, ModificationStep ms) throws Exception {
switch (entity) {
case "Users":
return updateEntityInAzure(entity, data);
return updateEntityInAzure(entity, ms);
case "Groups":
return updateEntityInAzure(entity, data);
return updateEntityInAzure(entity, ms);
default:
throw new InternalErrorException("No entity passed to Azure connector!");
}
Expand Down Expand Up @@ -198,7 +210,9 @@ private HashMap<String, Object> getEntityFromAzure(String entity, HashMap<String
hc.setUserName(this.oauthUser);
hc.setPassword(this.oauthPassword);

String result = hc.get(this.baseURL + "/v1.0/" + entity + "/" + userId);
String url= getURL(entity, userId, true);

String result = hc.get(url);
ObjectMapper mapper = new ObjectMapper();

HashMap<String, Object> map = new HashMap<>();
Expand All @@ -217,7 +231,7 @@ private List<HashMap<String, Object>> getEntitiesFromAzure(String entity) throws
hc.setUserName(this.oauthUser);
hc.setPassword(this.oauthPassword);

String result = hc.get(this.baseURL + "/v1.0/" + entity);
String result = hc.get(getURL(entity, null, true));
ObjectMapper mapper = new ObjectMapper();

HashMap<String, Object> map = new HashMap<>();
Expand All @@ -239,7 +253,7 @@ private String createEntityInAzure(String entity, HashMap<String, Object> data)

DocumentContext jsonContext = JsonPath.parse(data);

String retUser = hc.post(this.baseURL + "/v1.0/" + entity, jsonContext.jsonString());
String retUser = hc.post(getURL(entity, null, false), jsonContext.jsonString());
ObjectMapper mapper = new ObjectMapper();

HashMap<String, Object> map = new HashMap<>();
Expand All @@ -264,16 +278,17 @@ private boolean deleteEntityInAzure(String entity, HashMap<String, Object> data)
hc.setPassword(this.oauthPassword);
hc.setExpectedResponseCode(204);

hc.delete(this.baseURL + "/v1.0/" + entity + "/" + userId);
hc.delete(getURL(entity, userId, false));
return true;
}

private String updateEntityInAzure(String entity, HashMap<String, Object> data) throws Exception {
String userId = (String) ConnectorUtil.getAttributeValue(getNameId(), data);
private String updateEntityInAzure(String entity, ModificationStep ms) throws Exception {
String userId = (String) ms.findValueByAttribute(getNameId());
HashMap<String, Object> data = ModificationUtil.collectSimpleModifications(ms);
if (userId == null) {
throw new InternalErrorException("UserID not found in read mapping!");
}

OAuthInterceptor oi = new OAuthInterceptor(this.oauthUser, this.oauthPassword, this.oauthURL);
oi.addBody("resource", this.baseURL);

Expand All @@ -285,9 +300,30 @@ private String updateEntityInAzure(String entity, HashMap<String, Object> data)

DocumentContext jsonContext = JsonPath.parse(data);

hc.patch(this.baseURL + "/v1.0/" + entity + "/" + userId, jsonContext.jsonString());
hc.patch(getURL(entity, userId, false), jsonContext.jsonString());

return userId;
}


private String getURL(String entity, String userId, boolean includeParams) {
String retURL = this.baseURL + "/v1.0/" + entity;
if (userId != null) {
retURL += "/" + userId;
}
if (includeParams) {
switch (entity) {
case "Users":
if ((this.userExpandProperties != null) && (!this.userExpandProperties.isEmpty())) {
retURL += "?$expand=" + this.userExpandProperties;
}
break;
case "Groups":
if ((this.groupExpandProperties != null) && (!this.groupExpandProperties.isEmpty())) {
retURL += "?$expand=" + this.groupExpandProperties;
}
break;
}
}
return retURL;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

import java.util.HashMap;
import java.util.List;

import com.asena.scimgateway.model.ModificationStep;
import com.asena.scimgateway.model.RemoteSystem;

public interface IConnector {
public RemoteSystem getRemoteSystemTemplate();
public void setupConnector(RemoteSystem rs);
public String getNameId();
public String createEntity(String entity, HashMap<String, Object> data) throws Exception;
public String updateEntity(String entity, HashMap<String, Object> data) throws Exception;
public String updateEntity(String entity, ModificationStep ms) throws Exception;
public boolean deleteEntity(String entity, HashMap<String, Object> data) throws Exception;
public List<HashMap<String,Object>> getEntities(String entity) throws Exception;
public HashMap<String, Object> getEntity(String entity, HashMap<String, Object> data) throws Exception;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.asena.scimgateway.model.Attribute;
import com.asena.scimgateway.model.ConnectionProperty;
import com.asena.scimgateway.model.EntryTypeMapping;
import com.asena.scimgateway.model.ModificationStep;
import com.asena.scimgateway.model.RemoteSystem;
import com.asena.scimgateway.model.ConnectionProperty.ConnectionPropertyType;
import com.asena.scimgateway.utils.ConnectorUtil;
Expand Down Expand Up @@ -198,24 +199,24 @@ private void upsertAttributeArray(LdapConnection conn, String dn, String attr, L
conn.modify(dn, modAttr);
}

private String updateEntity(HashMap<String, Object> data) {
private String updateEntity(ModificationStep ms) {
LdapConnection connection = null;
String retStr = null;
try {
connection = ldapConnect();
retStr = (String)ConnectorUtil.getAttributeValue(nameId, data);
for (String key : data.keySet()) {
if (!key.equals(nameId)) {
Object objData = data.get(key);
retStr = (String) ms.findValueByAttribute(nameId);
for (com.asena.scimgateway.model.Modification m : ms.getModifications()) {
if (!m.getAttributeName().equals(nameId)) {
Object objData = m.getValue();
if (objData instanceof NativeArray) {
NativeArray tempArr = (NativeArray) objData;
List<String> lstValues = new ArrayList<String>();
for (int i = 0; i < tempArr.size(); i++) {
lstValues.add((String) tempArr.get(i));
}
upsertAttributeArray(connection, retStr, key, lstValues);
upsertAttributeArray(connection, retStr, m.getAttributeName(), lstValues);
} else {
upsertAttribute(connection, retStr, key, (String)data.get(key));
upsertAttribute(connection, retStr, m.getAttributeName(), (String)objData);
}
}
}
Expand All @@ -229,8 +230,8 @@ private String updateEntity(HashMap<String, Object> data) {
}

@Override
public String updateEntity(String entity, HashMap<String, Object> data) throws Exception {
return updateEntity(data);
public String updateEntity(String entity, ModificationStep ms) throws Exception {
return updateEntity(ms);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import java.util.HashMap;
import java.util.List;

import com.asena.scimgateway.exception.InternalErrorException;
import com.asena.scimgateway.model.Attribute;
import com.asena.scimgateway.model.ConnectionProperty;
import com.asena.scimgateway.model.ModificationStep;
import com.asena.scimgateway.model.RemoteSystem;
import com.asena.scimgateway.model.ConnectionProperty.ConnectionPropertyType;

Expand All @@ -32,8 +34,10 @@ public String createEntity(String entity, HashMap<String, Object> data) throws E
}

@Override
public String updateEntity(String entity, HashMap<String, Object> data) throws Exception {
return (String) data.get(this.nameId);
public String updateEntity(String entity, ModificationStep ms) throws Exception {
// throw new InternalErrorException("NOT SUPPORTED!");

return ms.getId();
}

@Override
Expand All @@ -43,16 +47,16 @@ public boolean deleteEntity(String entity, HashMap<String, Object> data) {

@Override
public List<HashMap<String, Object>> getEntities(String entity) throws Exception {
List<HashMap<String,Object>> ret = new ArrayList<>();
HashMap<String,Object> retObj = new HashMap<>();
List<HashMap<String, Object>> ret = new ArrayList<>();
HashMap<String, Object> retObj = new HashMap<>();
retObj.put("noop", "test");
ret.add(retObj);
return ret;
}

@Override
public HashMap<String, Object> getEntity(String entity, HashMap<String, Object> data) throws Exception {
HashMap<String,Object> retObj = new HashMap<>();
HashMap<String, Object> retObj = new HashMap<>();
retObj.put("noop", "test");
return retObj;
}
Expand All @@ -61,5 +65,5 @@ public HashMap<String, Object> getEntity(String entity, HashMap<String, Object>
public String getNameId() {
return nameId;
}

}
Loading

0 comments on commit 9132cfb

Please sign in to comment.