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
46
  /**
47
   * 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
57
  /**
58
   * determine if the credentials for this session are valid by
59
   * authenticating them using the authService configured for this session.
60 509 bojilova
   *
61
   * @param request the request made from the client
62
   * @param username the username entered when login
63
   * @param password the password entered when login
64 503 bojilova
   */
65 509 bojilova
  public boolean authenticate(HttpServletRequest request,
66 867 berkley
                              String username, String password)  {
67 509 bojilova
    String message = null;
68 503 bojilova
    try {
69
      if ( authService.authenticate(username, password) ) {
70 725 bojilova
        String[] groups = authService.getGroups(username,password,username);
71 888 berkley
        if(groups == null)
72
        {
73
          groups = new String[0];
74
        }
75 725 bojilova
        this.session = getSession(request, username, password, groups);
76 1822 jones
        String sessionId = session.getId();
77 510 bojilova
        message = "Authentication successful for user: " + username;
78 1822 jones
        this.statusMessage = formatOutput("login", message, sessionId);
79 509 bojilova
        return true;
80 503 bojilova
      } else {
81 509 bojilova
        message = "Authentication failed for user: " + username;
82 510 bojilova
        this.statusMessage = formatOutput("unauth_login", message);
83
        return false;
84 503 bojilova
      }
85
    } catch ( ConnectException ce ) {
86 675 berkley
      message = "Connection to the authentication service failed in " +
87
                "AuthSession.authenticate: " + ce.getMessage();
88 509 bojilova
    } catch ( IllegalStateException ise ) {
89
      message = ise.getMessage();
90 503 bojilova
    }
91 509 bojilova
92 510 bojilova
    this.statusMessage = formatOutput("error_login", message);
93 509 bojilova
    return false;
94 503 bojilova
  }
95
96 509 bojilova
  /** Get new HttpSession and store username & password in it */
97
  private HttpSession getSession(HttpServletRequest request,
98 725 bojilova
                                 String username, String password,
99
                                 String[] groups)
100
                      throws IllegalStateException {
101 509 bojilova
102
    // get the current session object, create one if necessary
103
    HttpSession session = request.getSession(true);
104
105
    // if it is still in use invalidate and get a new one
106
    if ( !session.isNew() ) {
107
      session.invalidate();
108
      session = request.getSession(true);
109
    }
110 725 bojilova
    // store the username, password, and groupname (the first only)
111
    // in the session obj for use on subsequent calls to Metacat servlet
112 509 bojilova
    session.setMaxInactiveInterval(-1);
113
    session.setAttribute("username", username);
114
    session.setAttribute("password", password);
115 725 bojilova
    if ( groups.length > 0 ) {
116 802 bojilova
      session.setAttribute("groupnames", groups);
117 725 bojilova
    }
118 509 bojilova
119
    return session;
120
  }
121
122 503 bojilova
  /**
123
   * Get the message associated with authenticating this session. The
124
   * message is formatted in XML.
125
   */
126
  public String getMessage()
127
  {
128
    return this.statusMessage;
129
  }
130
131
  /**
132 730 bojilova
   * Get all groups and users from authentication scheme.
133 725 bojilova
   * The output is formatted in XML.
134 730 bojilova
   * @param user the user which requests the information
135
   * @param password the user's password
136 503 bojilova
   */
137 725 bojilova
  public String getPrincipals(String user, String password)
138
                throws ConnectException
139 503 bojilova
  {
140 730 bojilova
    return authService.getPrincipals(user, password);
141 503 bojilova
  }
142
143
  /*
144
   * format the output in xml for processing from client applications
145
   *
146
   * @param tag the root element tag for the message (error or success)
147
   * @param message the message content of the root element
148
   */
149 1822 jones
  private String formatOutput(String tag, String message)
150
  {
151
      return formatOutput(tag, message, null);
152
  }
153
154
  /*
155
   * format the output in xml for processing from client applications
156
   *
157
   * @param tag the root element tag for the message (error or success)
158
   * @param message the message content of the root element
159
   * @param sessionId the session identifier for a successful login
160
   */
161
  private String formatOutput(String tag, String message, String sessionId)
162
  {
163 503 bojilova
    StringBuffer out = new StringBuffer();
164
165
    out.append("<?xml version=\"1.0\"?>\n");
166
    out.append("<" + tag + ">");
167 510 bojilova
    out.append("\n  <message>" + message + "</message>\n");
168 1822 jones
    if (sessionId != null) {
169 1825 jones
        out.append("\n  <sessionId>" + sessionId + "</sessionId>\n");
170 1822 jones
    }
171 503 bojilova
    out.append("</" + tag + ">");
172
173
    return out.toString();
174
  }
175
176
  /**
177
   * Instantiate a class using the name of the class at runtime
178
   *
179
   * @param className the fully qualified name of the class to instantiate
180
   */
181 509 bojilova
  private static Object createObject(String className) throws Exception {
182
183 503 bojilova
    Object object = null;
184
    try {
185
      Class classDefinition = Class.forName(className);
186
      object = classDefinition.newInstance();
187
    } catch (InstantiationException e) {
188 509 bojilova
      throw e;
189 503 bojilova
    } catch (IllegalAccessException e) {
190 509 bojilova
      throw e;
191 503 bojilova
    } catch (ClassNotFoundException e) {
192 509 bojilova
      throw e;
193 503 bojilova
    }
194
    return object;
195
  }
196
}