001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.security.impl;
018:
019: import java.security.Principal;
020: import java.sql.Date;
021: import java.util.ArrayList;
022: import java.util.HashSet;
023: import java.util.LinkedList;
024: import java.util.List;
025: import java.util.Set;
026:
027: import org.apache.jetspeed.security.AuthenticationProvider;
028: import org.apache.jetspeed.security.AuthenticationProviderProxy;
029: import org.apache.jetspeed.security.SecurityException;
030: import org.apache.jetspeed.security.UserPrincipal;
031:
032: /**
033: * @see org.apache.jetspeed.security.AuthenticationProviderProxy
034: *
035: * @author <a href="mailto:dlestrat@apache.org">David Le Strat </a>
036: */
037: public class AuthenticationProviderProxyImpl implements
038: AuthenticationProviderProxy {
039:
040: /** The list of {@link AuthenticationProvider}. */
041: private List authenticationProviders = new ArrayList();
042:
043: /** The default authentication provider name. */
044: private String defaultAuthenticationProvider = null;
045:
046: /**
047: * <p>
048: * Constructor given a list of {@link AuthenticationProvider}.
049: * </p>
050: *
051: * @param authenticationProviders The list of {@link AuthenticationProvider}.
052: * @param defaultAuthenticationProvider The default authentication provider name.
053: */
054: public AuthenticationProviderProxyImpl(
055: List authenticationProviders,
056: String defaultAuthenticationProvider) {
057: this .authenticationProviders = authenticationProviders;
058: this .defaultAuthenticationProvider = defaultAuthenticationProvider;
059: }
060:
061: protected AuthenticationProvider getAuthenticationProviderByName(
062: String providerName) {
063: AuthenticationProvider provider = null;
064:
065: for (int i = 0; i < authenticationProviders.size(); i++) {
066: provider = (AuthenticationProvider) authenticationProviders
067: .get(i);
068: if (providerName.equals(provider.getProviderName())) {
069: break;
070: } else {
071: provider = null;
072: }
073: }
074: return provider;
075: }
076:
077: /**
078: * @see org.apache.jetspeed.security.AuthenticationProviderProxy#getDefaultAuthenticationProvider()
079: */
080: public String getDefaultAuthenticationProvider() {
081: return this .defaultAuthenticationProvider;
082: }
083:
084: /**
085: * @see org.apache.jetspeed.security.AuthenticationProviderProxy#getAuthenticationProvider(java.lang.String)
086: */
087: public String getAuthenticationProvider(String userName) {
088: AuthenticationProvider authenticationProvider;
089: String providerName = null;
090:
091: for (int i = 0; i < authenticationProviders.size(); i++) {
092: authenticationProvider = (AuthenticationProvider) authenticationProviders
093: .get(i);
094: if (authenticationProvider.getUserSecurityHandler()
095: .isUserPrincipal(userName)) {
096: providerName = authenticationProvider.getProviderName();
097: break;
098: }
099: }
100: return providerName;
101: }
102:
103: /**
104: * @see org.apache.jetspeed.security.spi.UserSecurityHandler#isUserPrincipal(java.lang.String)
105: */
106: public boolean isUserPrincipal(String userName) {
107: boolean exists = false;
108:
109: for (int i = 0; i < authenticationProviders.size(); i++) {
110: exists = ((AuthenticationProvider) authenticationProviders
111: .get(i)).getUserSecurityHandler().isUserPrincipal(
112: userName);
113: if (exists) {
114: break;
115: }
116: }
117: return exists;
118: }
119:
120: /**
121: * @see org.apache.jetspeed.security.spi.UserSecurityHandler#getUserPrincipal(java.lang.String)
122: */
123: public Principal getUserPrincipal(String username) {
124: Principal userPrincipal = null;
125: for (int i = 0; i < authenticationProviders.size(); i++) {
126: userPrincipal = ((AuthenticationProvider) authenticationProviders
127: .get(i)).getUserSecurityHandler().getUserPrincipal(
128: username);
129: if (null != userPrincipal) {
130: break;
131: }
132: }
133: return userPrincipal;
134: }
135:
136: /**
137: * @see org.apache.jetspeed.security.spi.UserSecurityHandler#getUserPrincipals(java.lang.String)
138: */
139: public List getUserPrincipals(String filter) {
140: List userPrincipals = new LinkedList();
141: for (int i = 0; i < authenticationProviders.size(); i++) {
142: userPrincipals
143: .addAll(((AuthenticationProvider) authenticationProviders
144: .get(i)).getUserSecurityHandler()
145: .getUserPrincipals(filter));
146: }
147: return userPrincipals;
148: }
149:
150: /**
151: * @see org.apache.jetspeed.security.AuthenticationProviderProxy#addUserPrincipal(org.apache.jetspeed.security.UserPrincipal,
152: * java.lang.String)
153: */
154: public void addUserPrincipal(UserPrincipal userPrincipal,
155: String authenticationProvider) throws SecurityException {
156: AuthenticationProvider provider = getAuthenticationProviderByName(authenticationProvider);
157: if (provider != null) {
158: provider.getUserSecurityHandler().addUserPrincipal(
159: userPrincipal);
160: } else {
161: throw new SecurityException(
162: SecurityException.INVALID_AUTHENTICATION_PROVIDER
163: .create(authenticationProvider));
164: }
165: }
166:
167: /**
168: * @see org.apache.jetspeed.security.spi.UserSecurityHandler#addUserPrincipal(org.apache.jetspeed.security.UserPrincipal)
169: */
170: public void addUserPrincipal(UserPrincipal userPrincipal)
171: throws SecurityException {
172: String providerName = getAuthenticationProvider(userPrincipal
173: .getName());
174: if (providerName == null) {
175: addUserPrincipal(userPrincipal,
176: defaultAuthenticationProvider);
177: } else {
178: addUserPrincipal(userPrincipal, providerName);
179: }
180: }
181:
182: /**
183: * @see org.apache.jetspeed.security.AuthenticationProviderProxy#updateUserPrincipal(org.apache.jetspeed.security.UserPrincipal,
184: * java.lang.String)
185: */
186: public void updateUserPrincipal(UserPrincipal userPrincipal,
187: String authenticationProvider) throws SecurityException {
188: AuthenticationProvider provider = getAuthenticationProviderByName(authenticationProvider);
189: if (provider != null) {
190: provider.getUserSecurityHandler().updateUserPrincipal(
191: userPrincipal);
192: } else {
193: throw new SecurityException(
194: SecurityException.INVALID_AUTHENTICATION_PROVIDER
195: .create(authenticationProvider));
196: }
197: }
198:
199: /**
200: * @see org.apache.jetspeed.security.spi.UserSecurityHandler#updateUserPrincipal(org.apache.jetspeed.security.UserPrincipal)
201: */
202: public void updateUserPrincipal(UserPrincipal userPrincipal)
203: throws SecurityException {
204: String providerName = getAuthenticationProvider(userPrincipal
205: .getName());
206: if (providerName != null) {
207: updateUserPrincipal(userPrincipal, providerName);
208: } else {
209: throw new SecurityException(
210: SecurityException.USER_DOES_NOT_EXIST
211: .create(userPrincipal.getName()));
212: }
213: }
214:
215: /**
216: * @see org.apache.jetspeed.security.AuthenticationProviderProxy#removeUserPrincipal(org.apache.jetspeed.security.UserPrincipal,
217: * java.lang.String)
218: */
219: public void removeUserPrincipal(UserPrincipal userPrincipal,
220: String authenticationProvider) throws SecurityException {
221: AuthenticationProvider provider = getAuthenticationProviderByName(authenticationProvider);
222: if (provider != null) {
223: provider.getUserSecurityHandler().removeUserPrincipal(
224: userPrincipal);
225: } else {
226: throw new SecurityException(
227: SecurityException.INVALID_AUTHENTICATION_PROVIDER
228: .create(authenticationProvider));
229: }
230: }
231:
232: /**
233: * @see org.apache.jetspeed.security.spi.UserSecurityHandler#removeUserPrincipal(org.apache.jetspeed.security.UserPrincipal)
234: */
235: public void removeUserPrincipal(UserPrincipal userPrincipal)
236: throws SecurityException {
237: String providerName = getAuthenticationProvider(userPrincipal
238: .getName());
239: if (providerName != null) {
240: removeUserPrincipal(userPrincipal, providerName);
241: }
242: }
243:
244: /**
245: * @see org.apache.jetspeed.security.spi.CredentialHandler#getPublicCredentials(java.lang.String)
246: */
247: public Set getPublicCredentials(String username) {
248: Set publicCredentials = new HashSet();
249: String providerName = getAuthenticationProvider(username);
250: if (providerName != null) {
251: AuthenticationProvider provider = getAuthenticationProviderByName(providerName);
252: publicCredentials.addAll(provider.getCredentialHandler()
253: .getPublicCredentials(username));
254: }
255: return publicCredentials;
256: }
257:
258: /**
259: * @see org.apache.jetspeed.security.AuthenticationProviderProxy#setPassword(String, String, String, String)
260: */
261: public void setPassword(String userName, String oldPassword,
262: String newPassword, String authenticationProvider)
263: throws SecurityException {
264: AuthenticationProvider provider = getAuthenticationProviderByName(authenticationProvider);
265: if (provider != null) {
266: provider.getCredentialHandler().setPassword(userName,
267: oldPassword, newPassword);
268: } else {
269: throw new SecurityException(
270: SecurityException.INVALID_AUTHENTICATION_PROVIDER
271: .create(authenticationProvider));
272: }
273: }
274:
275: /**
276: * @see org.apache.jetspeed.security.spi.CredentialHandler#setPassword(java.lang.String,java.lang.String,java.lang.String)
277: */
278: public void setPassword(String userName, String oldPassword,
279: String newPassword) throws SecurityException {
280: String providerName = getAuthenticationProvider(userName);
281: if (providerName != null) {
282: setPassword(userName, oldPassword, newPassword,
283: providerName);
284: } else {
285: throw new SecurityException(
286: SecurityException.USER_DOES_NOT_EXIST
287: .create(userName));
288: }
289: }
290:
291: /**
292: * @see org.apache.jetspeed.security.AuthenticationProviderProxy#importPassword(String, String, String, String)
293: */
294: public void importPassword(String userName, String newPassword,
295: String authenticationProvider) throws SecurityException {
296: AuthenticationProvider provider = getAuthenticationProviderByName(authenticationProvider);
297: if (provider != null) {
298: provider.getCredentialHandler().importPassword(userName,
299: newPassword);
300: } else {
301: throw new SecurityException(
302: SecurityException.INVALID_AUTHENTICATION_PROVIDER
303: .create(authenticationProvider));
304: }
305: }
306:
307: /**
308: * @see org.apache.jetspeed.security.spi.CredentialHandler#importPassword(java.lang.String,java.lang.String,java.lang.String)
309: */
310: public void importPassword(String userName, String newPassword)
311: throws SecurityException {
312: String providerName = getAuthenticationProvider(userName);
313: if (providerName != null) {
314: importPassword(userName, newPassword, providerName);
315: } else {
316: throw new SecurityException(
317: SecurityException.USER_DOES_NOT_EXIST
318: .create(userName));
319: }
320: }
321:
322: /**
323: * @see org.apache.jetspeed.security.spi.CredentialHandler#getPrivateCredentials(java.lang.String)
324: */
325: public Set getPrivateCredentials(String username) {
326: Set privateCredentials = new HashSet();
327: String providerName = getAuthenticationProvider(username);
328: if (providerName != null) {
329: AuthenticationProvider provider = getAuthenticationProviderByName(providerName);
330: privateCredentials.addAll(provider.getCredentialHandler()
331: .getPrivateCredentials(username));
332: }
333: return privateCredentials;
334: }
335:
336: /**
337: * @see org.apache.jetspeed.security.AuthenticationProviderProxy#setPasswordEnabled(java.lang.String, boolean, java.lang.String)
338: */
339: public void setPasswordEnabled(String userName, boolean enabled,
340: String authenticationProvider) throws SecurityException {
341: AuthenticationProvider provider = getAuthenticationProviderByName(authenticationProvider);
342: if (provider != null) {
343: provider.getCredentialHandler().setPasswordEnabled(
344: userName, enabled);
345: } else {
346: throw new SecurityException(
347: SecurityException.INVALID_AUTHENTICATION_PROVIDER
348: .create(authenticationProvider));
349: }
350: }
351:
352: /**
353: * @see org.apache.jetspeed.security.spi.CredentialHandler#setPasswordEnabled(java.lang.String, boolean)
354: */
355: public void setPasswordEnabled(String userName, boolean enabled)
356: throws SecurityException {
357: String providerName = getAuthenticationProvider(userName);
358: if (providerName != null) {
359: setPasswordEnabled(userName, enabled, providerName);
360: } else {
361: throw new SecurityException(
362: SecurityException.USER_DOES_NOT_EXIST
363: .create(userName));
364: }
365: }
366:
367: /**
368: * @see org.apache.jetspeed.security.AuthenticationProviderProxy#setPasswordUpdateRequired(java.lang.String, boolean, java.lang.String)
369: */
370: public void setPasswordUpdateRequired(String userName,
371: boolean updateRequired, String authenticationProvider)
372: throws SecurityException {
373: AuthenticationProvider provider = getAuthenticationProviderByName(authenticationProvider);
374: if (provider != null) {
375: provider.getCredentialHandler().setPasswordUpdateRequired(
376: userName, updateRequired);
377: } else {
378: throw new SecurityException(
379: SecurityException.INVALID_AUTHENTICATION_PROVIDER
380: .create(authenticationProvider));
381: }
382: }
383:
384: /**
385: * @see org.apache.jetspeed.security.spi.CredentialHandler#setPasswordUpdateRequired(java.lang.String, boolean)
386: */
387: public void setPasswordUpdateRequired(String userName,
388: boolean updateRequired) throws SecurityException {
389: String providerName = getAuthenticationProvider(userName);
390: if (providerName != null) {
391: setPasswordUpdateRequired(userName, updateRequired,
392: providerName);
393: } else {
394: throw new SecurityException(
395: SecurityException.USER_DOES_NOT_EXIST
396: .create(userName));
397: }
398: }
399:
400: /**
401: * @see org.apache.jetspeed.security.AuthenticationProviderProxy#setPasswordExpiration(java.lang.String, java.sql.Date, java.lang.String)
402: */
403: public void setPasswordExpiration(String userName,
404: Date expirationDate, String authenticationProvider)
405: throws SecurityException {
406: AuthenticationProvider provider = getAuthenticationProviderByName(authenticationProvider);
407: if (provider != null) {
408: provider.getCredentialHandler().setPasswordExpiration(
409: userName, expirationDate);
410: } else {
411: throw new SecurityException(
412: SecurityException.INVALID_AUTHENTICATION_PROVIDER
413: .create(authenticationProvider));
414: }
415: }
416:
417: /**
418: * @see org.apache.jetspeed.security.spi.CredentialHandler#setPasswordExpiration(java.lang.String, java.sql.Date)
419: */
420: public void setPasswordExpiration(String userName,
421: Date expirationDate) throws SecurityException {
422: String providerName = getAuthenticationProvider(userName);
423: if (providerName != null) {
424: setPasswordExpiration(userName, expirationDate,
425: providerName);
426: } else {
427: throw new SecurityException(
428: SecurityException.USER_DOES_NOT_EXIST
429: .create(userName));
430: }
431: }
432:
433: /**
434: * @see org.apache.jetspeed.security.AuthenticationProviderProxy#authenticate(String, String, String)
435: */
436: public boolean authenticate(String userName, String password,
437: String authenticationProvider) throws SecurityException {
438: AuthenticationProvider provider = getAuthenticationProviderByName(authenticationProvider);
439: if (provider != null) {
440: return provider.getCredentialHandler().authenticate(
441: userName, password);
442: } else {
443: throw new SecurityException(
444: SecurityException.INVALID_AUTHENTICATION_PROVIDER
445: .create(authenticationProvider));
446: }
447: }
448:
449: /**
450: * @see org.apache.jetspeed.security.spi.CredentialHandler#authenticate(java.lang.String, java.lang.String)
451: */
452: public boolean authenticate(String userName, String password)
453: throws SecurityException {
454: String providerName = getAuthenticationProvider(userName);
455: if (providerName != null) {
456: return authenticate(userName, password, providerName);
457: } else {
458: throw new SecurityException(
459: SecurityException.USER_DOES_NOT_EXIST
460: .create(userName));
461: }
462: }
463: }
|