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