1 40 package org.dspace.eperson; 41 42 import java.io.IOException ; 43 44 import javax.servlet.ServletException ; 45 import javax.servlet.http.HttpServletRequest ; 46 import javax.servlet.http.HttpServletResponse ; 47 import javax.servlet.http.HttpSession ; 48 import java.sql.SQLException ; 49 import java.util.ArrayList ; 50 import java.util.Arrays ; 51 import java.util.List ; 52 import java.util.Iterator ; 53 import java.util.StringTokenizer ; 54 55 import org.apache.log4j.Logger; 56 import org.dspace.core.ConfigurationManager; 57 import org.dspace.core.Context; 58 import org.dspace.core.PluginManager; 59 import org.dspace.core.LogManager; 60 import org.dspace.eperson.EPerson; 61 import org.dspace.eperson.AuthenticationMethod; 62 63 90 public class AuthenticationManager 91 { 92 93 private static Logger log = Logger.getLogger(AuthenticationManager.class); 94 95 96 private static AuthenticationMethod methodStack[] = 97 (AuthenticationMethod[])PluginManager.getPluginSequence(AuthenticationMethod.class); 98 99 131 public static int authenticate(Context context, 132 String username, 133 String password, 134 String realm, 135 HttpServletRequest request) 136 { 137 return authenticateInternal(context, username, password, realm, 138 request, false); 139 } 140 141 172 public static int authenticateImplicit(Context context, 173 String username, 174 String password, 175 String realm, 176 HttpServletRequest request) 177 { 178 return authenticateInternal(context, username, password, realm, 179 request, true); 180 } 181 182 private static int authenticateInternal(Context context, 183 String username, 184 String password, 185 String realm, 186 HttpServletRequest request, 187 boolean implicitOnly) 188 { 189 int bestRet = AuthenticationMethod.BAD_ARGS; 191 192 for (int i = 0; i < methodStack.length; ++i) 194 { 195 if (!implicitOnly || methodStack[i].isImplicit()) 196 { 197 int ret = 0; 198 try 199 { 200 ret = methodStack[i].authenticate(context, username, password, realm, request); 201 } 202 catch (SQLException e) 203 { 204 ret = AuthenticationMethod.NO_SUCH_USER; 205 } 206 if (ret == AuthenticationMethod.SUCCESS) 207 return ret; 208 if (ret < bestRet) 209 bestRet = ret; 210 } 211 } 212 return bestRet; 213 } 214 215 228 public static boolean canSelfRegister(Context context, 229 HttpServletRequest request, 230 String username) 231 throws SQLException 232 { 233 for (int i = 0; i < methodStack.length; ++i) 234 if (methodStack[i].canSelfRegister(context, request, username)) 235 return true; 236 return false; 237 } 238 239 252 public static boolean allowSetPassword(Context context, 253 HttpServletRequest request, 254 String username) 255 throws SQLException 256 { 257 for (int i = 0; i < methodStack.length; ++i) 258 if (methodStack[i].allowSetPassword(context, request, username)) 259 return true; 260 return false; 261 } 262 263 276 public static void initEPerson(Context context, 277 HttpServletRequest request, 278 EPerson eperson) 279 throws SQLException 280 { 281 for (int i = 0; i < methodStack.length; ++i) 282 methodStack[i].initEPerson(context, request, eperson); 283 } 284 285 300 public static int[] getSpecialGroups(Context context, 301 HttpServletRequest request) 302 { 303 ArrayList gll = new ArrayList (); 304 int totalLen = 0; 305 306 for (int i = 0; i < methodStack.length; ++i) 307 { 308 int gl[] = methodStack[i].getSpecialGroups(context, request); 309 if (gl.length > 0) 310 { 311 gll.add(gl); 312 totalLen += gl.length; 313 } 314 } 315 316 if (totalLen == 0) 320 return new int[0]; 321 else if (gll.size() == 1) 322 return (int [])gll.get(0); 323 else 324 { 325 int result[] = new int[totalLen]; 328 int k = 0; 329 for (int i = 0; i < gll.size(); ++i) 330 { 331 int gl[] = (int [])gll.get(i); 332 for (int j = 0; j < gl.length; ++j) 333 result[k++] = gl[j]; 334 } 335 return result; 336 } 337 } 338 339 346 public static Iterator authenticationMethodIterator() 347 { 348 return Arrays.asList(methodStack).iterator(); 349 } 350 } 351 | Popular Tags |