001: /*
002: * (C) Copyright 2000 - 2006 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019:
020: package com.nabhinc.portal.spi.impl.jaas;
021:
022: import java.rmi.RemoteException;
023: import java.security.Principal;
024: import java.util.HashMap;
025: import java.util.Iterator;
026: import java.util.Map;
027: import java.util.Set;
028: import java.util.Vector;
029:
030: import javax.security.auth.Subject;
031: import javax.security.auth.login.LoginContext;
032: import javax.security.auth.login.LoginException;
033:
034: import com.nabhinc.portal.spi.BaseUserServiceImpl;
035: import com.nabhinc.portal.spi.UserAdminServiceLocator;
036: import com.nabhinc.spi.AuthenticationService;
037: import com.nabhinc.spi.LocalUserInfo;
038: import com.nabhinc.spi.NoSuchEntityException;
039: import com.nabhinc.util.StringUtil;
040:
041: /**
042: *
043: *
044: * @author Padmanabh Dabke
045: * (c) 2006 Nabh Information Systems, Inc. All Rights Reserved.
046: */
047: public class AuthenticationServiceJAASImpl extends BaseUserServiceImpl
048: implements AuthenticationService {
049: // private final String DEFAULT_USER_PRINCIPAL_CLASS = "com.nabhinc.spi.UserPrincipal";
050: // private final String DEFAULT_ROLE_PRINCIPAL_CLASS = "com.nabhinc.portal.spi.impl.jaas.RolePrincipal";
051:
052: private HashMap jiUserPrincipalClassMap = new HashMap();
053: private HashMap jiRolePrincipalClassMap = new HashMap();
054:
055: private String[] jiUserPrincipalClasses = null;
056: private String[] jiRolePrincipalClasses = null;
057:
058: private String jiLoginCallbackHandlerClass = null; //"com.nabhinc.portal.spi.impl.jaas.StringbeansCallbackHandlerImpl";
059: private String jiLoginAppName = null;
060:
061: private String jiUserAdminServiceURL = null;
062:
063: public String getLoginAppName() {
064: return jiLoginAppName;
065: }
066:
067: public void setLoginAppName(String jiLoginAppName) {
068: this .jiLoginAppName = jiLoginAppName;
069: }
070:
071: public String getLoginCallbackHandlerClass() {
072: return jiLoginCallbackHandlerClass;
073: }
074:
075: public void setLoginCallbackHandlerClass(
076: String jiLoginCallbackHandlerClass) {
077: this .jiLoginCallbackHandlerClass = jiLoginCallbackHandlerClass;
078: }
079:
080: public String getRolePrincipalClasses() {
081: return StringUtil.join(jiRolePrincipalClasses, ",");
082: }
083:
084: @SuppressWarnings("unchecked")
085: public void setRolePrincipalClasses(String cl) {
086: this .jiRolePrincipalClasses = StringUtil.split(cl, ",");
087: if (jiRolePrincipalClasses == null)
088: return;
089: for (int i = 0; i < jiRolePrincipalClasses.length; i++) {
090: jiRolePrincipalClassMap.put(jiRolePrincipalClasses[i], "");
091: }
092: }
093:
094: public String getUserAdminServiceURL() {
095: return jiUserAdminServiceURL;
096: }
097:
098: public void setUserAdminServiceURL(String jiUserAdminServiceURL) {
099: this .jiUserAdminServiceURL = jiUserAdminServiceURL;
100: }
101:
102: public String getUserPrincipalClasses() {
103: return StringUtil.join(jiUserPrincipalClasses, ",");
104: }
105:
106: @SuppressWarnings("unchecked")
107: public void setUserPrincipalClasses(String cl) {
108: this .jiUserPrincipalClasses = StringUtil.split(cl, ",");
109: if (jiUserPrincipalClasses == null)
110: return;
111: for (int i = 0; i < jiUserPrincipalClasses.length; i++) {
112: jiUserPrincipalClassMap.put(jiUserPrincipalClasses[i], "");
113: }
114: }
115:
116: @SuppressWarnings("unchecked")
117: public LocalUserInfo authenticateUserByName(String userName,
118: String password, Map paramMap) throws LoginException,
119: RemoteException {
120: StringbeansCallbackHandler callbackHandler = null;
121: if (jiLoginCallbackHandlerClass != null) {
122:
123: try {
124: callbackHandler = (StringbeansCallbackHandler) Class
125: .forName(jiLoginCallbackHandlerClass)
126: .newInstance();
127: } catch (Exception ex) {
128: throw new RemoteException(
129: "Failed to create instance of callback handler class.",
130: ex);
131: }
132: }
133:
134: LoginContext loginContext = new LoginContext(jiLoginAppName,
135: new LoginCallbackHandler(callbackHandler, userName,
136: password, paramMap));
137: loginContext.login();
138:
139: Subject subject = loginContext.getSubject();
140: Set principals = subject.getPrincipals();
141:
142: Iterator iter = principals.iterator();
143: Vector roleVec = new Vector(5);
144: LocalUserInfo userInfo = new LocalUserInfo();
145: while (iter.hasNext()) {
146: Principal p = (Principal) iter.next();
147: String pClass = p.getClass().getName();
148: if (userInfo.userName == null
149: && isUserPrincipalClass(pClass)) {
150: userInfo.userName = p.getName();
151: userInfo.principal = p;
152: } else {
153: if (isRolePrincipalClass(pClass)) {
154: roleVec.addElement(p.getName());
155: }
156: }
157: }
158:
159: String[] roles = new String[roleVec.size()];
160: roleVec.copyInto(roles);
161: userInfo.roles = roles;
162:
163: return userInfo;
164: }
165:
166: public LocalUserInfo authenticateUserByEmail(String userEmail,
167: String password, Map paramMap) throws LoginException,
168: RemoteException {
169: String userName;
170: try {
171: userName = UserAdminServiceLocator.getUserAdminService(
172: jiUserAdminServiceURL).getUserNameFromEmail(
173: userEmail);
174: } catch (NoSuchEntityException e) {
175: throw new RemoteException("Invalid email.", e);
176: }
177: return authenticateUserByName(userName, password, paramMap);
178: }
179:
180: private boolean isUserPrincipalClass(String pClass) {
181: return jiUserPrincipalClassMap.get(pClass) != null;
182: }
183:
184: private boolean isRolePrincipalClass(String pClass) {
185: return jiRolePrincipalClassMap.get(pClass) != null;
186: }
187:
188: }
|