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-22 15:49:18 -0800 (Thu, 22 Mar 2001) $'
11
 * '$Revision: 728 $'
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 ldapUrl;
45
  private String ldapBase;
46
  private String statusMessage = null;
47
 
48
  /** 
49
   * Construct an AuthSession
50
   */
51
  public AuthSession() throws Exception {
52

    
53
    // Determine our session authentication method and
54
    // create an instance of the auth class
55
    MetaCatUtil util = new MetaCatUtil();
56
    this.authClass = util.getOption("authclass");
57
    this.authService = (AuthInterface)createObject(authClass);
58
    this.ldapUrl = util.getOption("ldapurl");
59
    this.ldapBase = util.getOption("ldapbase");
60
    
61
  }
62

    
63
  /** 
64
   * determine if the credentials for this session are valid by 
65
   * authenticating them using the authService configured for this session.
66
   *
67
   * @param request the request made from the client
68
   * @param username the username entered when login
69
   * @param password the password entered when login
70
   */
71
  public boolean authenticate(HttpServletRequest request, 
72
                              String username, String password)  {
73
                          
74
    String message = null;
75
 
76
    try { 
77
      if ( authService.authenticate(username, password) ) {
78
        String[] groups = authService.getGroups(username,password,username);
79
        this.session = getSession(request, username, password, groups);
80
        message = "Authentication successful for user: " + username;
81
        this.statusMessage = formatOutput("login", message);
82
        return true;
83
      } else {  
84
        message = "Authentication failed for user: " + username;
85
        this.statusMessage = formatOutput("unauth_login", message);
86
        return false;
87
      }    
88
    } catch ( ConnectException ce ) {
89
      message = "Connection to the authentication service failed in " +
90
                "AuthSession.authenticate: " + ce.getMessage();
91
    } catch ( IllegalStateException ise ) {
92
      message = ise.getMessage();
93
    }
94
 
95
    this.statusMessage = formatOutput("error_login", message);
96
    return false;
97
  }
98

    
99
  /** Get new HttpSession and store username & password in it */
100
  private HttpSession getSession(HttpServletRequest request, 
101
                                 String username, String password,
102
                                 String[] groups)  
103
                      throws IllegalStateException {
104

    
105
    // get the current session object, create one if necessary
106
    HttpSession session = request.getSession(true);
107

    
108
    // if it is still in use invalidate and get a new one
109
    if ( !session.isNew() ) {
110
      session.invalidate();
111
      session = request.getSession(true);
112
    }
113
    // store the username, password, and groupname (the first only)
114
    // in the session obj for use on subsequent calls to Metacat servlet
115
    session.setMaxInactiveInterval(-1);
116
    session.setAttribute("username", username);
117
    session.setAttribute("password", password);
118
    if ( groups.length > 0 ) {
119
      session.setAttribute("groupname", groups[0]);
120
    }
121
    
122
    return session;
123
  }
124

    
125
  /**
126
   * Get the message associated with authenticating this session. The
127
   * message is formatted in XML.
128
   */
129
  public String getMessage() 
130
  {
131
    return this.statusMessage;
132
  }
133

    
134
  /**
135
   * Get list of all groups and users from authentication scheme.
136
   * The output is formatted in XML.
137
   */
138
  public String getPrincipals(String user, String password)
139
                throws ConnectException
140
  {
141
    StringBuffer out = new StringBuffer();
142
    String[] groups = authService.getGroups(user, password);
143
    
144
    out.append("<?xml version=\"1.0\"?>\n");
145
    out.append("<principals authSystemURI=\"" + ldapUrl + ldapBase + "\">\n");
146
    
147
    // for the groups and users that belong to them
148
    if ( groups.length > 0 ) {
149
      for (int i=0; i < groups.length; i++ ) {
150
        out.append("  <group>\n");
151
        out.append("    <groupname>" + groups[i] + "<groupname>\n");
152
        String[] usersForGroup = authService.getUsers(user,password,groups[i]);
153
        for (int j=0; j < usersForGroup.length; j++ ) {
154
          out.append("    <user>\n");
155
          out.append("      <username>" + usersForGroup[j] + "<username>\n");
156
          out.append("    </user>\n");
157
        }
158
        out.append("</group>\n");
159
      }
160
    // for the users only when there are no any groups defined
161
    } else {
162
      String[] users = authService.getUsers(user, password);
163
      for (int j=0; j < users.length; j++ ) {
164
        out.append("  <user>\n");
165
        out.append("    <username>" + users[j] + "<username>\n");
166
        out.append("  </user>\n");
167
      }
168
    }
169
    
170
    out.append("</principals>");
171
    return out.toString();
172
  }
173

    
174
  /* 
175
   * format the output in xml for processing from client applications
176
   *
177
   * @param tag the root element tag for the message (error or success)
178
   * @param message the message content of the root element
179
   */
180
  private String formatOutput(String tag, String message) {
181
    
182
    StringBuffer out = new StringBuffer();
183
      
184
    out.append("<?xml version=\"1.0\"?>\n");
185
    out.append("<" + tag + ">");
186
    out.append("\n  <message>" + message + "</message>\n");
187
    out.append("</" + tag + ">");
188
    
189
    return out.toString();
190
  }
191

    
192
  /**
193
   * Instantiate a class using the name of the class at runtime
194
   *
195
   * @param className the fully qualified name of the class to instantiate
196
   */
197
  private static Object createObject(String className) throws Exception {
198
 
199
    Object object = null;
200
    try {
201
      Class classDefinition = Class.forName(className);
202
      object = classDefinition.newInstance();
203
    } catch (InstantiationException e) {
204
      throw e;
205
    } catch (IllegalAccessException e) {
206
      throw e;
207
    } catch (ClassNotFoundException e) {
208
      throw e;
209
    }
210
    return object;
211
  }
212
}
(8-8/43)