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 725 bojilova
                              String username, String password)  {
69 509 bojilova
70
    String message = null;
71 503 bojilova
72
    try {
73
      if ( authService.authenticate(username, password) ) {
74 725 bojilova
        String[] groups = authService.getGroups(username,password,username);
75
        this.session = getSession(request, username, password, groups);
76 510 bojilova
        message = "Authentication successful for user: " + username;
77
        this.statusMessage = formatOutput("login", message);
78 509 bojilova
        return true;
79 503 bojilova
      } else {
80 509 bojilova
        message = "Authentication failed for user: " + username;
81 510 bojilova
        this.statusMessage = formatOutput("unauth_login", message);
82
        return false;
83 503 bojilova
      }
84
    } catch ( ConnectException ce ) {
85 675 berkley
      message = "Connection to the authentication service failed in " +
86
                "AuthSession.authenticate: " + ce.getMessage();
87 509 bojilova
    } catch ( IllegalStateException ise ) {
88
      message = ise.getMessage();
89 503 bojilova
    }
90 509 bojilova
91 510 bojilova
    this.statusMessage = formatOutput("error_login", message);
92 509 bojilova
    return false;
93 503 bojilova
  }
94
95 509 bojilova
  /** Get new HttpSession and store username & password in it */
96
  private HttpSession getSession(HttpServletRequest request,
97 725 bojilova
                                 String username, String password,
98
                                 String[] groups)
99
                      throws IllegalStateException {
100 509 bojilova
101
    // get the current session object, create one if necessary
102
    HttpSession session = request.getSession(true);
103
104
    // if it is still in use invalidate and get a new one
105
    if ( !session.isNew() ) {
106
      session.invalidate();
107
      session = request.getSession(true);
108
    }
109 725 bojilova
    // store the username, password, and groupname (the first only)
110
    // in the session obj for use on subsequent calls to Metacat servlet
111 509 bojilova
    session.setMaxInactiveInterval(-1);
112
    session.setAttribute("username", username);
113
    session.setAttribute("password", password);
114 725 bojilova
    if ( groups.length > 0 ) {
115
      session.setAttribute("groupname", groups[0]);
116
    }
117 509 bojilova
118
    return session;
119
  }
120
121 503 bojilova
  /**
122
   * Get the message associated with authenticating this session. The
123
   * message is formatted in XML.
124
   */
125
  public String getMessage()
126
  {
127
    return this.statusMessage;
128
  }
129
130
  /**
131 725 bojilova
   * Get list of all groups and users from authentication scheme.
132
   * The output is formatted in XML.
133 503 bojilova
   */
134 725 bojilova
  public String getPrincipals(String user, String password)
135
                throws ConnectException
136 503 bojilova
  {
137 725 bojilova
    StringBuffer out = new StringBuffer();
138
    String[] groups = authService.getGroups(user, password);
139
140
    out.append("<?xml version=\"1.0\"?>\n");
141
    out.append("<principals>\n");
142
143
    // for the groups and users that belong to them
144
    if ( groups.length > 0 ) {
145
      for (int i=0; i < groups.length; i++ ) {
146
        out.append("  <group>\n");
147
        out.append("    <groupname>" + groups[i] + "<groupname>\n");
148
        String[] usersForGroup = authService.getUsers(user,password,groups[i]);
149 726 bojilova
        for (int j=0; j < usersForGroup.length; j++ ) {
150 725 bojilova
          out.append("    <user>\n");
151
          out.append("      <username>" + usersForGroup[j] + "<username>\n");
152
          out.append("    </user>\n");
153
        }
154
        out.append("</group>\n");
155
      }
156
    // for the users only when there are no any groups defined
157
    } else {
158
      String[] users = authService.getUsers(user, password);
159
      for (int j=0; j < users.length; j++ ) {
160
        out.append("  <user>\n");
161
        out.append("    <username>" + users[j] + "<username>\n");
162
        out.append("  </user>\n");
163
      }
164
    }
165
166
    out.append("</principals>");
167
    return out.toString();
168 503 bojilova
  }
169
170
  /*
171
   * 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
  private String formatOutput(String tag, String message) {
177
178
    StringBuffer out = new StringBuffer();
179
180
    out.append("<?xml version=\"1.0\"?>\n");
181
    out.append("<" + tag + ">");
182 510 bojilova
    out.append("\n  <message>" + message + "</message>\n");
183 503 bojilova
    out.append("</" + tag + ">");
184
185
    return out.toString();
186
  }
187
188
  /**
189
   * Instantiate a class using the name of the class at runtime
190
   *
191
   * @param className the fully qualified name of the class to instantiate
192
   */
193 509 bojilova
  private static Object createObject(String className) throws Exception {
194
195 503 bojilova
    Object object = null;
196
    try {
197
      Class classDefinition = Class.forName(className);
198
      object = classDefinition.newInstance();
199
    } catch (InstantiationException e) {
200 509 bojilova
      throw e;
201 503 bojilova
    } catch (IllegalAccessException e) {
202 509 bojilova
      throw e;
203 503 bojilova
    } catch (ClassNotFoundException e) {
204 509 bojilova
      throw e;
205 503 bojilova
    }
206
    return object;
207
  }
208
}