001/*
002 * (C) Copyright 2012 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 *     bjalon
016 */
017package org.nuxeo.ecm.mobile.webengine;
018
019import javax.servlet.http.Cookie;
020import javax.servlet.http.HttpServletRequest;
021import javax.servlet.http.HttpServletResponse;
022import javax.ws.rs.GET;
023import javax.ws.rs.Path;
024import javax.ws.rs.Produces;
025import javax.ws.rs.core.Context;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.ecm.platform.ui.web.auth.service.PluggableAuthenticationService;
030import org.nuxeo.ecm.webengine.model.WebObject;
031import org.nuxeo.ecm.webengine.model.impl.DefaultObject;
032import org.nuxeo.runtime.api.Framework;
033
034/**
035 * Manage authentication form and logout action
036 *
037 * @author <a href="mailto:bjalon@nuxeo.com">Benjamin JALON</a>
038 * @since 5.5
039 */
040@WebObject(type = "WebMobileAuthentication")
041@Produces("text/html;charset=UTF-8")
042public class WebMobileAuthentication extends DefaultObject {
043
044    private static final Log log = LogFactory.getLog(WebMobileAuthentication.class);
045
046    private PluggableAuthenticationService service;
047
048    private String nuxeoContextPath;
049
050    @GET
051    @Path("login")
052    public Object doLogin() {
053        return getView("login-mobile");
054    }
055
056    @GET
057    @Path("logout")
058    public Object doLogout(@Context HttpServletResponse response, @Context HttpServletRequest request) {
059
060        Cookie cookie = new Cookie("JSESSIONID", null);
061        cookie.setMaxAge(0);
062        cookie.setPath("/");
063
064        response.addCookie(cookie);
065        getService().invalidateSession(request);
066
067        return redirect(getNuxeoContextPath());
068    }
069
070    private String getNuxeoContextPath() {
071        if (nuxeoContextPath == null) {
072            nuxeoContextPath = Framework.getProperty("org.nuxeo.ecm.contextPath");
073        }
074        return nuxeoContextPath;
075    }
076
077    private PluggableAuthenticationService getService() {
078        if (service == null && Framework.getRuntime() != null) {
079            service = (PluggableAuthenticationService) Framework.getRuntime().getComponent(
080                    PluggableAuthenticationService.NAME);
081            // init preFilters
082            service.initPreFilters();
083        }
084        return service;
085
086    }
087
088}