001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.web.servlets;
034:
035: import edu.yale.its.tp.cas.client.filter.CASFilter;
036:
037: import org.libresource.Libresource;
038:
039: import org.libresource.kernel.KernelConstants;
040: import org.libresource.kernel.interfaces.KernelService;
041:
042: import org.libresource.membership.MembershipConstants;
043: import org.libresource.membership.interfaces.MembershipService;
044:
045: import java.net.URI;
046:
047: import java.util.HashMap;
048:
049: import javax.security.auth.callback.CallbackHandler;
050: import javax.security.auth.login.LoginContext;
051:
052: import javax.servlet.http.HttpServletRequest;
053: import javax.servlet.http.HttpServletResponse;
054:
055: public class CASLogin extends BaseServlet {
056: protected void process(HttpServletRequest request,
057: HttpServletResponse response) throws Exception {
058: KernelService kernelService = (KernelService) Libresource
059: .getService(KernelConstants.SERVICE);
060: MembershipService membershipService = (MembershipService) Libresource
061: .getService(MembershipConstants.SERVICE);
062:
063: // get user from cas
064: String username = (String) request.getSession().getAttribute(
065: CASFilter.CAS_FILTER_USER);
066:
067: // login to libresource
068: CallbackHandler handler = org.libresource.Libresource
069: .buildCallBackHandler(username, "null");
070: LoginContext loginContext = new LoginContext("libresource",
071: handler);
072: loginContext.login();
073: kernelService.getConnectedResource();
074:
075: // check if first login
076: try {
077: membershipService.getProfile();
078: } catch (Exception e) {
079: // create a default profile
080: kernelService.systemCreateURI(new URI(membershipService
081: .getUsersRootURI()
082: + "/" + username));
083: membershipService.systemCreateProfile(username, username,
084: "", "", new HashMap());
085: }
086:
087: // init session
088: loginContext.logout();
089: request.getSession().invalidate();
090: request.getSession().setAttribute(CASFilter.CAS_FILTER_USER,
091: username);
092: request.getSession()
093: .setAttribute("security_username", username);
094:
095: String redirect = request.getParameter("loginRedirectURL");
096:
097: if (redirect != null) {
098: response.sendRedirect(redirect);
099:
100: return;
101: }
102:
103: response.sendRedirect(request.getContextPath());
104: }
105: }
|