001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.auth.common;
019
020import org.fcrepo.kernel.api.exception.RepositoryConfigurationException;
021
022import javax.jcr.Credentials;
023import java.security.Principal;
024import java.util.Set;
025
026/**
027 * An example principal provider that extracts principals from request headers.
028 *
029 * @author awoods
030 * @since 2015-10-31
031 */
032public class DelegateHeaderPrincipalProvider extends HttpHeaderPrincipalProvider {
033
034    private static final String SEP = "no-separator";
035    protected static final String DELEGATE_HEADER = "On-Behalf-Of";
036
037    /**
038     * Default Constructor
039     */
040    public DelegateHeaderPrincipalProvider() {
041        super();
042        setHeaderName(DELEGATE_HEADER);
043        setSeparator(SEP);
044    }
045
046    /**
047     * @param credentials from which the principal header is extracted
048     * @return null if no delegate found, and the delegate if one found
049     * @throws RepositoryConfigurationException if more than one delegate found
050     */
051    public Principal getDelegate(final Credentials credentials) {
052        final Set<Principal> principals = getPrincipals(credentials);
053        // No delegate
054        if (principals.size() == 0) {
055            return null;
056        }
057
058        // One delegate
059        if (principals.size() == 1) {
060            return principals.iterator().next();
061        }
062
063        throw new RepositoryConfigurationException("Too many delegates! " + principals);
064    }
065
066}