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: jones $'
10
 *     '$Date: 2001-01-18 11:52:00 -0800 (Thu, 18 Jan 2001) $'
11
 * '$Revision: 669 $'
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

    
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
    
57
  }
58

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

    
94
  /** 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
  /**
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
/* NOT NEEDED
126
  /**
127
   * Determine if the session has been successfully authenticated
128
   * @returns boolean true if authentication was successful, false otherwise
129
   */
130
/*
131
  public boolean isAuthenticated() 
132
  {
133
    return this.isAuthenticated;
134
  }
135
*/
136

    
137
/* NOT NEEDED
138
  /**
139
   * Invalidate this HTTPSession object. 
140
   * All objects stored in the session are unbound.
141
   */
142
/*
143
  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
*/
152
  /* 
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
    out.append("\n  <message>" + message + "</message>\n");
165
    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
  private static Object createObject(String className) throws Exception {
176
 
177
    Object object = null;
178
    try {
179
      Class classDefinition = Class.forName(className);
180
      object = classDefinition.newInstance();
181
    } catch (InstantiationException e) {
182
      throw e;
183
    } catch (IllegalAccessException e) {
184
      throw e;
185
    } catch (ClassNotFoundException e) {
186
      throw e;
187
    }
188
    return object;
189
  }
190
}
(8-8/43)