Moved name qualifer checking from the AA servlet into the NameIdentifierMapping....
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / hs / provider / SharedMemoryShibHandle.java
1 /*
2  * The Shibboleth License, Version 1. Copyright (c) 2002 University Corporation for Advanced Internet Development, Inc.
3  * All rights reserved Redistribution and use in source and binary forms, with or without modification, are permitted
4  * provided that the following conditions are met: Redistributions of source code must retain the above copyright
5  * notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the
6  * above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other
7  * materials provided with the distribution, if any, must include the following acknowledgment: "This product includes
8  * software developed by the University Corporation for Advanced Internet Development <http://www.ucaid.edu> Internet2
9  * Project. Alternately, this acknowledegement may appear in the software itself, if and wherever such third-party
10  * acknowledgments normally appear. Neither the name of Shibboleth nor the names of its contributors, nor Internet2,
11  * nor the University Corporation for Advanced Internet Development, Inc., nor UCAID may be used to endorse or promote
12  * products derived from this software without specific prior written permission. For written permission, please
13  * contact shibboleth@shibboleth.org Products derived from this software may not be called Shibboleth, Internet2,
14  * UCAID, or the University Corporation for Advanced Internet Development, nor may Shibboleth appear in their name,
15  * without prior written permission of the University Corporation for Advanced Internet Development. THIS SOFTWARE IS
16  * PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES,
17  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
18  * NON-INFRINGEMENT ARE DISCLAIMED AND THE ENTIRE RISK OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS
19  * WITH LICENSEE. IN NO EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY CORPORATION FOR ADVANCED
20  * INTERNET DEVELOPMENT, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
23  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 package edu.internet2.middleware.shibboleth.hs.provider;
28
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.Iterator;
32 import java.util.Map;
33 import java.util.Set;
34 import java.util.Map.Entry;
35
36 import org.apache.log4j.Logger;
37 import org.doomdark.uuid.UUIDGenerator;
38 import org.opensaml.SAMLException;
39 import org.opensaml.SAMLNameIdentifier;
40 import org.w3c.dom.Element;
41
42 import edu.internet2.middleware.shibboleth.common.AuthNPrincipal;
43 import edu.internet2.middleware.shibboleth.common.IdentityProvider;
44 import edu.internet2.middleware.shibboleth.common.InvalidNameIdentifierException;
45 import edu.internet2.middleware.shibboleth.common.NameIdentifierMappingException;
46 import edu.internet2.middleware.shibboleth.common.ServiceProvider;
47 import edu.internet2.middleware.shibboleth.hs.HSNameIdentifierMapping;
48
49 /**
50  * {@link HSNameIdentifierMapping}implementation that uses an in-memory cache to store mappings between principal
51  * names and Shibboleth Attribute Query Handles.
52  * 
53  * @author Walter Hoehn
54  */
55 public class SharedMemoryShibHandle extends AQHNameIdentifierMapping implements HSNameIdentifierMapping {
56
57         protected HandleCache   cache   = HandleCache.instance();
58         private static Logger   log             = Logger.getLogger(SharedMemoryShibHandle.class.getName());
59
60         public SharedMemoryShibHandle(Element config) throws NameIdentifierMappingException {
61                 super(config);
62         }
63
64         public SAMLNameIdentifier getNameIdentifierName(AuthNPrincipal principal, ServiceProvider sProv,
65                         IdentityProvider idProv) throws NameIdentifierMappingException {
66
67                 if (principal == null) {
68                         log.error("A principal must be supplied for Attribute Query Handle creation.");
69                         throw new IllegalArgumentException("A principal must be supplied for Attribute Query Handle creation.");
70                 }
71
72                 String handle = UUIDGenerator.getInstance().generateRandomBasedUUID().toString();
73                 log.debug("Assigning handle (" + handle + ") to principal (" + principal.getName() + ").");
74                 synchronized (cache.handleEntries) {
75                         cache.handleEntries.put(handle, createHandleEntry(principal));
76                 }
77
78                 try {
79                         return new SAMLNameIdentifier(handle, idProv.getProviderId(), getNameIdentifierFormat().toString());
80                 } catch (SAMLException e) {
81                         throw new NameIdentifierMappingException("Unable to generate Attribute Query Handle: " + e);
82                 }
83
84         }
85
86         public AuthNPrincipal getPrincipal(SAMLNameIdentifier nameId, ServiceProvider sProv, IdentityProvider idProv)
87                         throws NameIdentifierMappingException, InvalidNameIdentifierException {
88                 
89                 verifyQualifier(nameId, idProv);
90
91                 synchronized (cache.handleEntries) {
92                         if (!cache.handleEntries.containsKey(nameId.getName())) {
93                                 log.debug("The Name Mapping Cache does not contain an entry for this Attribute Query Handle.");
94                                 throw new InvalidNameIdentifierException(
95                                                 "The Name Mapping Cache does not contain an entry for this Attribute Query Handle.", errorCodes);
96                         }
97                 }
98
99                 HandleEntry handleEntry;
100                 synchronized (cache.handleEntries) {
101                         handleEntry = (HandleEntry) cache.handleEntries.get(nameId.getName());
102                 }
103
104                 if (handleEntry.isExpired()) {
105                         log.debug("Attribute Query Handle is expired.");
106                         synchronized (cache.handleEntries) {
107                                 cache.handleEntries.remove(nameId.getName());
108                         }
109                         throw new InvalidNameIdentifierException("Attribute Query Handle is expired.", errorCodes);
110                 } else {
111                         log.debug("Attribute Query Handle recognized.");
112                         return handleEntry.principal;
113                 }
114         }
115
116         public void destroy() {
117                 cache.destroy();
118         }
119
120 }
121
122 class HandleCache {
123
124         protected Map                                           handleEntries   = new HashMap();
125         private static HandleCache                      instance;
126         protected MemoryRepositoryCleaner       cleaner                 = new MemoryRepositoryCleaner();
127         private static Logger                           log                             = Logger.getLogger(HandleCache.class.getName());
128
129         protected HandleCache() {}
130
131         public static synchronized HandleCache instance() {
132                 if (instance == null) {
133                         instance = new HandleCache();
134                         return instance;
135                 }
136                 return instance;
137         }
138
139         protected void finalize() throws Throwable {
140                 super.finalize();
141                 destroy();
142         }
143
144         protected void destroy() {
145                 synchronized (cleaner) {
146                         if (cleaner != null) {
147                                 cleaner.shutdown = true;
148                                 cleaner.interrupt();
149                         }
150                 }
151         }
152
153         private class MemoryRepositoryCleaner extends Thread {
154
155                 private boolean shutdown        = false;
156                 private Thread  master;
157
158                 public MemoryRepositoryCleaner() {
159                         super(
160                                         "edu.internet2.middleware.shibboleth.hs.provider.SharedMemoryShibHandle.HandleCache.MemoryRepositoryCleaner");
161                         this.master = Thread.currentThread();
162                         setDaemon(true);
163                         if (getPriority() > Thread.MIN_PRIORITY) {
164                                 setPriority(getPriority() - 1);
165                         }
166                         log.debug("Starting memory-based shib handle cache cleanup thread.");
167                         start();
168                 }
169
170                 public void run() {
171                         try {
172                                 sleep(60 * 1000); //one minute
173                         } catch (InterruptedException e) {
174                                 log.debug("Memory-based shib handle cache cleanup interrupted.");
175                         }
176                         while (true) {
177                                 try {
178                                         if (!master.isAlive()) {
179                                                 shutdown = true;
180                                                 log.debug("Memory-based shib handle cache cleaner is orphaned.");
181                                         }
182                                         if (shutdown) {
183                                                 log.debug("Stopping Memory-based shib handle cache cleanup thread.");
184                                                 return;
185                                         }
186                                         log.debug("Memory cache handle cache cleanup thread searching for stale entries.");
187                                         Set needsDeleting = new HashSet();
188                                         synchronized (handleEntries) {
189                                                 Iterator iterator = handleEntries.entrySet().iterator();
190                                                 while (iterator.hasNext()) {
191                                                         Entry entry = (Entry) iterator.next();
192                                                         HandleEntry handleEntry = (HandleEntry) entry.getValue();
193                                                         if (handleEntry.isExpired()) {
194                                                                 needsDeleting.add(entry.getKey());
195                                                         }
196                                                 }
197                                                 //release the lock to be friendly
198                                                 Iterator deleteIterator = needsDeleting.iterator();
199                                                 while (deleteIterator.hasNext()) {
200                                                         synchronized (handleEntries) {
201                                                                 log.debug("Expiring an Attribute Query Handle from the memory cache.");
202                                                                 handleEntries.remove(deleteIterator.next());
203                                                         }
204                                                 }
205                                         }
206                                         sleep(60 * 1000); //one minute
207                                 } catch (InterruptedException e) {
208                                         log.debug("Memory-based shib handle cache cleanup interrupted.");
209                                 }
210                         }
211                 }
212         }
213
214 }