001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.opencmis.bindings;
020
021import java.io.Serializable;
022import java.security.Principal;
023import java.util.HashMap;
024import java.util.Map;
025
026import javax.servlet.http.HttpServletRequest;
027
028import org.apache.chemistry.opencmis.commons.server.CallContext;
029import org.apache.chemistry.opencmis.server.shared.CallContextHandler;
030
031/**
032 * Call Context Handler for Nuxeo authentication that extracts the relevant user name.
033 * <p>
034 * Configured as a "callContextHandler" servlet parameter in the AtomPub and JSON servlets.
035 * <p>
036 * Authentication happened earlier in the chain through Nuxeo's authentication filter, and a JAAS context has already
037 * been set up.
038 * <p>
039 * There is no password available, as authentication is opaque and may use SSO.
040 */
041public class NuxeoAuthCallContextHandler implements CallContextHandler, Serializable {
042
043    private static final long serialVersionUID = 1L;
044
045    @Override
046    public Map<String, String> getCallContextMap(HttpServletRequest request) {
047        Principal principal = request.getUserPrincipal();
048        Map<String, String> result = new HashMap<>();
049        if (principal != null) {
050            result.put(CallContext.USERNAME, principal.getName());
051        }
052        return result;
053    }
054
055}