Search in sources :

Example 11 with ScaleError

use of com.tremolosecurity.scalejs.data.ScaleError in project OpenUnison by TremoloSecurity.

the class ScaleJSOperator method doFilter.

@Override
public void doFilter(HttpFilterRequest request, HttpFilterResponse response, HttpFilterChain chain) throws Exception {
    Gson gson = new Gson();
    request.getServletRequest().setAttribute("com.tremolosecurity.unison.proxy.noRedirectOnError", "com.tremolosecurity.unison.proxy.noRedirectOnError");
    try {
        if (request.getRequestURI().endsWith("/ops/config")) {
            ScaleJSUtils.addCacheHeaders(response);
            response.setContentType("application/json");
            response.getWriter().println(gson.toJson(this.config).trim());
        } else if (request.getRequestURI().endsWith("/ops/search")) {
            runSearch(request, response, gson);
        } else if (request.getRequestURI().endsWith("/ops/user") && request.getMethod().equalsIgnoreCase("GET")) {
            lookupUser(request, response, gson);
        } else if (request.getRequestURI().endsWith("/ops/user") && request.getMethod().equalsIgnoreCase("POST")) {
            AuthInfo loggedIn = ((AuthController) request.getSession().getAttribute(ProxyConstants.AUTH_CTL)).getAuthInfo();
            String json = new String((byte[]) request.getAttribute(ProxySys.MSG_BODY));
            OpsUpdate updateInput = gson.fromJson(json, OpsUpdate.class);
            if (this.scaleMainConfig == null) {
                UrlHolder holder = GlobalEntries.getGlobalEntries().getConfigManager().findURL(this.scaleMainURL);
                for (HttpFilter filter : holder.getFilterChain()) {
                    if (filter instanceof ScaleMain) {
                        ScaleMain scaleMain = (ScaleMain) filter;
                        this.scaleMainConfig = scaleMain.scaleConfig;
                    }
                }
            }
            String dn = updateInput.getDn();
            LDAPSearchResults res = GlobalEntries.getGlobalEntries().getConfigManager().getMyVD().search(dn, 0, "(objectClass=*)", new ArrayList<String>());
            if (!res.hasMore()) {
                throw new Exception("Could not locate user '" + dn + "'");
            }
            LDAPEntry entry = res.next();
            AuthInfo userData = new AuthInfo();
            userData.setUserDN(entry.getDN());
            LDAPAttributeSet attrs = entry.getAttributeSet();
            for (Object obj : attrs) {
                LDAPAttribute attr = (LDAPAttribute) obj;
                Attribute attrib = new Attribute(attr.getName());
                String[] vals = attr.getStringValueArray();
                for (String val : vals) {
                    attrib.getValues().add(val);
                }
                userData.getAttribs().put(attrib.getName(), attrib);
            }
            ScaleError errors = new ScaleError();
            Set<String> allowedAttrs = null;
            if (this.scaleMainConfig.getUiDecisions() != null) {
                allowedAttrs = this.scaleMainConfig.getUiDecisions().availableAttributes(userData, request.getServletRequest());
            }
            HashMap<String, String> values = new HashMap<String, String>();
            boolean ok = true;
            for (Attribute attr : updateInput.getAttributes()) {
                String attributeName = attr.getName();
                if (allowedAttrs == null || allowedAttrs.contains(attributeName)) {
                    String value = attr.getValues().get(0);
                    if (this.scaleMainConfig.getAttributes().get(attributeName) == null) {
                        errors.getErrors().add("Invalid attribute : '" + attributeName + "'");
                        ok = false;
                    } else if (this.scaleMainConfig.getAttributes().get(attributeName).isReadOnly()) {
                        errors.getErrors().add("Attribute is read only : '" + this.scaleMainConfig.getAttributes().get(attributeName).getDisplayName() + "'");
                        ok = false;
                    } else if (this.scaleMainConfig.getAttributes().get(attributeName).isRequired() && value.length() == 0) {
                        errors.getErrors().add("Attribute is required : '" + this.scaleMainConfig.getAttributes().get(attributeName).getDisplayName() + "'");
                        ok = false;
                    } else if (this.scaleMainConfig.getAttributes().get(attributeName).getMinChars() > 0 && this.scaleMainConfig.getAttributes().get(attributeName).getMinChars() > value.length()) {
                        errors.getErrors().add(this.scaleMainConfig.getAttributes().get(attributeName).getDisplayName() + " must have at least " + this.scaleMainConfig.getAttributes().get(attributeName).getMinChars() + " characters");
                        ok = false;
                    } else if (this.scaleMainConfig.getAttributes().get(attributeName).getMaxChars() > 0 && this.scaleMainConfig.getAttributes().get(attributeName).getMaxChars() < value.length()) {
                        errors.getErrors().add(this.scaleMainConfig.getAttributes().get(attributeName).getDisplayName() + " must have at most " + this.scaleMainConfig.getAttributes().get(attributeName).getMaxChars() + " characters");
                        ok = false;
                    } else if (this.scaleMainConfig.getAttributes().get(attributeName).getPattern() != null) {
                        try {
                            Matcher m = this.scaleMainConfig.getAttributes().get(attributeName).getPattern().matcher(value);
                            if (m == null || !m.matches()) {
                                ok = false;
                            }
                        } catch (Exception e) {
                            ok = false;
                        }
                        if (!ok) {
                            errors.getErrors().add("Attribute value not valid : '" + this.scaleMainConfig.getAttributes().get(attributeName).getDisplayName() + "' - " + this.scaleMainConfig.getAttributes().get(attributeName).getRegExFailedMsg());
                        }
                    }
                    values.put(attributeName, value);
                }
            }
            for (String attrName : this.scaleMainConfig.getAttributes().keySet()) {
                if (this.scaleMainConfig.getAttributes().get(attrName).isRequired() && !values.containsKey(attrName) && (allowedAttrs == null || allowedAttrs.contains(attrName))) {
                    errors.getErrors().add("Attribute is required : '" + this.scaleMainConfig.getAttributes().get(attrName).getDisplayName() + "'");
                    ok = false;
                }
            }
            if (updateInput.getReason() == null || updateInput.getReason().trim().isEmpty()) {
                errors.getErrors().add("Reason For Updates Required");
                ok = false;
            }
            if (ok) {
                ConfigManager cfgMgr = GlobalEntries.getGlobalEntries().getConfigManager();
                WFCall wfCall = new WFCall();
                wfCall.setName(this.scaleMainConfig.getWorkflowName());
                wfCall.setReason(updateInput.getReason());
                wfCall.setUidAttributeName(this.scaleMainConfig.getUidAttributeName());
                wfCall.setRequestor(loggedIn.getAttribs().get(this.scaleMainConfig.getUidAttributeName()).getValues().get(0));
                TremoloUser tu = new TremoloUser();
                tu.setUid(userData.getAttribs().get(this.scaleMainConfig.getUidAttributeName()).getValues().get(0));
                for (String name : values.keySet()) {
                    tu.getAttributes().add(new Attribute(name, values.get(name)));
                }
                tu.getAttributes().add(new Attribute(this.scaleMainConfig.getUidAttributeName(), userData.getAttribs().get(this.scaleMainConfig.getUidAttributeName()).getValues().get(0)));
                wfCall.setUser(tu);
                try {
                    com.tremolosecurity.provisioning.workflow.ExecuteWorkflow exec = new com.tremolosecurity.provisioning.workflow.ExecuteWorkflow();
                    exec.execute(wfCall, GlobalEntries.getGlobalEntries().getConfigManager());
                } catch (Exception e) {
                    logger.error("Could not update user", e);
                    response.setStatus(500);
                    ScaleError error = new ScaleError();
                    error.getErrors().add("Please contact your system administrator");
                    ScaleJSUtils.addCacheHeaders(response);
                    response.getWriter().print(gson.toJson(error).trim());
                    response.getWriter().flush();
                }
            } else {
                response.setStatus(500);
                ScaleJSUtils.addCacheHeaders(response);
                response.getWriter().print(gson.toJson(errors).trim());
                response.getWriter().flush();
            }
        }
    } catch (Throwable t) {
        logger.error("Could not execute request", t);
        response.setStatus(500);
        ScaleError error = new ScaleError();
        error.getErrors().add("Operation not supported");
        ScaleJSUtils.addCacheHeaders(response);
        response.getWriter().print(gson.toJson(error).trim());
        response.getWriter().flush();
    }
}
Also used : LDAPAttributeSet(com.novell.ldap.LDAPAttributeSet) Set(java.util.Set) LDAPAttribute(com.novell.ldap.LDAPAttribute) Attribute(com.tremolosecurity.saml.Attribute) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) OpsUpdate(com.tremolosecurity.scalejs.operators.data.OpsUpdate) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) UrlHolder(com.tremolosecurity.config.util.UrlHolder) LDAPEntry(com.novell.ldap.LDAPEntry) TremoloUser(com.tremolosecurity.provisioning.service.util.TremoloUser) HttpFilter(com.tremolosecurity.proxy.filter.HttpFilter) LDAPAttribute(com.novell.ldap.LDAPAttribute) AuthInfo(com.tremolosecurity.proxy.auth.AuthInfo) WFCall(com.tremolosecurity.provisioning.service.util.WFCall) LDAPAttributeSet(com.novell.ldap.LDAPAttributeSet) ScaleError(com.tremolosecurity.scalejs.data.ScaleError) LDAPException(com.novell.ldap.LDAPException) IOException(java.io.IOException) ConfigManager(com.tremolosecurity.config.util.ConfigManager) LDAPSearchResults(com.novell.ldap.LDAPSearchResults) ScaleMain(com.tremolosecurity.scalejs.ws.ScaleMain)

Aggregations

ScaleError (com.tremolosecurity.scalejs.data.ScaleError)11 AuthInfo (com.tremolosecurity.proxy.auth.AuthInfo)10 AuthController (com.tremolosecurity.proxy.auth.AuthController)9 Attribute (com.tremolosecurity.saml.Attribute)7 RichTextString (org.apache.poi.ss.usermodel.RichTextString)7 XSSFRichTextString (org.apache.poi.xssf.usermodel.XSSFRichTextString)7 ConfigManager (com.tremolosecurity.config.util.ConfigManager)6 Gson (com.google.gson.Gson)5 TremoloUser (com.tremolosecurity.provisioning.service.util.TremoloUser)5 WFCall (com.tremolosecurity.provisioning.service.util.WFCall)5 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 LDAPAttribute (com.novell.ldap.LDAPAttribute)4 OrgType (com.tremolosecurity.config.xml.OrgType)4 AzSys (com.tremolosecurity.proxy.auth.AzSys)4 ScaleAttribute (com.tremolosecurity.scalejs.cfg.ScaleAttribute)4 IOException (java.io.IOException)4 HashSet (java.util.HashSet)4 LDAPException (com.novell.ldap.LDAPException)3 LDAPSearchResults (com.novell.ldap.LDAPSearchResults)3