1 |
503
|
bojilova
|
/**
|
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$'
|
10 |
|
|
* '$Date$'
|
11 |
|
|
* '$Revision$'
|
12 |
669
|
jones
|
*
|
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 |
503
|
bojilova
|
*/
|
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 |
509
|
bojilova
|
public AuthSession() throws Exception {
|
50 |
503
|
bojilova
|
|
51 |
|
|
// Determine our session authentication method and
|
52 |
|
|
// create an instance of the auth class
|
53 |
|
|
MetaCatUtil util = new MetaCatUtil();
|
54 |
728
|
bojilova
|
this.authClass = util.getOption("authclass");
|
55 |
|
|
this.authService = (AuthInterface)createObject(authClass);
|
56 |
509
|
bojilova
|
|
57 |
503
|
bojilova
|
}
|
58 |
|
|
|
59 |
|
|
/**
|
60 |
|
|
* determine if the credentials for this session are valid by
|
61 |
|
|
* authenticating them using the authService configured for this session.
|
62 |
509
|
bojilova
|
*
|
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 |
503
|
bojilova
|
*/
|
67 |
509
|
bojilova
|
public boolean authenticate(HttpServletRequest request,
|
68 |
725
|
bojilova
|
String username, String password) {
|
69 |
509
|
bojilova
|
|
70 |
|
|
String message = null;
|
71 |
503
|
bojilova
|
|
72 |
|
|
try {
|
73 |
|
|
if ( authService.authenticate(username, password) ) {
|
74 |
725
|
bojilova
|
String[] groups = authService.getGroups(username,password,username);
|
75 |
|
|
this.session = getSession(request, username, password, groups);
|
76 |
510
|
bojilova
|
message = "Authentication successful for user: " + username;
|
77 |
|
|
this.statusMessage = formatOutput("login", message);
|
78 |
509
|
bojilova
|
return true;
|
79 |
503
|
bojilova
|
} else {
|
80 |
509
|
bojilova
|
message = "Authentication failed for user: " + username;
|
81 |
510
|
bojilova
|
this.statusMessage = formatOutput("unauth_login", message);
|
82 |
|
|
return false;
|
83 |
503
|
bojilova
|
}
|
84 |
|
|
} catch ( ConnectException ce ) {
|
85 |
675
|
berkley
|
message = "Connection to the authentication service failed in " +
|
86 |
|
|
"AuthSession.authenticate: " + ce.getMessage();
|
87 |
509
|
bojilova
|
} catch ( IllegalStateException ise ) {
|
88 |
|
|
message = ise.getMessage();
|
89 |
503
|
bojilova
|
}
|
90 |
509
|
bojilova
|
|
91 |
510
|
bojilova
|
this.statusMessage = formatOutput("error_login", message);
|
92 |
509
|
bojilova
|
return false;
|
93 |
503
|
bojilova
|
}
|
94 |
|
|
|
95 |
509
|
bojilova
|
/** Get new HttpSession and store username & password in it */
|
96 |
|
|
private HttpSession getSession(HttpServletRequest request,
|
97 |
725
|
bojilova
|
String username, String password,
|
98 |
|
|
String[] groups)
|
99 |
|
|
throws IllegalStateException {
|
100 |
509
|
bojilova
|
|
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 |
725
|
bojilova
|
// store the username, password, and groupname (the first only)
|
110 |
|
|
// in the session obj for use on subsequent calls to Metacat servlet
|
111 |
509
|
bojilova
|
session.setMaxInactiveInterval(-1);
|
112 |
|
|
session.setAttribute("username", username);
|
113 |
|
|
session.setAttribute("password", password);
|
114 |
725
|
bojilova
|
if ( groups.length > 0 ) {
|
115 |
|
|
session.setAttribute("groupname", groups[0]);
|
116 |
|
|
}
|
117 |
509
|
bojilova
|
|
118 |
|
|
return session;
|
119 |
|
|
}
|
120 |
|
|
|
121 |
503
|
bojilova
|
/**
|
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 |
730
|
bojilova
|
* Get all groups and users from authentication scheme.
|
132 |
725
|
bojilova
|
* The output is formatted in XML.
|
133 |
730
|
bojilova
|
* @param user the user which requests the information
|
134 |
|
|
* @param password the user's password
|
135 |
503
|
bojilova
|
*/
|
136 |
725
|
bojilova
|
public String getPrincipals(String user, String password)
|
137 |
|
|
throws ConnectException
|
138 |
503
|
bojilova
|
{
|
139 |
730
|
bojilova
|
return authService.getPrincipals(user, password);
|
140 |
503
|
bojilova
|
}
|
141 |
|
|
|
142 |
|
|
/*
|
143 |
|
|
* format the output in xml for processing from client applications
|
144 |
|
|
*
|
145 |
|
|
* @param tag the root element tag for the message (error or success)
|
146 |
|
|
* @param message the message content of the root element
|
147 |
|
|
*/
|
148 |
|
|
private String formatOutput(String tag, String message) {
|
149 |
|
|
|
150 |
|
|
StringBuffer out = new StringBuffer();
|
151 |
|
|
|
152 |
|
|
out.append("<?xml version=\"1.0\"?>\n");
|
153 |
|
|
out.append("<" + tag + ">");
|
154 |
510
|
bojilova
|
out.append("\n <message>" + message + "</message>\n");
|
155 |
503
|
bojilova
|
out.append("</" + tag + ">");
|
156 |
|
|
|
157 |
|
|
return out.toString();
|
158 |
|
|
}
|
159 |
|
|
|
160 |
|
|
/**
|
161 |
|
|
* Instantiate a class using the name of the class at runtime
|
162 |
|
|
*
|
163 |
|
|
* @param className the fully qualified name of the class to instantiate
|
164 |
|
|
*/
|
165 |
509
|
bojilova
|
private static Object createObject(String className) throws Exception {
|
166 |
|
|
|
167 |
503
|
bojilova
|
Object object = null;
|
168 |
|
|
try {
|
169 |
|
|
Class classDefinition = Class.forName(className);
|
170 |
|
|
object = classDefinition.newInstance();
|
171 |
|
|
} catch (InstantiationException e) {
|
172 |
509
|
bojilova
|
throw e;
|
173 |
503
|
bojilova
|
} catch (IllegalAccessException e) {
|
174 |
509
|
bojilova
|
throw e;
|
175 |
503
|
bojilova
|
} catch (ClassNotFoundException e) {
|
176 |
509
|
bojilova
|
throw e;
|
177 |
503
|
bojilova
|
}
|
178 |
|
|
return object;
|
179 |
|
|
}
|
180 |
|
|
}
|