Search in sources :

Example 16 with HttpCon

use of com.tremolosecurity.provisioning.util.HttpCon in project OpenUnison by TremoloSecurity.

the class AttributeChange method getIdFromEmail.

public String getIdFromEmail(String email) throws ProvisioningException {
    HttpCon con = null;
    try {
        con = this.createClient();
        email = email.replace("'", "''");
        String json = this.callWS(con, new StringBuilder().append("/users?$filter=").append(URLEncoder.encode(new StringBuilder().append("mail eq '").append(email).append("'").toString(), "UTf-8")).toString());
        JSONObject root = (JSONObject) new JSONParser().parse(json);
        JSONArray vals = (JSONArray) root.get("value");
        if (vals.size() == 0) {
            return null;
        } else if (vals.size() > 1) {
            throw new ProvisioningException("Multiple entries for " + email);
        } else {
            return ((JSONObject) vals.get(0)).get("userPrincipalName").toString();
        }
    } catch (Exception e) {
        throw new ProvisioningException("Could not find user", e);
    } finally {
        try {
            con.getHttp().close();
        } catch (IOException e) {
        }
        con.getBcm().close();
    }
}
Also used : HttpCon(com.tremolosecurity.provisioning.util.HttpCon) JSONObject(org.json.simple.JSONObject) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException) ParseException(org.json.simple.parser.ParseException) MalformedURLException(java.net.MalformedURLException) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 17 with HttpCon

use of com.tremolosecurity.provisioning.util.HttpCon in project OpenUnison by TremoloSecurity.

the class MatterMostProvider method deleteUser.

@Override
public void deleteUser(User user, Map<String, Object> request) throws ProvisioningException {
    int approvalID = 0;
    if (request.containsKey("APPROVAL_ID")) {
        approvalID = (Integer) request.get("APPROVAL_ID");
    }
    Workflow workflow = (Workflow) request.get("WORKFLOW");
    HashSet<String> attrs = new HashSet<String>();
    attrs.add("id");
    attrs.add("username");
    User fromServer = this.findUser(user.getUserID(), attrs, request);
    if (fromServer == null) {
        logger.warn("User '" + user.getUserID() + "' not found");
        return;
    }
    String id = fromServer.getAttribs().get("id").getValues().get(0);
    StringBuilder sb = new StringBuilder();
    sb.append("/api/v4/users/").append(id);
    HttpCon con = null;
    try {
        con = this.createClient();
        String jsonFromMatterMost = this.callDeleteWS(con, sb.toString());
        this.cfgMgr.getProvisioningEngine().logAction(this.name, false, ActionType.Replace, approvalID, workflow, "delete_at", "0");
    } catch (Exception e) {
        throw new ProvisioningException("Could not delete '" + user.getUserID() + "'", e);
    } finally {
        if (con != null) {
            try {
                con.getHttp().close();
            } catch (IOException e) {
            }
            con.getBcm().close();
        }
    }
}
Also used : User(com.tremolosecurity.provisioning.core.User) HttpCon(com.tremolosecurity.provisioning.util.HttpCon) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) Workflow(com.tremolosecurity.provisioning.core.Workflow) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException) ParseException(org.json.simple.parser.ParseException) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 18 with HttpCon

use of com.tremolosecurity.provisioning.util.HttpCon in project OpenUnison by TremoloSecurity.

the class MatterMostProvider method createClient.

public HttpCon createClient() throws Exception {
    ArrayList<Header> defheaders = new ArrayList<Header>();
    defheaders.add(new BasicHeader("X-Csrf-Token", "1"));
    BasicHttpClientConnectionManager bhcm = new BasicHttpClientConnectionManager(cfgMgr.getHttpClientSocketRegistry());
    RequestConfig rc = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).setRedirectsEnabled(false).build();
    CloseableHttpClient http = HttpClients.custom().setConnectionManager(bhcm).setDefaultHeaders(defheaders).setDefaultRequestConfig(rc).build();
    HttpCon con = new HttpCon();
    con.setBcm(bhcm);
    con.setHttp(http);
    return con;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpCon(com.tremolosecurity.provisioning.util.HttpCon) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) ArrayList(java.util.ArrayList) BasicHeader(org.apache.http.message.BasicHeader) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager)

Example 19 with HttpCon

use of com.tremolosecurity.provisioning.util.HttpCon in project OpenUnison by TremoloSecurity.

the class MatterMostProvider method createUser.

@Override
public void createUser(User user, Set<String> attributes, Map<String, Object> request) throws ProvisioningException {
    int approvalID = 0;
    if (request.containsKey("APPROVAL_ID")) {
        approvalID = (Integer) request.get("APPROVAL_ID");
    }
    Workflow workflow = (Workflow) request.get("WORKFLOW");
    String userID = user.getUserID();
    HttpCon con = null;
    try {
        con = this.createClient();
        JSONObject newUser = new JSONObject();
        for (String attribute : attributes) {
            Attribute attr = user.getAttribs().get(attribute);
            if (attr != null) {
                newUser.put(attr.getName(), attr.getValues().get(0));
            }
        }
        StringBuilder sb = new StringBuilder();
        for (String group : user.getGroups()) {
            sb.append(group).append(' ');
        }
        String groups = sb.toString().trim();
        if (!groups.isEmpty()) {
            newUser.put("roles", groups);
        }
        if (user.getPassword() != null) {
            // user.setPassword(new GenPasswd(25,true,true,true,true).getPassword());
            newUser.put("password", user.getPassword());
        }
        this.callWSPost(con, "/api/v4/users", newUser.toString());
        this.cfgMgr.getProvisioningEngine().logAction(this.name, true, ActionType.Add, approvalID, workflow, "username", userID);
        for (String attribute : attributes) {
            Attribute attr = user.getAttribs().get(attribute);
            if (attr != null) {
                this.cfgMgr.getProvisioningEngine().logAction(this.name, false, ActionType.Add, approvalID, workflow, attr.getName(), attr.getValues().get(0));
            }
        }
        if (user.getPassword() != null) {
            this.cfgMgr.getProvisioningEngine().logAction(this.name, false, ActionType.Add, approvalID, workflow, "password", "*******");
        }
        for (String group : user.getGroups()) {
            this.cfgMgr.getProvisioningEngine().logAction(this.name, false, ActionType.Add, approvalID, workflow, "role", group);
        }
    } catch (Exception e) {
        throw new ProvisioningException("Could create '" + userID + "'", e);
    } finally {
        if (con != null) {
            try {
                con.getHttp().close();
            } catch (IOException e) {
            }
            con.getBcm().close();
        }
    }
}
Also used : HttpCon(com.tremolosecurity.provisioning.util.HttpCon) JSONObject(org.json.simple.JSONObject) Attribute(com.tremolosecurity.saml.Attribute) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) Workflow(com.tremolosecurity.provisioning.core.Workflow) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException) ParseException(org.json.simple.parser.ParseException) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) IOException(java.io.IOException)

Example 20 with HttpCon

use of com.tremolosecurity.provisioning.util.HttpCon in project OpenUnison by TremoloSecurity.

the class MatterMostProvider method findUser.

@Override
public User findUser(String userID, Set<String> attributes, Map<String, Object> request) throws ProvisioningException {
    userID = userID.toLowerCase();
    HttpCon con = null;
    try {
        con = this.createClient();
        JSONObject mmUser = loadUserJson(userID, con);
        if (mmUser == null) {
            return null;
        }
        User user = new User(userID);
        for (String attribute : attributes) {
            Object val = mmUser.get(attribute);
            if (val != null) {
                user.getAttribs().put(attribute, new Attribute(attribute, val.toString()));
            }
        }
        String groups = (String) mmUser.get("roles");
        if (groups != null) {
            StringTokenizer toker = new StringTokenizer(groups, " ", false);
            while (toker.hasMoreTokens()) {
                user.getGroups().add(toker.nextToken());
            }
        }
        return user;
    } catch (Exception e) {
        throw new ProvisioningException("Could not load '" + userID + "'", e);
    } finally {
        if (con != null) {
            try {
                con.getHttp().close();
            } catch (IOException e) {
            }
            con.getBcm().close();
        }
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) HttpCon(com.tremolosecurity.provisioning.util.HttpCon) User(com.tremolosecurity.provisioning.core.User) JSONObject(org.json.simple.JSONObject) Attribute(com.tremolosecurity.saml.Attribute) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) JSONObject(org.json.simple.JSONObject) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException) ParseException(org.json.simple.parser.ParseException) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) IOException(java.io.IOException)

Aggregations

HttpCon (com.tremolosecurity.provisioning.util.HttpCon)104 ProvisioningException (com.tremolosecurity.provisioning.core.ProvisioningException)82 IOException (java.io.IOException)70 ClientProtocolException (org.apache.http.client.ClientProtocolException)49 JSONObject (org.json.simple.JSONObject)43 ParseException (org.json.simple.parser.ParseException)33 Workflow (com.tremolosecurity.provisioning.core.Workflow)32 ArrayList (java.util.ArrayList)32 UnsupportedEncodingException (java.io.UnsupportedEncodingException)31 OpenShiftTarget (com.tremolosecurity.unison.openshiftv3.OpenShiftTarget)27 JSONParser (org.json.simple.parser.JSONParser)25 HashMap (java.util.HashMap)24 JSONArray (org.json.simple.JSONArray)22 User (com.tremolosecurity.provisioning.core.User)18 Attribute (com.tremolosecurity.saml.Attribute)17 Gson (com.google.gson.Gson)16 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)14 HashSet (java.util.HashSet)13 List (java.util.List)13 KSToken (com.tremolosecurity.unison.openstack.util.KSToken)12