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: bojilova $'
10
 *     '$Date: 2001-03-05 16:25:52 -0800 (Mon, 05 Mar 2001) $'
11
 * '$Revision: 725 $'
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
        String[] groups = authService.getGroups(username,password,username);
75
        this.session = getSession(request, username, password, groups);
76
        message = "Authentication successful for user: " + username;
77
        this.statusMessage = formatOutput("login", message);
78
        return true;
79
      } else {  
80
        message = "Authentication failed for user: " + username;
81
        this.statusMessage = formatOutput("unauth_login", message);
82
        return false;
83
      }    
84
    } catch ( ConnectException ce ) {
85
      message = "Connection to the authentication service failed in " +
86
                "AuthSession.authenticate: " + ce.getMessage();
87
    } catch ( IllegalStateException ise ) {
88
      message = ise.getMessage();
89
    }
90
 
91
    this.statusMessage = formatOutput("error_login", message);
92
    return false;
93
  }
94

    
95
  /** Get new HttpSession and store username & password in it */
96
  private HttpSession getSession(HttpServletRequest request, 
97
                                 String username, String password,
98
                                 String[] groups)  
99
                      throws IllegalStateException {
100

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

    
121
  /**
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
   * Get list of all groups and users from authentication scheme.
132
   * The output is formatted in XML.
133
   */
134
  public String getPrincipals(String user, String password)
135
                throws ConnectException
136
  {
137
    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
        for (int j=0; j <= usersForGroup.length; j++ ) {
150
          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
  }
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
    out.append("\n  <message>" + message + "</message>\n");
183
    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
  private static Object createObject(String className) throws Exception {
194
 
195
    Object object = null;
196
    try {
197
      Class classDefinition = Class.forName(className);
198
      object = classDefinition.newInstance();
199
    } catch (InstantiationException e) {
200
      throw e;
201
    } catch (IllegalAccessException e) {
202
      throw e;
203
    } catch (ClassNotFoundException e) {
204
      throw e;
205
    }
206
    return object;
207
  }
208
}
(8-8/43)