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 javax.servlet.http.HttpServletRequest; 021 022import java.security.Principal; 023import java.util.Set; 024 025import org.fcrepo.kernel.api.exception.RepositoryConfigurationException; 026 027/** 028 * An example principal provider that extracts principals from request headers. 029 * 030 * @author awoods 031 * @since 2015-10-31 032 */ 033public class DelegateHeaderPrincipalProvider extends HttpHeaderPrincipalProvider { 034 035 private static final String SEP = "no-separator"; 036 protected static final String DELEGATE_HEADER = "On-Behalf-Of"; 037 038 public static class DelegatedHeaderPrincipal implements Principal { 039 040 private final String name; 041 042 protected DelegatedHeaderPrincipal(final String name) { 043 this.name = name; 044 } 045 046 @Override 047 public String getName() { 048 return name; 049 } 050 051 @Override 052 public String toString() { 053 return name; 054 } 055 056 @Override 057 public boolean equals(final Object o) { 058 if (o instanceof DelegatedHeaderPrincipal) { 059 return ((DelegatedHeaderPrincipal) o).getName().equals( 060 this.getName()); 061 } 062 return false; 063 } 064 065 @Override 066 public int hashCode() { 067 if (name == null) { 068 return 0; 069 } 070 return name.hashCode(); 071 } 072 073 } 074 075 /** 076 * Default Constructor 077 */ 078 public DelegateHeaderPrincipalProvider() { 079 super(); 080 setHeaderName(DELEGATE_HEADER); 081 setSeparator(SEP); 082 } 083 084 /** 085 * @param request from which the principal header is extracted 086 * @return null if no delegate found, and the delegate if one found 087 * @throws RepositoryConfigurationException if more than one delegate found 088 */ 089 public Principal getDelegate(final HttpServletRequest request) { 090 final Set<Principal> principals = getPrincipals(request); 091 // No delegate 092 if (principals.size() == 0) { 093 return null; 094 } 095 096 // One delegate 097 if (principals.size() == 1) { 098 return principals.iterator().next(); 099 } 100 101 throw new RepositoryConfigurationException("Too many delegates! " + principals); 102 } 103 104 @Override 105 protected Principal createPrincipal(final String name) { 106 return new DelegatedHeaderPrincipal(name.trim()); 107 } 108 109}