use of com.tremolosecurity.unison.freeipa.json.IPABatchResponse in project OpenUnison by TremoloSecurity.
the class UserPrincipal method executeIPABatchCall.
private IPABatchResponse executeIPABatchCall(IPACall ipaCall, HttpCon con) throws IPAException, ClientProtocolException, IOException {
Gson gson = new Gson();
String json = gson.toJson(ipaCall);
if (logger.isDebugEnabled()) {
logger.debug("Outbound JSON : '" + json + "'");
}
HttpClient http = con.getHttp();
StringEntity str = new StringEntity(json, ContentType.APPLICATION_JSON);
HttpPost httppost = new HttpPost(this.url + "/ipa/session/json");
httppost.addHeader("Referer", this.url + "/ipa/ui/");
httppost.setEntity(str);
HttpResponse resp = http.execute(httppost);
BufferedReader in = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
StringBuffer b = new StringBuffer();
String line = null;
while ((line = in.readLine()) != null) {
b.append(line);
}
if (logger.isDebugEnabled()) {
logger.debug("Inbound JSON : " + b.toString());
}
EntityUtils.consumeQuietly(resp.getEntity());
httppost.completed();
IPABatchResponse ipaResponse = gson.fromJson(b.toString(), IPABatchResponse.class);
if (ipaResponse.getError() != null) {
IPAException ipaException = new IPAException(ipaResponse.getError().getMessage());
ipaException.setCode(ipaResponse.getError().getCode());
ipaException.setName(ipaResponse.getError().getName());
throw ipaException;
} else {
return ipaResponse;
}
}
use of com.tremolosecurity.unison.freeipa.json.IPABatchResponse in project OpenUnison by TremoloSecurity.
the class UserPrincipal method findUser.
private User findUser(String userID, Set<String> attributes, HttpCon con, Map<String, Object> request) throws IPAException, ClientProtocolException, IOException {
UserPrincipal principal = new UserPrincipal(userID, multiDomain, primaryDomain);
if (principal.isPrimaryDomain()) {
IPACall userSearch = new IPACall();
userSearch.setId(0);
userSearch.setMethod("user_show");
ArrayList<String> userArray = new ArrayList<String>();
userArray.add(principal.getUid());
userSearch.getParams().add(userArray);
HashMap<String, String> additionalParams = new HashMap<String, String>();
additionalParams.put("all", "true");
additionalParams.put("rights", "true");
userSearch.getParams().add(additionalParams);
IPAResponse resp = this.executeIPACall(userSearch, con);
User user = new User();
user.setUserID(userID);
Map<String, Object> results = (Map<String, Object>) resp.getResult().getResult();
for (String attributeName : attributes) {
if (attributeName.equalsIgnoreCase("uid")) {
Attribute a = user.getAttribs().get(attributeName);
if (a == null) {
a = new Attribute(attributeName);
user.getAttribs().put(attributeName, a);
}
StringBuilder s = new StringBuilder().append((String) ((List) results.get(attributeName)).get(0));
if (this.multiDomain) {
s.append('@').append(principal.getDomain());
}
a.getValues().add(s.toString());
} else {
if (results.get(attributeName) != null) {
if (results.get(attributeName) instanceof List) {
Attribute a = user.getAttribs().get(attributeName);
if (a == null) {
a = new Attribute(attributeName);
user.getAttribs().put(attributeName, a);
}
List l = (List) results.get(attributeName);
for (Object o : l) {
a.getValues().add((String) o);
}
} else {
Attribute a = user.getAttribs().get(attributeName);
if (a == null) {
a = new Attribute(attributeName);
user.getAttribs().put(attributeName, a);
}
a.getValues().add((String) results.get(attributeName));
}
}
}
}
if (results != null && results.get("memberof_group") != null) {
for (Object o : ((List) results.get("memberof_group"))) {
String groupName = (String) o;
user.getGroups().add(groupName);
}
}
return user;
} else {
IPACall listGroups = new IPACall();
listGroups.setId(0);
listGroups.setMethod("group_find");
ArrayList<String> userArray = new ArrayList<String>();
userArray.add("");
listGroups.getParams().add(userArray);
HashMap<String, String> additionalParams = new HashMap<String, String>();
additionalParams.put("pkey_only", "true");
additionalParams.put("sizelimit", "0");
listGroups.getParams().add(additionalParams);
IPAResponse resp = this.executeIPACall(listGroups, con);
List<Map> groups = (List<Map>) resp.getResult().getResult();
List<IPACall> groupsToFind = new ArrayList<IPACall>();
for (Map group : groups) {
IPACall showGroup = new IPACall();
showGroup.setId(0);
showGroup.setMethod("group_show");
ArrayList<String> groupName = new ArrayList<String>();
groupName.add(((List) group.get("cn")).get(0).toString());
showGroup.getParams().add(groupName);
additionalParams = new HashMap<String, String>();
additionalParams.put("no_members", "true");
showGroup.getParams().add(additionalParams);
groupsToFind.add(showGroup);
}
IPACall groupDetails = new IPACall();
groupDetails.setId(0);
groupDetails.setMethod("batch");
groupDetails.getParams().add(groupsToFind);
additionalParams = new HashMap<String, String>();
groupDetails.getParams().add(additionalParams);
IPABatchResponse batchResp = this.executeIPABatchCall(groupDetails, con);
User user = new User();
user.setUserID(userID);
user.getAttribs().put("uid", new Attribute("uid", userID));
if (batchResp.getResult() != null) {
for (IPATopResult res : batchResp.getResult().getResults()) {
String groupName = (String) res.getValue();
if (((Map) res.getResult()).containsKey("ipaexternalmember")) {
List<String> vals = (List<String>) ((Map) res.getResult()).get("ipaexternalmember");
for (String val : vals) {
if (val.equalsIgnoreCase(userID)) {
user.getGroups().add(groupName);
break;
}
}
}
}
}
// call id_override
IPACall idOveride = new IPACall();
idOveride.setId(0);
idOveride.setMethod("idoverrideuser_show");
List<String> params = new ArrayList<String>();
params.add(this.trustViewName);
params.add(userID);
idOveride.getParams().add(params);
Map<String, Object> param2 = new HashMap<String, Object>();
param2.put("all", true);
param2.put("rights", false);
idOveride.getParams().add(param2);
resp = null;
try {
resp = this.executeIPACall(idOveride, con);
Map<String, List<String>> attrFromIpa = (Map<String, List<String>>) resp.getResult().getResult();
for (String attrName : attrFromIpa.keySet()) {
if (attributes.contains(attrName)) {
Attribute attrToAdd = new Attribute(attrName);
attrToAdd.getValues().addAll(attrFromIpa.get(attrName));
user.getAttribs().put(attrName, attrToAdd);
}
}
} catch (IPAException e) {
if (!e.getMessage().contains("User ID override not found")) {
throw e;
} else {
request.put("freeipa.exists", false);
}
}
return user;
}
}
Aggregations