Project

General

Profile

1
/**
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: berkley $'
10
 *     '$Date: 2001-11-01 14:29:05 -0800 (Thu, 01 Nov 2001) $'
11
 * '$Revision: 867 $'
12
 *
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
 */
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
  public AuthSession() throws Exception {
50
    // Determine our session authentication method and
51
    // create an instance of the auth class
52
    MetaCatUtil util = new MetaCatUtil();
53
    this.authClass = util.getOption("authclass");
54
    this.authService = (AuthInterface)createObject(authClass);
55
  }
56

    
57
  /** 
58
   * determine if the credentials for this session are valid by 
59
   * authenticating them using the authService configured for this session.
60
   *
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
   */
65
  public boolean authenticate(HttpServletRequest request, 
66
                              String username, String password)  { 
67
    String message = null;
68
    try { 
69
      if ( authService.authenticate(username, password) ) {
70
        String[] groups = authService.getGroups(username,password,username);
71
        this.session = getSession(request, username, password, groups);
72
        message = "Authentication successful for user: " + username;
73
        this.statusMessage = formatOutput("login", message);
74
        return true;
75
      } else {  
76
        message = "Authentication failed for user: " + username;
77
        this.statusMessage = formatOutput("unauth_login", message);
78
        return false;
79
      }    
80
    } catch ( ConnectException ce ) {
81
      message = "Connection to the authentication service failed in " +
82
                "AuthSession.authenticate: " + ce.getMessage();
83
    } catch ( IllegalStateException ise ) {
84
      message = ise.getMessage();
85
    }
86
 
87
    this.statusMessage = formatOutput("error_login", message);
88
    return false;
89
  }
90

    
91
  /** Get new HttpSession and store username & password in it */
92
  private HttpSession getSession(HttpServletRequest request, 
93
                                 String username, String password,
94
                                 String[] groups)  
95
                      throws IllegalStateException {
96

    
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
    // store the username, password, and groupname (the first only)
106
    // in the session obj for use on subsequent calls to Metacat servlet
107
    session.setMaxInactiveInterval(-1);
108
    session.setAttribute("username", username);
109
    session.setAttribute("password", password);
110
    if ( groups.length > 0 ) {
111
      session.setAttribute("groupnames", groups);
112
    }
113
    
114
    return session;
115
  }
116

    
117
  /**
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
   * Get all groups and users from authentication scheme.
128
   * The output is formatted in XML.
129
   * @param user the user which requests the information
130
   * @param password the user's password
131
   */
132
  public String getPrincipals(String user, String password)
133
                throws ConnectException
134
  {
135
    return authService.getPrincipals(user, password);
136
  }
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
    out.append("\n  <message>" + message + "</message>\n");
151
    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
  private static Object createObject(String className) throws Exception {
162
 
163
    Object object = null;
164
    try {
165
      Class classDefinition = Class.forName(className);
166
      object = classDefinition.newInstance();
167
    } catch (InstantiationException e) {
168
      throw e;
169
    } catch (IllegalAccessException e) {
170
      throw e;
171
    } catch (ClassNotFoundException e) {
172
      throw e;
173
    }
174
    return object;
175
  }
176
}
(8-8/40)