001/**
002 * Copyright 2015 DuraSpace, Inc.
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 */
016package org.fcrepo.auth.common;
017
018import org.fcrepo.kernel.api.exception.RepositoryConfigurationException;
019
020import javax.jcr.Credentials;
021import java.security.Principal;
022import java.util.Set;
023
024/**
025 * An example principal provider that extracts principals from request headers.
026 *
027 * @author awoods
028 * @since 2015-10-31
029 */
030public class DelegateHeaderPrincipalProvider extends HttpHeaderPrincipalProvider implements PrincipalProvider {
031
032    private static final String SEP = "no-separator";
033    protected static final String DELEGATE_HEADER = "On-Behalf-Of";
034
035    /**
036     * Default Constructor
037     */
038    public DelegateHeaderPrincipalProvider() {
039        super();
040        setHeaderName(DELEGATE_HEADER);
041        setSeparator(SEP);
042    }
043
044    /**
045     * @param credentials from which the principal header is extracted
046     * @return null if no delegate found, and the delegate if one found
047     * @throws RepositoryConfigurationException if more than one delegate found
048     */
049    public Principal getDelegate(final Credentials credentials) {
050        final Set<Principal> principals = getPrincipals(credentials);
051        // No delegate
052        if (principals.size() == 0) {
053            return null;
054        }
055
056        // One delegate
057        if (principals.size() == 1) {
058            return principals.iterator().next();
059        }
060
061        throw new RepositoryConfigurationException("Too many delegates! " + principals);
062    }
063
064}