Skip to content
This repository
Browse code

Verify user can read the target database

  • Loading branch information...
commit d12215c4f8cd470032c3c4b0cafb5af5df5c424b 1 parent 37ec6ae
authored
53  src/main/java/com/github/rnewson/couchdb/lucene/CouchDbRealm.java
@@ -2,7 +2,6 @@
2 2
 
3 3
 import java.io.IOException;
4 4
 import java.security.Principal;
5  
-import java.util.Arrays;
6 5
 
7 6
 import org.apache.commons.configuration.Configuration;
8 7
 import org.apache.commons.configuration.HierarchicalINIConfiguration;
@@ -17,43 +16,11 @@
17 16
 import org.apache.http.client.methods.HttpGet;
18 17
 import org.apache.http.impl.auth.BasicScheme;
19 18
 import org.apache.http.protocol.HTTP;
20  
-import org.apache.http.util.EntityUtils;
21  
-import org.json.JSONArray;
22  
-import org.json.JSONException;
23  
-import org.json.JSONObject;
24 19
 import org.mortbay.jetty.Request;
25 20
 import org.mortbay.jetty.security.UserRealm;
26 21
 
27 22
 public class CouchDbRealm implements UserRealm {
28 23
 
29  
-	private class CouchDbPrincipal implements Principal {
30  
-
31  
-		private final String name;
32  
-		private final JSONArray roles;
33  
-
34  
-		public CouchDbPrincipal(final String name, final JSONArray roles) {
35  
-			this.name = name;
36  
-			this.roles = roles;
37  
-		}
38  
-
39  
-		public String getName() {
40  
-			return name;
41  
-		}
42  
-
43  
-		public boolean isInRole(final String role) {
44  
-			for (int i = 0; i < roles.length(); i++) {
45  
-				try {
46  
-					if (role.equals(roles.getString(i)))
47  
-						return true;
48  
-				} catch (JSONException e) {
49  
-					// ignored.
50  
-				}
51  
-			}
52  
-			return false;
53  
-		}
54  
-
55  
-	}
56  
-
57 24
 	private final HttpClient client;
58 25
 
59 26
 	private final HierarchicalINIConfiguration ini;
@@ -74,7 +41,8 @@ public Principal getPrincipal(String username) {
74 41
 
75 42
 	public Principal authenticate(final String username,
76 43
 			final Object credentials, final Request req) {
77  
-		final String sectionName = new PathParts(req).getKey();
  44
+		final PathParts parts = new PathParts(req);
  45
+		final String sectionName = parts.getKey();
78 46
 		final Configuration section = ini.getSection(sectionName);
79 47
 		if (!section.containsKey("url")) {
80 48
 			return null;
@@ -83,7 +51,7 @@ public Principal authenticate(final String username,
83 51
 		String url = section.getString("url");
84 52
 		url = url.endsWith("/") ? url : url + "/";
85 53
 
86  
-		final HttpGet get = new HttpGet(url + "_session");
  54
+		final HttpGet get = new HttpGet(url + parts.getDatabaseName());
87 55
 		final UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
88 56
 				username, (String) credentials);
89 57
 		final Header auth = BasicScheme.authenticate(creds,
@@ -96,18 +64,7 @@ public Principal authenticate(final String username,
96 64
 				public Principal handleResponse(final HttpResponse response)
97 65
 						throws ClientProtocolException, IOException {
98 66
 					if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
99  
-						final String body = EntityUtils.toString(response
100  
-								.getEntity());
101  
-						try {
102  
-							final JSONObject json = new JSONObject(body);
103  
-							final JSONObject userCtx = json
104  
-									.getJSONObject("userCtx");
105  
-							final JSONArray roles = userCtx
106  
-									.getJSONArray("roles");
107  
-							return new CouchDbPrincipal(username, roles);
108  
-						} catch (final JSONException e) {
109  
-							return null;
110  
-						}						
  67
+						return new BasicUserPrincipal(username);
111 68
 					}
112 69
 					return null;
113 70
 				}
@@ -124,7 +81,7 @@ public boolean reauthenticate(Principal user) {
124 81
 	}
125 82
 
126 83
 	public boolean isUserInRole(Principal user, String role) {
127  
-		return ((CouchDbPrincipal) user).isInRole(role);
  84
+		return true;
128 85
 	}
129 86
 
130 87
 	public void disassociate(Principal user) {

0 notes on commit d12215c

Please sign in to comment.
Something went wrong with that request. Please try again.