001/*
002 * (C) Copyright 2006-2013 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nelson Silva <nelson.silva@inevo.pt> - initial API and implementation
016 *     Nuxeo
017 */
018
019package org.nuxeo.ecm.platform.oauth2.openid;
020
021import javax.servlet.http.HttpServletRequest;
022
023import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper;
024
025/**
026 * Default RedirectUriResolver that allows overriding the redirect uri by setting a session attribute By default it will
027 * use a fixed redirect uri since some provider do not support wildcards
028 *
029 * @since 5.7
030 */
031public class RedirectUriResolverHelper implements RedirectUriResolver {
032
033    public static final String REDIRECT_URI_SESSION_ATTRIBUTE = "OPENID_REDIRECT_URI";
034
035    @Override
036    public String getRedirectUri(OpenIDConnectProvider openIDConnectProvider, HttpServletRequest request) {
037        String redirectUri = (String) request.getSession().getAttribute(REDIRECT_URI_SESSION_ATTRIBUTE);
038        // TODO - Use the requestedUrl for providers with support for wildcards
039        // String requestedUrl = request.getParameter(NXAuthConstants.REQUESTED_URL);
040        if (redirectUri == null) {
041            redirectUri = VirtualHostHelper.getBaseURL(request) + "nxstartup.faces?" + "" + "provider="
042                    + openIDConnectProvider.oauth2Provider.getServiceName() + "&forceAnonymousLogin=true";
043        }
044        return redirectUri;
045    }
046
047}