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
51
    // Determine our session authentication method and
52
    // create an instance of the auth class
53
    MetaCatUtil util = new MetaCatUtil();
54
    authClass = util.getOption("authclass");
55
    authService = (AuthInterface)createObject(authClass);
56 509 bojilova
57 503 bojilova
  }
58
59
  /**
60
   * determine if the credentials for this session are valid by
61
   * authenticating them using the authService configured for this session.
62 509 bojilova
   *
63
   * @param request the request made from the client
64
   * @param username the username entered when login
65
   * @param password the password entered when login
66 503 bojilova
   */
67 509 bojilova
  public boolean authenticate(HttpServletRequest request,
68
                        String username, String password)  {
69
70
    String message = null;
71 503 bojilova
72
    try {
73
      if ( authService.authenticate(username, password) ) {
74 509 bojilova
        this.session = getSession(request, username, password);
75 510 bojilova
        message = "Authentication successful for user: " + username;
76
        this.statusMessage = formatOutput("login", message);
77 509 bojilova
        return true;
78 503 bojilova
      } else {
79 509 bojilova
        message = "Authentication failed for user: " + username;
80 510 bojilova
        this.statusMessage = formatOutput("unauth_login", message);
81
        return false;
82 503 bojilova
      }
83
    } catch ( ConnectException ce ) {
84 675 berkley
      message = "Connection to the authentication service failed in " +
85
                "AuthSession.authenticate: " + ce.getMessage();
86 509 bojilova
    } catch ( IllegalStateException ise ) {
87
      message = ise.getMessage();
88 503 bojilova
    }
89 509 bojilova
90 510 bojilova
    this.statusMessage = formatOutput("error_login", message);
91 509 bojilova
    return false;
92 503 bojilova
  }
93
94 509 bojilova
  /** Get new HttpSession and store username & password in it */
95
  private HttpSession getSession(HttpServletRequest request,
96
                            String username, String password)
97
                                throws IllegalStateException {
98
99
    // get the current session object, create one if necessary
100
    HttpSession session = request.getSession(true);
101
102
    // if it is still in use invalidate and get a new one
103
    if ( !session.isNew() ) {
104
      session.invalidate();
105
      session = request.getSession(true);
106
    }
107
    // store username & password in the session for later use, especially by
108
    // the authenticate() method
109
    session.setMaxInactiveInterval(-1);
110
    session.setAttribute("username", username);
111
    session.setAttribute("password", password);
112
113
    return session;
114
  }
115
116 503 bojilova
  /**
117
   * Get the message associated with authenticating this session. The
118
   * message is formatted in XML.
119
   */
120
  public String getMessage()
121
  {
122
    return this.statusMessage;
123
  }
124
125 509 bojilova
/* NOT NEEDED
126 503 bojilova
  /**
127
   * Determine if the session has been successfully authenticated
128
   * @returns boolean true if authentication was successful, false otherwise
129
   */
130 509 bojilova
/*
131 503 bojilova
  public boolean isAuthenticated()
132
  {
133
    return this.isAuthenticated;
134
  }
135 509 bojilova
*/
136 503 bojilova
137 509 bojilova
/* NOT NEEDED
138 503 bojilova
  /**
139
   * Invalidate this HTTPSession object.
140
   * All objects stored in the session are unbound.
141
   */
142 509 bojilova
/*
143 503 bojilova
  private void invalidate(String message)
144
  {
145
    this.isAuthenticated = false;
146
    this.session.setAttribute("isAuthenticated", new Boolean(isAuthenticated));
147
    this.statusMessage = formatOutput("error", message);
148
    this.session.setAttribute("statusMessage", this.statusMessage);
149
    this.session.invalidate();
150
  }
151 509 bojilova
*/
152 503 bojilova
  /*
153
   * format the output in xml for processing from client applications
154
   *
155
   * @param tag the root element tag for the message (error or success)
156
   * @param message the message content of the root element
157
   */
158
  private String formatOutput(String tag, String message) {
159
160
    StringBuffer out = new StringBuffer();
161
162
    out.append("<?xml version=\"1.0\"?>\n");
163
    out.append("<" + tag + ">");
164 510 bojilova
    out.append("\n  <message>" + message + "</message>\n");
165 503 bojilova
    out.append("</" + tag + ">");
166
167
    return out.toString();
168
  }
169
170
  /**
171
   * Instantiate a class using the name of the class at runtime
172
   *
173
   * @param className the fully qualified name of the class to instantiate
174
   */
175 509 bojilova
  private static Object createObject(String className) throws Exception {
176
177 503 bojilova
    Object object = null;
178
    try {
179
      Class classDefinition = Class.forName(className);
180
      object = classDefinition.newInstance();
181
    } catch (InstantiationException e) {
182 509 bojilova
      throw e;
183 503 bojilova
    } catch (IllegalAccessException e) {
184 509 bojilova
      throw e;
185 503 bojilova
    } catch (ClassNotFoundException e) {
186 509 bojilova
      throw e;
187 503 bojilova
    }
188
    return object;
189
  }
190
}