001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/user/tags/sakai_2-4-1/user-impl/impl/src/java/org/sakaiproject/user/impl/UserAuthnComponent.java $
003: * $Id: UserAuthnComponent.java 9195 2006-05-09 19:52:44Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.user.impl;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.sakaiproject.user.api.Authentication;
025: import org.sakaiproject.user.api.AuthenticationException;
026: import org.sakaiproject.user.api.AuthenticationManager;
027: import org.sakaiproject.user.api.AuthenticationUnknownException;
028: import org.sakaiproject.user.api.Evidence;
029: import org.sakaiproject.user.api.ExternalTrustedEvidence;
030: import org.sakaiproject.user.api.IdPwEvidence;
031: import org.sakaiproject.user.api.User;
032: import org.sakaiproject.user.api.UserDirectoryService;
033: import org.sakaiproject.user.api.UserNotDefinedException;
034:
035: /**
036: * <p>
037: * An Authentication component working with the UserDirectoryService.
038: * </p>
039: */
040: public abstract class UserAuthnComponent implements
041: AuthenticationManager {
042: /** Our log (commons). */
043: private static Log M_log = LogFactory
044: .getLog(UserAuthnComponent.class);
045:
046: /**********************************************************************************************************************************************************************************************************************************************************
047: * Dependencies
048: *********************************************************************************************************************************************************************************************************************************************************/
049:
050: /**
051: * @return the UserDirectoryService collaborator.
052: */
053: protected abstract UserDirectoryService userDirectoryService();
054:
055: /**********************************************************************************************************************************************************************************************************************************************************
056: * Init and Destroy
057: *********************************************************************************************************************************************************************************************************************************************************/
058:
059: /**
060: * Final initialization, once all dependencies are set.
061: */
062: public void init() {
063: M_log.info("init()");
064: }
065:
066: /**
067: * Final cleanup.
068: */
069: public void destroy() {
070: M_log.info("destroy()");
071: }
072:
073: /**********************************************************************************************************************************************************************************************************************************************************
074: * Work interface methods: AuthenticationManager
075: *********************************************************************************************************************************************************************************************************************************************************/
076:
077: /**
078: * @inheritDoc
079: */
080: public Authentication authenticate(Evidence e)
081: throws AuthenticationException {
082: if (e instanceof IdPwEvidence) {
083: IdPwEvidence evidence = (IdPwEvidence) e;
084:
085: // reject null or blank
086: if ((evidence.getPassword() == null)
087: || (evidence.getPassword().trim().length() == 0)
088: || (evidence.getIdentifier() == null)
089: || (evidence.getIdentifier().trim().length() == 0)) {
090: throw new AuthenticationException("invalid login");
091: }
092:
093: // the evidence id must match a defined User
094: User user = userDirectoryService().authenticate(
095: evidence.getIdentifier(), evidence.getPassword());
096: if (user == null) {
097: throw new AuthenticationException("invalid login");
098: }
099:
100: Authentication rv = new org.sakaiproject.util.Authentication(
101: user.getId(), user.getEid());
102: return rv;
103: }
104:
105: else if (e instanceof ExternalTrustedEvidence) {
106: ExternalTrustedEvidence evidence = (ExternalTrustedEvidence) e;
107:
108: // reject null or blank
109: if ((evidence.getIdentifier() == null)
110: || (evidence.getIdentifier().trim().length() == 0)) {
111: throw new AuthenticationException("invalid login");
112: }
113:
114: // accept, so now lookup the user in our database.
115: try {
116: User user = userDirectoryService().getUserByEid(
117: evidence.getIdentifier());
118:
119: Authentication rv = new org.sakaiproject.util.Authentication(
120: user.getId(), user.getEid());
121: return rv;
122: } catch (UserNotDefinedException ex) {
123: // reject if the user is not defined
124: // TODO: create the user record here?
125: throw new AuthenticationException("invalid login");
126: }
127: }
128:
129: else {
130: throw new AuthenticationUnknownException(e.toString());
131: }
132: }
133: }
|