Skip to content

Commit

Permalink
Verify user can read the target database
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Newson committed Feb 22, 2012
1 parent 37ec6ae commit d12215c
Showing 1 changed file with 5 additions and 48 deletions.
53 changes: 5 additions & 48 deletions src/main/java/com/github/rnewson/couchdb/lucene/CouchDbRealm.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.IOException;
import java.security.Principal;
import java.util.Arrays;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.HierarchicalINIConfiguration;
Expand All @@ -17,43 +16,11 @@
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.mortbay.jetty.Request;
import org.mortbay.jetty.security.UserRealm;

public class CouchDbRealm implements UserRealm {

private class CouchDbPrincipal implements Principal {

private final String name;
private final JSONArray roles;

public CouchDbPrincipal(final String name, final JSONArray roles) {
this.name = name;
this.roles = roles;
}

public String getName() {
return name;
}

public boolean isInRole(final String role) {
for (int i = 0; i < roles.length(); i++) {
try {
if (role.equals(roles.getString(i)))
return true;
} catch (JSONException e) {
// ignored.
}
}
return false;
}

}

private final HttpClient client;

private final HierarchicalINIConfiguration ini;
Expand All @@ -74,7 +41,8 @@ public Principal getPrincipal(String username) {

public Principal authenticate(final String username,
final Object credentials, final Request req) {
final String sectionName = new PathParts(req).getKey();
final PathParts parts = new PathParts(req);
final String sectionName = parts.getKey();
final Configuration section = ini.getSection(sectionName);
if (!section.containsKey("url")) {
return null;
Expand All @@ -83,7 +51,7 @@ public Principal authenticate(final String username,
String url = section.getString("url");
url = url.endsWith("/") ? url : url + "/";

final HttpGet get = new HttpGet(url + "_session");
final HttpGet get = new HttpGet(url + parts.getDatabaseName());
final UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
username, (String) credentials);
final Header auth = BasicScheme.authenticate(creds,
Expand All @@ -96,18 +64,7 @@ public Principal authenticate(final String username,
public Principal handleResponse(final HttpResponse response)
throws ClientProtocolException, IOException {
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
final String body = EntityUtils.toString(response
.getEntity());
try {
final JSONObject json = new JSONObject(body);
final JSONObject userCtx = json
.getJSONObject("userCtx");
final JSONArray roles = userCtx
.getJSONArray("roles");
return new CouchDbPrincipal(username, roles);
} catch (final JSONException e) {
return null;
}
return new BasicUserPrincipal(username);
}
return null;
}
Expand All @@ -124,7 +81,7 @@ public boolean reauthenticate(Principal user) {
}

public boolean isUserInRole(Principal user, String role) {
return ((CouchDbPrincipal) user).isInRole(role);
return true;
}

public void disassociate(Principal user) {
Expand Down

0 comments on commit d12215c

Please sign in to comment.