1 40 package org.dspace.app.webui.util; 41 42 import java.io.IOException ; 43 import java.util.Iterator ; 44 45 import javax.servlet.ServletException ; 46 import javax.servlet.http.HttpServletRequest ; 47 import javax.servlet.http.HttpServletResponse ; 48 import javax.servlet.http.HttpSession ; 49 50 import org.apache.log4j.Logger; 51 import org.dspace.core.Context; 52 import org.dspace.core.LogManager; 53 import org.dspace.eperson.EPerson; 54 import org.dspace.eperson.AuthenticationManager; 55 import org.dspace.eperson.AuthenticationMethod; 56 57 65 public class Authenticate 66 { 67 68 private static Logger log = Logger.getLogger(Authenticate.class); 69 70 81 public static HttpServletRequest getRealRequest(HttpServletRequest request) 82 { 83 HttpSession session = request.getSession(); 84 85 if (session.getAttribute("resuming.request") != null) 86 { 87 RequestInfo requestInfo = (RequestInfo) session 89 .getAttribute("interrupted.request.info"); 90 91 HttpServletRequest actualRequest; 92 93 if (requestInfo == null) 94 { 95 actualRequest = request; 98 } 99 else 100 { 101 105 actualRequest = requestInfo.wrapRequest(request); 106 } 107 108 session.removeAttribute("resuming.request"); 110 session.removeAttribute("interrupted.request.info"); 111 session.removeAttribute("interrupted.request.url"); 112 113 return actualRequest; 115 } 116 else 117 { 118 return request; 119 } 120 } 121 122 132 public static void resumeInterruptedRequest(HttpServletRequest request, 133 HttpServletResponse response) throws IOException 134 { 135 HttpSession session = request.getSession(); 136 String originalURL = (String ) session 137 .getAttribute("interrupted.request.url"); 138 139 if (originalURL == null) 140 { 141 originalURL = request.getContextPath() + "/mydspace"; 144 } 145 else 146 { 147 session.setAttribute("resuming.request", new Boolean (true)); 150 } 151 152 response.sendRedirect(response.encodeRedirectURL(originalURL)); 154 } 155 156 175 public static boolean startAuthentication(Context context, 176 HttpServletRequest request, HttpServletResponse response) 177 throws ServletException , IOException 178 { 179 HttpSession session = request.getSession(); 180 181 188 if (AuthenticationManager.authenticateImplicit(context, null, null, 189 null, request) == AuthenticationMethod.SUCCESS) 190 { 191 loggedIn(context, request, context.getCurrentUser()); 192 log.info(LogManager.getHeader(context, "login", "type=implicit")); 193 return true; 194 } 195 else 196 { 197 response.addDateHeader("expires", 1); 200 response.addHeader("Pragma", "no-cache"); 201 response.addHeader("Cache-control", "no-store"); 202 203 RequestInfo info = new RequestInfo(request); 205 session.setAttribute("interrupted.request.info", info); 206 207 session.setAttribute("interrupted.request.url", UIUtil 209 .getOriginalURL(request)); 210 211 216 Iterator ai = AuthenticationManager.authenticationMethodIterator(); 217 AuthenticationMethod am; 218 int count = 0; 219 String url = null; 220 while (ai.hasNext()) 221 { 222 String s; 223 am = (AuthenticationMethod)ai.next(); 224 if ((s = am.loginPageURL(context, request, response)) != null) 225 { 226 url = s; 227 ++count; 228 } 229 } 230 if (count == 1) 231 response.sendRedirect(url); 232 else 233 JSPManager.showJSP(request, response, "/login/chooser.jsp"); 234 } 235 return false; 236 } 237 238 248 public static void loggedIn(Context context, HttpServletRequest request, 249 EPerson eperson) 250 { 251 HttpSession session = request.getSession(); 252 253 context.setCurrentUser(eperson); 254 255 request.setAttribute("dspace.current.user", eperson); 257 258 session.setAttribute("dspace.current.user.id", new Integer (eperson 260 .getID())); 261 262 session.setAttribute("dspace.current.remote.addr", 265 request.getRemoteAddr()); 266 267 } 268 269 277 public static void loggedOut(Context context, HttpServletRequest request) 278 { 279 HttpSession session = request.getSession(); 280 281 context.setCurrentUser(null); 282 request.removeAttribute("dspace.current.user"); 283 session.removeAttribute("dspace.current.user.id"); 284 } 285 } 286 | Popular Tags |