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: sgarg $'
10
 *     '$Date: 2004-03-23 10:26:13 -0800 (Tue, 23 Mar 2004) $'
11
 * '$Revision: 2058 $'
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
   * Get the new session
59
   */
60
  public HttpSession getSessions()
61
  {
62
    return this.session;
63
  }
64

    
65
  /**
66
   * determine if the credentials for this session are valid by
67
   * authenticating them using the authService configured for this session.
68
   *
69
   * @param request the request made from the client
70
   * @param username the username entered when login
71
   * @param password the password entered when login
72
   */
73
  public boolean authenticate(HttpServletRequest request,
74
                              String username, String password)  {
75
    String message = null;
76
    try {
77
      if ( authService.authenticate(username, password) ) {
78

    
79
        // getGroups returns groupname along with their description.
80
        // hence groups[] is generated from groupsWithDescription[][]
81
        String[][] groupsWithDescription =
82
            authService.getGroups(username,password,username);
83
        String groups[] = new String[groupsWithDescription.length];
84

    
85
        for(int i=0; i<groupsWithDescription.length; i++){
86
          groups[i] = groupsWithDescription[i][0];
87
        }
88

    
89
        if(groups == null)
90
        {
91
          groups = new String[0];
92
        }
93
        this.session = createSession(request, username, password, groups);
94
        String sessionId = session.getId();
95
        message = "Authentication successful for user: " + username;
96
        this.statusMessage = formatOutput("login", message, sessionId);
97
        return true;
98
      } else {
99
        message = "Authentication failed for user: " + username;
100
        this.statusMessage = formatOutput("unauth_login", message);
101
        return false;
102
      }
103
    } catch ( ConnectException ce ) {
104
      message = "Connection to the authentication service failed in " +
105
                "AuthSession.authenticate: " + ce.getMessage();
106
    } catch ( IllegalStateException ise ) {
107
      message = ise.getMessage();
108
    }
109

    
110
    this.statusMessage = formatOutput("error_login", message);
111
    return false;
112
  }
113

    
114
  /** Get new HttpSession and store username & password in it */
115
  private HttpSession createSession(HttpServletRequest request,
116
                                 String username, String password,
117
                                 String[] groups)
118
                      throws IllegalStateException {
119

    
120
    // get the current session object, create one if necessary
121
    HttpSession session = request.getSession(true);
122

    
123
    // if it is still in use invalidate and get a new one
124
    if ( !session.isNew() ) {
125
      MetaCatUtil.debugMessage("in session is not new", 40);
126
      MetaCatUtil.debugMessage("the old session id is : " +
127
                                session.getId(), 30);
128
      MetaCatUtil.debugMessage("the old session username : " +
129
                                session.getAttribute("username"), 30);
130
      session.invalidate();
131
      MetaCatUtil.debugMessage("in session is not new", 40);
132
      session = request.getSession(true);
133
    }
134
    // store the username, password, and groupname (the first only)
135
    // in the session obj for use on subsequent calls to Metacat servlet
136
    session.setMaxInactiveInterval(-1);
137
    session.setAttribute("username", username);
138
    session.setAttribute("password", password);
139
    if ( groups.length > 0 ) {
140
      session.setAttribute("groupnames", groups);
141
    }
142
     MetaCatUtil.debugMessage("the new session id is : " +
143
                                session.getId(), 30);
144
     MetaCatUtil.debugMessage("the new session username : " +
145
                                session.getAttribute("username"), 30);
146
    return session;
147
  }
148

    
149
  /**
150
   * Get the message associated with authenticating this session. The
151
   * message is formatted in XML.
152
   */
153
  public String getMessage()
154
  {
155
    return this.statusMessage;
156
  }
157

    
158
  /**
159
   * Get all groups and users from authentication scheme.
160
   * The output is formatted in XML.
161
   * @param user the user which requests the information
162
   * @param password the user's password
163
   */
164
  public String getPrincipals(String user, String password)
165
                throws ConnectException
166
  {
167
    return authService.getPrincipals(user, password);
168
  }
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
      return formatOutput(tag, message, null);
179
  }
180

    
181
  /*
182
   * format the output in xml for processing from client applications
183
   *
184
   * @param tag the root element tag for the message (error or success)
185
   * @param message the message content of the root element
186
   * @param sessionId the session identifier for a successful login
187
   */
188
  private String formatOutput(String tag, String message, String sessionId)
189
  {
190
    StringBuffer out = new StringBuffer();
191

    
192
    out.append("<?xml version=\"1.0\"?>\n");
193
    out.append("<" + tag + ">");
194
    out.append("\n  <message>" + message + "</message>\n");
195
    if (sessionId != null) {
196
        out.append("\n  <sessionId>" + sessionId + "</sessionId>\n");
197
    }
198
    out.append("</" + tag + ">");
199

    
200
    return out.toString();
201
  }
202

    
203
  /**
204
   * Instantiate a class using the name of the class at runtime
205
   *
206
   * @param className the fully qualified name of the class to instantiate
207
   */
208
  private static Object createObject(String className) throws Exception {
209

    
210
    Object object = null;
211
    try {
212
      Class classDefinition = Class.forName(className);
213
      object = classDefinition.newInstance();
214
    } catch (InstantiationException e) {
215
      throw e;
216
    } catch (IllegalAccessException e) {
217
      throw e;
218
    } catch (ClassNotFoundException e) {
219
      throw e;
220
    }
221
    return object;
222
  }
223
}
(13-13/58)