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: 2003-09-17 18:32:33 -0700 (Wed, 17 Sep 2003) $'
|
11
|
* '$Revision: 1825 $'
|
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
|
* determine if the credentials for this session are valid by
|
59
|
* authenticating them using the authService configured for this session.
|
60
|
*
|
61
|
* @param request the request made from the client
|
62
|
* @param username the username entered when login
|
63
|
* @param password the password entered when login
|
64
|
*/
|
65
|
public boolean authenticate(HttpServletRequest request,
|
66
|
String username, String password) {
|
67
|
String message = null;
|
68
|
try {
|
69
|
if ( authService.authenticate(username, password) ) {
|
70
|
String[] groups = authService.getGroups(username,password,username);
|
71
|
if(groups == null)
|
72
|
{
|
73
|
groups = new String[0];
|
74
|
}
|
75
|
this.session = getSession(request, username, password, groups);
|
76
|
String sessionId = session.getId();
|
77
|
message = "Authentication successful for user: " + username;
|
78
|
this.statusMessage = formatOutput("login", message, sessionId);
|
79
|
return true;
|
80
|
} else {
|
81
|
message = "Authentication failed for user: " + username;
|
82
|
this.statusMessage = formatOutput("unauth_login", message);
|
83
|
return false;
|
84
|
}
|
85
|
} catch ( ConnectException ce ) {
|
86
|
message = "Connection to the authentication service failed in " +
|
87
|
"AuthSession.authenticate: " + ce.getMessage();
|
88
|
} catch ( IllegalStateException ise ) {
|
89
|
message = ise.getMessage();
|
90
|
}
|
91
|
|
92
|
this.statusMessage = formatOutput("error_login", message);
|
93
|
return false;
|
94
|
}
|
95
|
|
96
|
/** Get new HttpSession and store username & password in it */
|
97
|
private HttpSession getSession(HttpServletRequest request,
|
98
|
String username, String password,
|
99
|
String[] groups)
|
100
|
throws IllegalStateException {
|
101
|
|
102
|
// get the current session object, create one if necessary
|
103
|
HttpSession session = request.getSession(true);
|
104
|
|
105
|
// if it is still in use invalidate and get a new one
|
106
|
if ( !session.isNew() ) {
|
107
|
session.invalidate();
|
108
|
session = request.getSession(true);
|
109
|
}
|
110
|
// store the username, password, and groupname (the first only)
|
111
|
// in the session obj for use on subsequent calls to Metacat servlet
|
112
|
session.setMaxInactiveInterval(-1);
|
113
|
session.setAttribute("username", username);
|
114
|
session.setAttribute("password", password);
|
115
|
if ( groups.length > 0 ) {
|
116
|
session.setAttribute("groupnames", groups);
|
117
|
}
|
118
|
|
119
|
return session;
|
120
|
}
|
121
|
|
122
|
/**
|
123
|
* Get the message associated with authenticating this session. The
|
124
|
* message is formatted in XML.
|
125
|
*/
|
126
|
public String getMessage()
|
127
|
{
|
128
|
return this.statusMessage;
|
129
|
}
|
130
|
|
131
|
/**
|
132
|
* Get all groups and users from authentication scheme.
|
133
|
* The output is formatted in XML.
|
134
|
* @param user the user which requests the information
|
135
|
* @param password the user's password
|
136
|
*/
|
137
|
public String getPrincipals(String user, String password)
|
138
|
throws ConnectException
|
139
|
{
|
140
|
return authService.getPrincipals(user, password);
|
141
|
}
|
142
|
|
143
|
/*
|
144
|
* format the output in xml for processing from client applications
|
145
|
*
|
146
|
* @param tag the root element tag for the message (error or success)
|
147
|
* @param message the message content of the root element
|
148
|
*/
|
149
|
private String formatOutput(String tag, String message)
|
150
|
{
|
151
|
return formatOutput(tag, message, null);
|
152
|
}
|
153
|
|
154
|
/*
|
155
|
* format the output in xml for processing from client applications
|
156
|
*
|
157
|
* @param tag the root element tag for the message (error or success)
|
158
|
* @param message the message content of the root element
|
159
|
* @param sessionId the session identifier for a successful login
|
160
|
*/
|
161
|
private String formatOutput(String tag, String message, String sessionId)
|
162
|
{
|
163
|
StringBuffer out = new StringBuffer();
|
164
|
|
165
|
out.append("<?xml version=\"1.0\"?>\n");
|
166
|
out.append("<" + tag + ">");
|
167
|
out.append("\n <message>" + message + "</message>\n");
|
168
|
if (sessionId != null) {
|
169
|
out.append("\n <sessionId>" + sessionId + "</sessionId>\n");
|
170
|
}
|
171
|
out.append("</" + tag + ">");
|
172
|
|
173
|
return out.toString();
|
174
|
}
|
175
|
|
176
|
/**
|
177
|
* Instantiate a class using the name of the class at runtime
|
178
|
*
|
179
|
* @param className the fully qualified name of the class to instantiate
|
180
|
*/
|
181
|
private static Object createObject(String className) throws Exception {
|
182
|
|
183
|
Object object = null;
|
184
|
try {
|
185
|
Class classDefinition = Class.forName(className);
|
186
|
object = classDefinition.newInstance();
|
187
|
} catch (InstantiationException e) {
|
188
|
throw e;
|
189
|
} catch (IllegalAccessException e) {
|
190
|
throw e;
|
191
|
} catch (ClassNotFoundException e) {
|
192
|
throw e;
|
193
|
}
|
194
|
return object;
|
195
|
}
|
196
|
}
|