Example usage for com.liferay.portal.util PrefsPropsUtil getString

List of usage examples for com.liferay.portal.util PrefsPropsUtil getString

Introduction

In this page you can find the example usage for com.liferay.portal.util PrefsPropsUtil getString.

Prototype

public static String getString(PortletPreferences preferences, String name, String defaultValue) 

Source Link

Usage

From source file:com.ext.portal.servlet.filters.sso.cas.CASFilterExt.java

License:Open Source License

protected TicketValidator getTicketValidator(long companyId) throws Exception {

    TicketValidator ticketValidator = _ticketValidators.get(companyId);

    if (ticketValidator != null) {
        return ticketValidator;
    }/*from   ww w  .  j a v a2s  . co m*/

    String serverName = PrefsPropsUtil.getString(companyId, PropsKeys.CAS_SERVER_NAME,
            PropsValues.CAS_SERVER_NAME);
    String serverUrl = PrefsPropsUtil.getString(companyId, PropsKeys.CAS_SERVER_URL,
            PropsValues.CAS_SERVER_URL);
    String loginUrl = PrefsPropsUtil.getString(companyId, PropsKeys.CAS_LOGIN_URL, PropsValues.CAS_LOGIN_URL);

    Cas20ProxyTicketValidator cas20ProxyTicketValidator = new Cas20ProxyTicketValidator(serverUrl);

    Map<String, String> parameters = new HashMap<String, String>();

    parameters.put("serverName", serverName);
    parameters.put("casServerUrlPrefix", serverUrl);
    parameters.put("casServerLoginUrl", loginUrl);
    parameters.put("redirectAfterValidation", "false");

    cas20ProxyTicketValidator.setCustomParameters(parameters);

    _ticketValidators.put(companyId, cas20ProxyTicketValidator);

    return cas20ProxyTicketValidator;
}

From source file:com.ext.portal.servlet.filters.sso.cas.CASFilterExt.java

License:Open Source License

@Override
protected void processFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
        throws Exception {

    HttpSession session = request.getSession();

    long companyId = PortalUtil.getCompanyId(request);

    String pathInfo = request.getPathInfo();

    Object forceLogout = session.getAttribute(WebKeys.CAS_FORCE_LOGOUT);

    if (forceLogout != null) {
        session.removeAttribute(WebKeys.CAS_FORCE_LOGOUT);

        String logoutUrl = PrefsPropsUtil.getString(companyId, PropsKeys.CAS_LOGOUT_URL,
                PropsValues.CAS_LOGOUT_URL);

        response.sendRedirect(logoutUrl);

        return;/* w w w  .  j av a2 s. co m*/
    }

    if (pathInfo.contains("/portal/logout")) {
        session.invalidate();

        String logoutUrl = PrefsPropsUtil.getString(companyId, PropsKeys.CAS_LOGOUT_URL,
                PropsValues.CAS_LOGOUT_URL);

        response.sendRedirect(logoutUrl);

        return;
    } else {
        String login = (String) session.getAttribute(WebKeys.CAS_LOGIN);

        if (Validator.isNotNull(login)) {
            processFilter(CASFilterExt.class, request, response, filterChain);

            return;
        }

        String serverName = PrefsPropsUtil.getString(companyId, PropsKeys.CAS_SERVER_NAME,
                PropsValues.CAS_SERVER_NAME);

        String serviceUrl = PrefsPropsUtil.getString(companyId, PropsKeys.CAS_SERVICE_URL,
                PropsValues.CAS_SERVICE_URL);

        if (Validator.isNull(serviceUrl)) {
            serviceUrl = CommonUtils.constructServiceUrl(request, response, serviceUrl, serverName, "ticket",
                    false);
        }

        String ticket = ParamUtil.getString(request, "ticket");

        if (Validator.isNull(ticket)) {
            String loginUrl = PrefsPropsUtil.getString(companyId, PropsKeys.CAS_LOGIN_URL,
                    PropsValues.CAS_LOGIN_URL);

            loginUrl = HttpUtil.addParameter(loginUrl, "service", serviceUrl);

            response.sendRedirect(loginUrl);

            return;
        }

        TicketValidator ticketValidator = getTicketValidator(companyId);

        Assertion assertion = ticketValidator.validate(ticket, serviceUrl);

        if (assertion != null) {
            AttributePrincipal attributePrincipal = assertion.getPrincipal();

            login = attributePrincipal.getName();

            session.setAttribute(WebKeys.CAS_LOGIN, login);

            // Try to add user if it doesn't exist

            //_log.info(attributePrincipal);
            _createUserFromCAS(companyId, login, assertion);
        }
    }

    processFilter(CASFilterExt.class, request, response, filterChain);
}

From source file:com.liferay.portlet.login.action.STORKAction.java

License:Apache License

@Override
public void processAction(ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
        ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {
    ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
    long companyId = themeDisplay.getCompanyId();

    if (!STORKUtil.isEnabled(companyId)) {
        throw new PrincipalException();
    }//  w ww  .  j  a  va2 s  . co  m

    String storkMandatoryAttr = PrefsPropsUtil.getString(companyId, FedPropsKeys.STORK_AUTH_LOCAL_SEARCH_FILTER,
            FedPropsValues.STORK_AUTH_LOCAL_SEARCH_FILTER);
    Map<String, String> storkUserMapping = getAttrMap(
            PrefsPropsUtil.getString(companyId, FedPropsKeys.STORK_USER_MAPPING));

    if (ParamUtil.getString(actionRequest, "StorkAction", "none").equals("login")) {
        byte[] decSamlToken = PEPSUtil.decodeSAMLToken(ParamUtil.getString(actionRequest, "SAMLResponse"));
        STORKSAMLEngine storkEngine = STORKSAMLEngine.getInstance("SP");
        STORKAuthnResponse authnResponse = null;
        try {
            authnResponse = storkEngine.validateSTORKAuthnResponse(decSamlToken,
                    PortalUtil.getHttpServletRequest(actionRequest).getRemoteHost());
        } catch (Exception ex) {
            _log.error("Could not validate token for Saml Response");
            _log.error(ex);
            setForward(actionRequest, "portlet.login.stork.error");
            return;
        }
        _log.debug("Authentication response status: " + authnResponse.getStatusCode() + "(reason: "
                + authnResponse.getSubStatusCode() + ") and this is a fail: " + authnResponse.isFail());
        if (!authnResponse.isFail()) {
            Map<String, PersonalAttribute> mPersAttr = createPersonalAttributeMap(
                    authnResponse.getPersonalAttributeList().values());

            User user = null;
            if (storkMandatoryAttr.equals("screenName")) {
                _log.debug("Finding user using the " + storkUserMapping.get("screenName"));
                if (mPersAttr.containsKey(storkUserMapping.get("screenName"))) {
                    Iterator<String> pa = mPersAttr.get(storkUserMapping.get("screenName")).getValue()
                            .iterator();

                    while (pa.hasNext() && user == null) {
                        String screenName = pa.next();
                        try {
                            user = UserLocalServiceUtil.getUserByScreenName(companyId, screenName);
                        } catch (NoSuchUserException nse) {
                            _log.info("User screenName: " + screenName + " is not registered");
                        }
                    }
                } else {
                    actionResponse.sendRedirect(
                            PrefsPropsUtil.getString(companyId, FedPropsKeys.STORK_AUTH_PAGE_MISS_ATTRIBUTE,
                                    FedPropsValues.STORK_AUTH_PAGE_MISS_ATTRIBUTE));
                    _log.info("Stork authentication miss the matching attribute. Impossible to identify users");
                    return;
                }
            }
            if (storkMandatoryAttr.equals("uuid")) {
                _log.debug("Finding user using the " + storkUserMapping.get("uuid"));
                if (mPersAttr.containsKey(storkUserMapping.get("uuid"))) {
                    Iterator<String> pa = mPersAttr.get(storkUserMapping.get("uuid")).getValue().iterator();

                    while (pa.hasNext() && user == null) {
                        String uuid = pa.next();
                        try {
                            user = UserLocalServiceUtil.getUserByUuid(uuid);
                        } catch (NoSuchUserException nse) {
                            _log.info("User uuid: " + uuid + " is not registered");
                        }
                    }
                } else {
                    actionResponse.sendRedirect(
                            PrefsPropsUtil.getString(companyId, FedPropsKeys.STORK_AUTH_PAGE_MISS_ATTRIBUTE,
                                    FedPropsValues.STORK_AUTH_PAGE_MISS_ATTRIBUTE));
                    _log.info("Stork authentication miss the matching attribute. Impossible to identify users");
                    return;
                }

            }
            if (storkMandatoryAttr.equals("emailAddress")) {
                _log.debug("Finding user using the " + storkUserMapping.get("emailAddress"));
                if (mPersAttr.containsKey(storkUserMapping.get("emailAddress"))) {
                    Iterator<String> pa = mPersAttr.get(storkUserMapping.get("emailAddress")).getValue()
                            .iterator();

                    while (pa.hasNext() && user == null) {
                        Pattern pat = Pattern.compile("[\\w\\-]([\\.\\w\\-])+@([\\w\\-]+\\.)+[a-zA-Z]{2,4}");
                        Matcher mailMatch;

                        mailMatch = pat.matcher(pa.next());
                        while (mailMatch.find() && user == null) {
                            if (Validator.isNotNull(mailMatch.group())) {
                                try {
                                    user = UserLocalServiceUtil.getUserByEmailAddress(companyId,
                                            mailMatch.group());
                                } catch (NoSuchUserException nse) {
                                    _log.info("Mail: " + mailMatch.group() + " is not registered");
                                }
                            }
                        }

                    }
                } else {
                    actionResponse.sendRedirect(
                            PrefsPropsUtil.getString(companyId, FedPropsKeys.STORK_AUTH_PAGE_MISS_ATTRIBUTE,
                                    FedPropsValues.STORK_AUTH_PAGE_MISS_ATTRIBUTE));
                    _log.info("Stork authentication miss the matching attribute. Impossible to identify users");
                    return;
                }

            }

            if (user == null && PrefsPropsUtil.getBoolean(companyId, FedPropsKeys.STORK_AUTH_LDLAP_CHECK,
                    FedPropsValues.STORK_AUTH_LDLAP_CHECK)) {
                _log.debug("User not found, check on LDAP");
                //                    user=getUserFromLdap();

                String originalLdapFilter = PrefsPropsUtil.getString(themeDisplay.getCompanyId(),
                        FedPropsKeys.STORK_AUTH_LDAP_SEARCH_FILTER,
                        FedPropsValues.STORK_AUTH_LDAP_SEARCH_FILTER);
                List<String> lstLdapFilter = null;
                try {
                    lstLdapFilter = generateFilters(companyId,
                            mPersAttr.get(storkUserMapping.get("screenName")),
                            mPersAttr.get(storkUserMapping.get("emailAddress")),
                            mPersAttr.get(storkUserMapping.get("firstName")),
                            mPersAttr.get(storkUserMapping.get("lastName")), originalLdapFilter);
                } catch (STORKException se) {
                    _log.error(se.getMessage());
                    actionResponse.sendRedirect(
                            PrefsPropsUtil.getString(companyId, FedPropsKeys.STORK_AUTH_PAGE_MISS_ATTRIBUTE,
                                    FedPropsValues.STORK_AUTH_PAGE_MISS_ATTRIBUTE));
                    return;
                }
                String[] idLDAPS = PrefsPropsUtil.getStringArray(companyId, "ldap.server.ids", ",");
                String idLDAP;
                int idLDAPCounter = 0;
                while (user == null && idLDAPCounter < idLDAPS.length) {
                    idLDAP = idLDAPS[idLDAPCounter++];

                    String mailMap = null;
                    String userMaps[] = PrefsPropsUtil
                            .getString(companyId, PropsKeys.LDAP_USER_MAPPINGS + "." + idLDAP).split("\n");
                    int mIndex = 0;
                    while (mailMap == null && mIndex < userMaps.length) {
                        String map = userMaps[mIndex++];
                        if (map.indexOf("=") == -1 || map.split("=").length != 2) {
                            continue;
                        }
                        String[] sMap = map.split("=");
                        if (sMap[0].equals("emailAddress")) {
                            mailMap = sMap[1];
                        }
                    }

                    if (mailMap == null) {
                        _log.warn("LDAP server configured without the mail map");
                        continue;
                    }

                    LDAPUtil samlLdapUtil = new LDAPUtil(
                            PrefsPropsUtil.getString(companyId,
                                    PropsKeys.LDAP_BASE_PROVIDER_URL + "." + idLDAP),
                            PrefsPropsUtil.getString(companyId, PropsKeys.LDAP_BASE_DN + "." + idLDAP));

                    Iterator<String> ldapFilter = lstLdapFilter.iterator();

                    while (ldapFilter.hasNext() && user == null) {
                        String mail = samlLdapUtil.getUserAttribute(
                                PrefsPropsUtil.getString(companyId,
                                        PropsKeys.LDAP_IMPORT_USER_SEARCH_FILTER + "." + idLDAP),
                                ldapFilter.next(), mailMap);

                        if (mail != null) {
                            try {
                                user = UserLocalServiceUtil.getUserByEmailAddress(companyId, mail);
                            } catch (NoSuchUserException nse) {
                                _log.debug("Mail: " + mail + " found in LDAP but it is not registered");
                            }
                        }
                    }
                }

            }

            if (user == null) {
                _log.info("Impossible to find a user with the current attributes");
                actionResponse.sendRedirect(PrefsPropsUtil.getString(themeDisplay.getCompanyId(),
                        FedPropsKeys.STORK_AUTH_PAGE_MISS_USER, FedPropsValues.STORK_AUTH_PAGE_MISS_USER));
                return;
            }

            HttpSession session = PortalUtil.getHttpServletRequest(actionRequest).getSession();
            session.setAttribute(FedWebKeys.STORK_ID_LOGIN, new Long(user.getUserId()));

            //                sendRedirect(actionRequest, actionResponse, PortalUtil.getPortalURL(actionRequest) + themeDisplay.getURLSignIn());
            sendRedirect(actionRequest, actionResponse, ParamUtil.getString(actionRequest, "redirect",
                    PortalUtil.getPortalURL(actionRequest) + themeDisplay.getURLSignIn()));
        } else {
            setForward(actionRequest, "portlet.login.stork.notAuth");
        }

    } else {

        if (Validator.isNull(ParamUtil.getString(actionRequest, "citizenCountry"))) {
            SessionErrors.add(actionRequest, "missUserCountry");
            return;
        }
        STORKAuthnRequest authnRequest = new STORKAuthnRequest();

        authnRequest.setCitizenCountryCode(ParamUtil.getString(actionRequest, "citizenCountry"));

        authnRequest
                .setIssuer(PrefsPropsUtil.getString(themeDisplay.getCompanyId(), FedPropsKeys.STORK_SP_NAME));

        authnRequest.setDestination(
                PrefsPropsUtil.getString(themeDisplay.getCompanyId(), FedPropsKeys.STORK_SPEPS_URL));

        authnRequest.setProviderName(
                PrefsPropsUtil.getString(themeDisplay.getCompanyId(), FedPropsKeys.STORK_SP_NAME));

        authnRequest
                .setQaa(PrefsPropsUtil.getInteger(themeDisplay.getCompanyId(), FedPropsKeys.STORK_SP_QAALEVEL));

        ActionResponseImpl actionResponseImpl = (ActionResponseImpl) actionResponse;
        PortletURL portletURL = actionResponseImpl.createActionURL();
        portletURL.setParameter("struts_action", "/login/stork");
        portletURL.setParameter("StorkAction", "login");
        portletURL.setParameter("saveLastPath", "0");
        portletURL.setParameter("redirect",
                ParamUtil.getString(actionRequest, "redirect", themeDisplay.getPathMain()));
        portletURL.setWindowState(WindowState.NORMAL);

        authnRequest.setAssertionConsumerServiceURL(portletURL.toString());
        _log.debug("STORK Return url: " + portletURL.toString());

        authnRequest.setSpSector(
                PrefsPropsUtil.getString(themeDisplay.getCompanyId(), FedPropsKeys.STORK_SP_SECTOR));

        authnRequest.setSpInstitution(
                PrefsPropsUtil.getString(themeDisplay.getCompanyId(), FedPropsKeys.STORK_SP_NAME));

        authnRequest.setSpApplication(
                PrefsPropsUtil.getString(themeDisplay.getCompanyId(), FedPropsKeys.STORK_SP_APLICATION));

        authnRequest.setSpCountry(
                PrefsPropsUtil.getString(themeDisplay.getCompanyId(), FedPropsKeys.STORK_SP_COUNTRY));

        authnRequest.setSPID(PrefsPropsUtil.getString(themeDisplay.getCompanyId(), FedPropsKeys.STORK_SP_NAME));

        IPersonalAttributeList pAttList = new PersonalAttributeList();

        boolean eIdentifier = false;
        if (storkUserMapping != null) {

            for (String attrMap : storkUserMapping.keySet()) {
                PersonalAttribute attr = new PersonalAttribute();
                attr.setName(storkUserMapping.get(attrMap));
                if (attrMap.equals(storkMandatoryAttr) && storkUserMapping.get(attrMap).equals("eIdentifier")) {
                    attr.setIsRequired(true);
                    eIdentifier = true;
                    _log.debug("Attribute " + attrMap + " mapped in " + storkUserMapping.get(attrMap)
                            + " is required");
                } else {
                    if (attrMap.equals(storkMandatoryAttr)) {
                        attr.setIsRequired(true);
                        _log.debug("Attribute " + attrMap + " mapped in " + storkUserMapping.get(attrMap)
                                + " is required");
                    } else {
                        attr.setIsRequired(false);
                        _log.debug("Attribute " + attrMap + " mapped in " + storkUserMapping.get(attrMap)
                                + " is not required");

                    }
                }
                pAttList.add(attr);
            }
            if (!eIdentifier) {
                pAttList.add(new PersonalAttribute("eIdentifier", true, null, null));
            }
        }

        authnRequest.setPersonalAttributeList(pAttList);

        byte token[] = null;
        try {

            STORKSAMLEngine storkEngine = STORKSAMLEngine.getInstance("SP");
            token = storkEngine.generateSTORKAuthnRequest(authnRequest).getTokenSaml();

        } catch (Exception ex) {
            _log.error("Impossible to create the SAML token");
            _log.error(ex);
            setForward(actionRequest, "portlet.login.stork.error");
        }

        if (token != null) {
            actionResponse.setRenderParameter("SAMLToken", PEPSUtil.encodeSAMLToken(token));
            actionResponse.setRenderParameter("CCountry", ParamUtil.getString(actionRequest, "citizenCountry"));
            actionResponse.setRenderParameter("PEPSUrl",
                    PrefsPropsUtil.getString(themeDisplay.getCompanyId(), FedPropsKeys.STORK_SPEPS_URL));
            setForward(actionRequest, "portlet.login.stork.peps");
        }
    }
}

From source file:org.intalio.tempo.web.CASFilter520.java

License:Open Source License

protected Filter getCASFilter(long companyId) throws Exception {
    edu.yale.its.tp.cas.client.filter.CASFilter casFilter = _casFilters.get(companyId);

    if (casFilter == null) {
        casFilter = new edu.yale.its.tp.cas.client.filter.CASFilter();

        DynamicFilterConfig config = new DynamicFilterConfig(_filterName, _servletContext);

        String serverName = PrefsPropsUtil.getString(companyId, PropsKeys.CAS_SERVER_NAME,
                PropsValues.CAS_SERVER_NAME);
        String serviceUrl = PrefsPropsUtil.getString(companyId, PropsKeys.CAS_SERVICE_URL,
                PropsValues.CAS_SERVICE_URL);

        config.addInitParameter(edu.yale.its.tp.cas.client.filter.CASFilter.LOGIN_INIT_PARAM,
                PrefsPropsUtil.getString(companyId, PropsKeys.CAS_LOGIN_URL, PropsValues.CAS_LOGIN_URL));

        if (Validator.isNotNull(serviceUrl)) {
            config.addInitParameter(edu.yale.its.tp.cas.client.filter.CASFilter.SERVICE_INIT_PARAM, serviceUrl);
        } else {// www. j  av a2 s  .  c om
            config.addInitParameter(edu.yale.its.tp.cas.client.filter.CASFilter.SERVERNAME_INIT_PARAM,
                    serverName);
        }

        config.addInitParameter(edu.yale.its.tp.cas.client.filter.CASFilter.VALIDATE_INIT_PARAM,
                PrefsPropsUtil.getString(companyId, PropsKeys.CAS_VALIDATE_URL, PropsValues.CAS_VALIDATE_URL));

        //Add proxy call back url
        config.addInitParameter(edu.yale.its.tp.cas.client.filter.CASFilter.PROXY_CALLBACK_INIT_PARAM,
                PrefsPropsUtil.getString(companyId, "cas.proxycallback.url"));

        casFilter.init(config);

        _casFilters.put(companyId, casFilter);
    }

    return casFilter;
}

From source file:org.intalio.tempo.web.CASFilter520.java

License:Open Source License

protected void processFilter(HttpServletRequest request, HttpServletResponse response,
        FilterChain filterChain) {//from w  w w.j  ava  2s.c o  m

    try {
        long companyId = PortalUtil.getCompanyId(request);

        if (PrefsPropsUtil.getBoolean(companyId, PropsKeys.CAS_AUTH_ENABLED, PropsValues.CAS_AUTH_ENABLED)) {

            String pathInfo = request.getPathInfo();

            if (pathInfo.indexOf("/portal/logout") != -1) {
                HttpSession session = request.getSession();

                session.invalidate();

                String logoutUrl = PrefsPropsUtil.getString(companyId, PropsKeys.CAS_LOGOUT_URL,
                        PropsValues.CAS_LOGOUT_URL);

                response.sendRedirect(logoutUrl);
            } else {
                Filter casFilter = getCASFilter(companyId);

                casFilter.doFilter(request, response, filterChain);
            }
        } else {
            processFilter(CASFilter.class, request, response, filterChain);
        }
    } catch (Exception e) {
        _log.error(e, e);
    }
}