Project

General

Profile

1 503 bojilova
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that tracks sessions for MetaCatServlet users.
4
 *  Copyright: 2000 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Matt Jones
7
 *    Release: @release@
8
 *
9
 *   '$Author$'
10
 *     '$Date$'
11
 * '$Revision$'
12 669 jones
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 503 bojilova
 */
27
28
package edu.ucsb.nceas.metacat;
29
30
import java.net.ConnectException;
31
import javax.servlet.http.HttpSession;
32
import javax.servlet.http.HttpServletRequest;
33
34
/**
35
 * A Class that implements session tracking for MetaCatServlet users.
36
 * User's login data are stored in the session object.
37
 * User authentication is done through a dynamically determined AuthInterface.
38
 */
39
public class AuthSession {
40
41
  private String authClass = null;
42
  private HttpSession session = null;
43
  private AuthInterface authService = null;
44
  private String statusMessage = null;
45 2058 sgarg
46
  /**
47 503 bojilova
   * Construct an AuthSession
48
   */
49 509 bojilova
  public AuthSession() throws Exception {
50 503 bojilova
    // Determine our session authentication method and
51
    // create an instance of the auth class
52
    MetaCatUtil util = new MetaCatUtil();
53 728 bojilova
    this.authClass = util.getOption("authclass");
54
    this.authService = (AuthInterface)createObject(authClass);
55 503 bojilova
  }
56 2058 sgarg
57 2045 tao
  /**
58
   * Get the new session
59
   */
60
  public HttpSession getSessions()
61
  {
62
    return this.session;
63
  }
64 503 bojilova
65 2058 sgarg
  /**
66
   * determine if the credentials for this session are valid by
67 503 bojilova
   * authenticating them using the authService configured for this session.
68 509 bojilova
   *
69
   * @param request the request made from the client
70
   * @param username the username entered when login
71
   * @param password the password entered when login
72 503 bojilova
   */
73 2058 sgarg
  public boolean authenticate(HttpServletRequest request,
74
                              String username, String password)  {
75 509 bojilova
    String message = null;
76 2058 sgarg
    try {
77 503 bojilova
      if ( authService.authenticate(username, password) ) {
78 2058 sgarg
79
        // getGroups returns groupname along with their description.
80
        // hence groups[] is generated from groupsWithDescription[][]
81
        String[][] groupsWithDescription =
82
            authService.getGroups(username,password,username);
83
        String groups[] = new String[groupsWithDescription.length];
84
85
        for(int i=0; i<groupsWithDescription.length; i++){
86
          groups[i] = groupsWithDescription[i][0];
87
        }
88
89 888 berkley
        if(groups == null)
90
        {
91
          groups = new String[0];
92
        }
93 2045 tao
        this.session = createSession(request, username, password, groups);
94 1822 jones
        String sessionId = session.getId();
95 510 bojilova
        message = "Authentication successful for user: " + username;
96 1822 jones
        this.statusMessage = formatOutput("login", message, sessionId);
97 509 bojilova
        return true;
98 2058 sgarg
      } else {
99 509 bojilova
        message = "Authentication failed for user: " + username;
100 510 bojilova
        this.statusMessage = formatOutput("unauth_login", message);
101
        return false;
102 2058 sgarg
      }
103 503 bojilova
    } catch ( ConnectException ce ) {
104 675 berkley
      message = "Connection to the authentication service failed in " +
105
                "AuthSession.authenticate: " + ce.getMessage();
106 509 bojilova
    } catch ( IllegalStateException ise ) {
107
      message = ise.getMessage();
108 503 bojilova
    }
109 2058 sgarg
110 510 bojilova
    this.statusMessage = formatOutput("error_login", message);
111 509 bojilova
    return false;
112 503 bojilova
  }
113
114 509 bojilova
  /** Get new HttpSession and store username & password in it */
115 2058 sgarg
  private HttpSession createSession(HttpServletRequest request,
116 725 bojilova
                                 String username, String password,
117 2058 sgarg
                                 String[] groups)
118 725 bojilova
                      throws IllegalStateException {
119 509 bojilova
120
    // get the current session object, create one if necessary
121
    HttpSession session = request.getSession(true);
122
123
    // if it is still in use invalidate and get a new one
124
    if ( !session.isNew() ) {
125 2045 tao
      MetaCatUtil.debugMessage("in session is not new", 40);
126
      MetaCatUtil.debugMessage("the old session id is : " +
127
                                session.getId(), 30);
128
      MetaCatUtil.debugMessage("the old session username : " +
129
                                session.getAttribute("username"), 30);
130 509 bojilova
      session.invalidate();
131 2045 tao
      MetaCatUtil.debugMessage("in session is not new", 40);
132 509 bojilova
      session = request.getSession(true);
133
    }
134 725 bojilova
    // store the username, password, and groupname (the first only)
135
    // in the session obj for use on subsequent calls to Metacat servlet
136 509 bojilova
    session.setMaxInactiveInterval(-1);
137
    session.setAttribute("username", username);
138
    session.setAttribute("password", password);
139 725 bojilova
    if ( groups.length > 0 ) {
140 802 bojilova
      session.setAttribute("groupnames", groups);
141 725 bojilova
    }
142 2045 tao
     MetaCatUtil.debugMessage("the new session id is : " +
143
                                session.getId(), 30);
144
     MetaCatUtil.debugMessage("the new session username : " +
145
                                session.getAttribute("username"), 30);
146 509 bojilova
    return session;
147
  }
148
149 503 bojilova
  /**
150
   * Get the message associated with authenticating this session. The
151
   * message is formatted in XML.
152
   */
153 2058 sgarg
  public String getMessage()
154 503 bojilova
  {
155
    return this.statusMessage;
156
  }
157
158
  /**
159 730 bojilova
   * Get all groups and users from authentication scheme.
160 725 bojilova
   * The output is formatted in XML.
161 730 bojilova
   * @param user the user which requests the information
162
   * @param password the user's password
163 503 bojilova
   */
164 725 bojilova
  public String getPrincipals(String user, String password)
165
                throws ConnectException
166 503 bojilova
  {
167 730 bojilova
    return authService.getPrincipals(user, password);
168 503 bojilova
  }
169
170 2058 sgarg
  /*
171 503 bojilova
   * format the output in xml for processing from client applications
172
   *
173
   * @param tag the root element tag for the message (error or success)
174
   * @param message the message content of the root element
175
   */
176 2058 sgarg
  private String formatOutput(String tag, String message)
177 1822 jones
  {
178
      return formatOutput(tag, message, null);
179
  }
180
181 2058 sgarg
  /*
182 1822 jones
   * format the output in xml for processing from client applications
183
   *
184
   * @param tag the root element tag for the message (error or success)
185
   * @param message the message content of the root element
186
   * @param sessionId the session identifier for a successful login
187
   */
188 2058 sgarg
  private String formatOutput(String tag, String message, String sessionId)
189 1822 jones
  {
190 503 bojilova
    StringBuffer out = new StringBuffer();
191 2058 sgarg
192 503 bojilova
    out.append("<?xml version=\"1.0\"?>\n");
193
    out.append("<" + tag + ">");
194 510 bojilova
    out.append("\n  <message>" + message + "</message>\n");
195 1822 jones
    if (sessionId != null) {
196 1825 jones
        out.append("\n  <sessionId>" + sessionId + "</sessionId>\n");
197 1822 jones
    }
198 503 bojilova
    out.append("</" + tag + ">");
199 2058 sgarg
200 503 bojilova
    return out.toString();
201
  }
202
203
  /**
204
   * Instantiate a class using the name of the class at runtime
205
   *
206
   * @param className the fully qualified name of the class to instantiate
207
   */
208 509 bojilova
  private static Object createObject(String className) throws Exception {
209 2058 sgarg
210 503 bojilova
    Object object = null;
211
    try {
212
      Class classDefinition = Class.forName(className);
213
      object = classDefinition.newInstance();
214
    } catch (InstantiationException e) {
215 509 bojilova
      throw e;
216 503 bojilova
    } catch (IllegalAccessException e) {
217 509 bojilova
      throw e;
218 503 bojilova
    } catch (ClassNotFoundException e) {
219 509 bojilova
      throw e;
220 503 bojilova
    }
221
    return object;
222
  }
223
}