001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Florent Guillaume
011 */
012package org.nuxeo.ecm.core.opencmis.bindings;
013
014import java.io.Serializable;
015import java.security.Principal;
016import java.util.HashMap;
017import java.util.Map;
018
019import javax.servlet.http.HttpServletRequest;
020
021import org.apache.chemistry.opencmis.commons.server.CallContext;
022import org.apache.chemistry.opencmis.server.shared.CallContextHandler;
023
024/**
025 * Call Context Handler for Nuxeo authentication that extracts the relevant user name.
026 * <p>
027 * Configured as a "callContextHandler" servlet parameter in the AtomPub and JSON servlets.
028 * <p>
029 * Authentication happened earlier in the chain through Nuxeo's authentication filter, and a JAAS context has already
030 * been set up. For SOAP, authentication happened through {@link NuxeoCmisAuthHandler} instead of the standard Nuxeo
031 * filter.
032 * <p>
033 * There is no password available, as authentication is opaque and may use SSO.
034 */
035public class NuxeoAuthCallContextHandler implements CallContextHandler, Serializable {
036
037    private static final long serialVersionUID = 1L;
038
039    @Override
040    public Map<String, String> getCallContextMap(HttpServletRequest request) {
041        Principal principal = request.getUserPrincipal();
042        Map<String, String> result = new HashMap<String, String>();
043        if (principal != null) {
044            result.put(CallContext.USERNAME, principal.getName());
045        }
046        return result;
047    }
048
049}