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: 2000-10-24 13:03:07 -0700 (Tue, 24 Oct 2000) $'
|
11
|
* '$Revision: 503 $'
|
12
|
*/
|
13
|
|
14
|
package edu.ucsb.nceas.metacat;
|
15
|
|
16
|
import java.net.ConnectException;
|
17
|
import javax.servlet.http.HttpSession;
|
18
|
import javax.servlet.http.HttpServletRequest;
|
19
|
|
20
|
/**
|
21
|
* A Class that implements session tracking for MetaCatServlet users.
|
22
|
* User's login data are stored in the session object.
|
23
|
* User authentication is done through a dynamically determined AuthInterface.
|
24
|
*/
|
25
|
public class AuthSession {
|
26
|
|
27
|
private String authClass = null;
|
28
|
|
29
|
private HttpSession session = null;
|
30
|
private AuthInterface authService = null;
|
31
|
private boolean isAuthenticated = false;
|
32
|
private String statusMessage = null;
|
33
|
|
34
|
/**
|
35
|
* Construct an AuthSession
|
36
|
*
|
37
|
* @param request the request made from the client
|
38
|
* @param username the username entered when login
|
39
|
* @param password the password entered when login
|
40
|
*/
|
41
|
public AuthSession(HttpServletRequest request,
|
42
|
String username, String password)
|
43
|
throws IllegalStateException {
|
44
|
|
45
|
// Initialize attributes
|
46
|
isAuthenticated = false;
|
47
|
|
48
|
// Determine our session authentication method and
|
49
|
// create an instance of the auth class
|
50
|
MetaCatUtil util = new MetaCatUtil();
|
51
|
authClass = util.getOption("authclass");
|
52
|
authService = (AuthInterface)createObject(authClass);
|
53
|
|
54
|
// get the current session object, create one if necessary
|
55
|
session = request.getSession(true);
|
56
|
|
57
|
// if it is still in use invalidate and get a new one
|
58
|
if ( !session.isNew() ) {
|
59
|
session.invalidate();
|
60
|
session = request.getSession(true);
|
61
|
}
|
62
|
// store username & password in the session for later use, especially by
|
63
|
// the authenticate() method
|
64
|
session.setMaxInactiveInterval(-1);
|
65
|
session.setAttribute("username", username);
|
66
|
session.setAttribute("password", password);
|
67
|
session.setAttribute("isAuthenticated", new Boolean(isAuthenticated));
|
68
|
}
|
69
|
|
70
|
/**
|
71
|
* determine if the credentials for this session are valid by
|
72
|
* authenticating them using the authService configured for this session.
|
73
|
* Data for authenticating is derived from the attributes stored in
|
74
|
* the session object.
|
75
|
*/
|
76
|
public boolean authenticate()
|
77
|
{
|
78
|
String out = null;
|
79
|
String username = (String)session.getAttribute("username");
|
80
|
String password = (String)session.getAttribute("password");
|
81
|
|
82
|
try {
|
83
|
if ( authService.authenticate(username, password) ) {
|
84
|
this.isAuthenticated = true;
|
85
|
this.session.setAttribute("isAuthenticated",
|
86
|
new Boolean(isAuthenticated));
|
87
|
String message = "User Authentication successful";
|
88
|
this.statusMessage = formatOutput("success", message);
|
89
|
} else {
|
90
|
String message = "Authentication failed for user: " + username;
|
91
|
invalidate(message);
|
92
|
}
|
93
|
} catch ( ConnectException ce ) {
|
94
|
String message = "Connection to the authentication service failed. "
|
95
|
+ ce.getMessage();
|
96
|
invalidate(message);
|
97
|
}
|
98
|
return this.isAuthenticated;
|
99
|
}
|
100
|
|
101
|
/**
|
102
|
* Get the message associated with authenticating this session. The
|
103
|
* message is formatted in XML.
|
104
|
*/
|
105
|
public String getMessage()
|
106
|
{
|
107
|
return this.statusMessage;
|
108
|
}
|
109
|
|
110
|
/**
|
111
|
* Determine if the session has been successfully authenticated
|
112
|
* @returns boolean true if authentication was successful, false otherwise
|
113
|
*/
|
114
|
public boolean isAuthenticated()
|
115
|
{
|
116
|
return this.isAuthenticated;
|
117
|
}
|
118
|
|
119
|
/**
|
120
|
* Invalidate this HTTPSession object.
|
121
|
* All objects stored in the session are unbound.
|
122
|
*/
|
123
|
private void invalidate(String message)
|
124
|
{
|
125
|
this.isAuthenticated = false;
|
126
|
this.session.setAttribute("isAuthenticated", new Boolean(isAuthenticated));
|
127
|
this.statusMessage = formatOutput("error", message);
|
128
|
this.session.setAttribute("statusMessage", this.statusMessage);
|
129
|
this.session.invalidate();
|
130
|
}
|
131
|
|
132
|
/*
|
133
|
* format the output in xml for processing from client applications
|
134
|
*
|
135
|
* @param tag the root element tag for the message (error or success)
|
136
|
* @param message the message content of the root element
|
137
|
*/
|
138
|
private String formatOutput(String tag, String message) {
|
139
|
|
140
|
StringBuffer out = new StringBuffer();
|
141
|
|
142
|
out.append("<?xml version=\"1.0\"?>\n");
|
143
|
out.append("<" + tag + ">");
|
144
|
if ( tag.equals("error") ) {
|
145
|
out.append(message);
|
146
|
} else {
|
147
|
out.append("\n <message>" + message + "</message>\n");
|
148
|
String username = (String)this.session.getAttribute("username");
|
149
|
out.append(" <username>" + username + "</username>\n");
|
150
|
}
|
151
|
out.append("</" + tag + ">");
|
152
|
|
153
|
return out.toString();
|
154
|
}
|
155
|
|
156
|
/**
|
157
|
* Instantiate a class using the name of the class at runtime
|
158
|
*
|
159
|
* @param className the fully qualified name of the class to instantiate
|
160
|
*/
|
161
|
private static Object createObject(String className)
|
162
|
{
|
163
|
Object object = null;
|
164
|
try {
|
165
|
Class classDefinition = Class.forName(className);
|
166
|
object = classDefinition.newInstance();
|
167
|
} catch (InstantiationException e) {
|
168
|
System.out.println(e);
|
169
|
} catch (IllegalAccessException e) {
|
170
|
System.out.println(e);
|
171
|
} catch (ClassNotFoundException e) {
|
172
|
System.out.println(e);
|
173
|
}
|
174
|
return object;
|
175
|
}
|
176
|
}
|