Project

General

Profile

« Previous | Next » 

Revision 4586

Added by daigle about 16 years ago

Added this class to be able to test multiple authentication strategies

View differences:

src/edu/ucsb/nceas/metacat/AuthTest.java
1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: An implementation of the AuthInterface interface that
4
 *             allows Metacat to use the LDAP protocol for
5
 *             directory services
6
 *  Copyright: 2000 Regents of the University of California and the
7
 *             National Center for Ecological Analysis and Synthesis
8
 *    Authors: Matt Jones
9
 *
10
 *   '$Author: daigle $'
11
 *     '$Date: 2008-11-10 15:22:04 -0800 (Mon, 10 Nov 2008) $'
12
 * '$Revision: 4547 $'
13
 *
14
 * This program is free software; you can redistribute it and/or modify
15
 * it under the terms of the GNU General Public License as published by
16
 * the Free Software Foundation; either version 2 of the License, or
17
 * (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU General Public License
25
 * along with this program; if not, write to the Free Software
26
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27
 */
28

  
29
package edu.ucsb.nceas.metacat;
30

  
31
import java.net.ConnectException;
32

  
33
import org.apache.log4j.Logger;
34

  
35
import edu.ucsb.nceas.metacat.service.PropertyService;
36
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
37

  
38
import java.lang.InstantiationException;
39
import java.util.HashMap;
40
import java.util.Vector;
41

  
42
/**
43
 * An implementation of the AuthInterface interface that
44
 * allows Metacat to use the LDAP protocol for directory services.
45
 * The LDAP authentication service is used to determine if a user
46
 * is authenticated, and whether they are a member of a particular group.
47
 */
48
public class AuthTest implements AuthInterface {
49
	private String authUrl = "";
50
	private String testUser = "test-user";
51
	private String testUserName = "Test User";
52
	private String testPassword = "test-password";
53
	private String testGroup = "test-group";
54
	private String testGroupDesc = "this is a test group";
55
	private String testOrg = "NCEAS";
56
	private String testOrgUnit = "UCSB";
57
	private String testEmail = "test-user@dummy.email.com";
58
	private String otherTestUser = "other-test-user";
59
	private String otherTestUserName = "Other Test User";
60
	private String otherTestGroup = "other-test-group";
61
	private String otherTestGroupDesc = "this is a another test group";
62
	private String otherTestOrg = "DUMMY";
63
	private String otherTestOrgUnit = "UCLA";
64
	private String otherTestEmail = "other-test-user@dummy.email.com";
65
	private String attributeName = "attribute-name";
66
	private String attributeValue1 = "attribute-value1";
67
	private String attributeValue2 = "attribute-value2";
68
	
69

  
70
  private static Logger logMetacat = Logger.getLogger(AuthTest.class);
71
  
72
    /**
73
	 * Construct an AuthTest
74
	 */
75
	public AuthTest() throws InstantiationException {	
76
		try {
77
			authUrl = PropertyService.getProperty("auth.url");
78
		}
79
			catch (PropertyNotFoundException pnfe) {
80
				throw new InstantiationException(
81
						"Could not instantiate AuthTest.  Property not found: "
82
								+ pnfe.getMessage());
83
			}
84
	}
85

  
86
  /**
87
	 * Determine if a user/password are valid according to the authentication
88
	 * service.
89
	 * 
90
	 * @param user
91
	 *            the name of the principal to authenticate
92
	 * @param password
93
	 *            the password to use for authentication
94
	 * @returns boolean true if authentication successful, false otherwise
95
	 */
96
  
97
  public boolean authenticate(String user, String password) throws ConnectException {
98
    if (user.equals(testUser) && password.equals(testPassword)) {
99
    	logMetacat.debug(user + " is authenticated");
100
    	return true;
101
    }
102
    
103
    logMetacat.debug(user + " could not be authenticated");
104
    return false;
105
  }
106
  
107
  /**
108
   * Get all users from the authentication service
109
   *
110
   * @param user the user for authenticating against the service
111
   * @param password the password for authenticating against the service
112
   * @returns string array of all of the user names
113
   */
114
  public String[][] getUsers(String user, String password) throws 
115
    ConnectException {
116
    String[][] users = new String[1][1];
117
    
118
    users[0][0] = testUser;
119
    
120
    return users;
121
  }
122

  
123
  
124
  /**
125
   * Get all users from the authentication service
126
   *
127
   * @param user the user for authenticating against the service
128
   * @param password the password for authenticating against the service
129
   * @returns string array of all of the user names
130
   */
131
  public String[] getUserInfo(String user, String password) throws 
132
    ConnectException {
133
    String[] userinfo = new String[3];
134

  
135
    userinfo[0] = testUser;
136
    userinfo[1] = testOrg;
137
    userinfo[2] = testEmail;
138

  
139
    return userinfo;
140
  }
141

  
142
  /**
143
   * Get the users for a particular group from the authentication service
144
   *
145
   * @param user the user for authenticating against the service
146
   * @param password the password for authenticating against the service
147
   * @param group the group whose user list should be returned
148
   * @returns string array of the user names belonging to the group
149
   */
150
  public String[] getUsers(String user, String password, String group) throws 
151
    ConnectException {
152
    String[] users = null;
153

  
154
    users[0] = testUser;
155

  
156
    return users;
157
  }
158

  
159
  /**
160
   * Get all groups from the authentication service
161
   *
162
   * @param user the user for authenticating against the service
163
   * @param password the password for authenticating against the service
164
   * @returns string array of the group names
165
   */
166
  public String[][] getGroups(String user, String password) throws 
167
    ConnectException {
168
    return getGroups(user, password, null);
169
  }
170

  
171
  /**
172
   * Get the groups for a particular user from the authentication service
173
   *
174
   * @param user the user for authenticating against the service
175
   * @param password the password for authenticating against the service
176
   * @param foruser the user whose group list should be returned
177
   * @returns string array of the group names
178
   */
179
  public String[][] getGroups(String user, String password, 
180
    String foruser) throws ConnectException {
181
    
182
    //build and return the groups array
183
    String groups[][] = new String[1][2];
184
    
185
    if (user.equals(testUser) && password.equals(testPassword)) {
186
    	if (foruser != null) {
187
    		groups[0][1] = testGroup;
188
    		groups[0][1] = testGroupDesc;
189
    	} else if (foruser.equals(otherTestUser)) {
190
    		groups[0][0] = otherTestGroup;
191
    		groups[0][1] = otherTestGroupDesc;
192
    	}
193
    }
194

  
195
    return groups;
196

  
197
  }
198

  
199
  /**
200
   * Get attributes describing a user or group
201
   *
202
   * @param foruser the user for which the attribute list is requested
203
   * @returns HashMap a map of attribute name to a Vector of values
204
   */
205
  public HashMap<String,Vector<String>> getAttributes(String foruser) throws ConnectException {
206
    return getAttributes(null, null, foruser);
207
  }
208

  
209
  /**
210
   * Get attributes describing a user or group
211
   *
212
   * @param user the user for authenticating against the service
213
   * @param password the password for authenticating against the service
214
   * @param foruser the user whose attributes should be returned
215
   * @returns HashMap a map of attribute name to a Vector of values
216
   */
217
  public HashMap<String,Vector<String>> getAttributes(String user, String password, 
218
    String foruser) throws ConnectException {
219
    HashMap<String,Vector<String>> attributes = new HashMap<String,Vector<String>>();
220
    
221
    if (foruser.equals(otherTestUser)) {
222
    	Vector<String> attributeValues = new Vector<String>();
223
    	attributeValues.add(attributeValue1);
224
    	attributeValues.add(attributeValue2);
225
    	
226
    	attributes.put(attributeName, attributeValues);
227
    }
228

  
229
    return attributes;
230
  }
231

  
232
  /**
233
   * Get all groups and users from authentication scheme.
234
   * The output is formatted in XML.
235
   * @param user the user which requests the information
236
   * @param password the user's password
237
   */
238
  public String getPrincipals(String user, String password) throws 
239
    ConnectException {
240
    String out = new String();
241
   
242
    out += "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
243
    out += "<principals>\n";
244
    out += "  <authSystem URI=\"" + authUrl +">\n";
245
    out += "    <group>\n";
246
    out += "      <groupname>" + testGroup + "</groupname>\n";
247
    out += "      <description>" + testGroupDesc + "</description>\n";
248
    out += "      <user>\n";
249
    out += "        <username>" + testUser +"</username>\n";
250
    out += "        <name>" + testUserName +"</name>\n";
251
    out += "        <organization>" + testOrg + "</organization>\n";
252
    out += "        <organizationUnitName>" + testOrgUnit + "</organizationUnitName>\n";
253
    out += "        <email>" + testEmail + "</email>\n";
254
    out += "      </user>\n";
255
    out += "    </group>\n";
256
    out += "    <group>\n";
257
    out += "      <groupname>" + otherTestGroup + "</groupname>\n";
258
    out += "      <description>" + otherTestGroupDesc + "</description>\n";
259
    out += "      <user>\n";
260
    out += "        <username>" + otherTestUser +"</username>\n";
261
    out += "        <name>" + otherTestUserName +"</name>\n";
262
    out += "        <organization>" + otherTestOrg + "</organization>\n";
263
    out += "        <organizationUnitName>" + otherTestOrgUnit + "</organizationUnitName>\n";
264
    out += "        <email>" + otherTestEmail + "</email>\n";
265
    out += "      </user>\n";
266
    out += "    </group>\n";
267
    out += "  </authSystem>\n";
268
    out += "</principals>";
269
    
270
    return out;
271
  }
272
}
0 273

  

Also available in: Unified diff