a | b | |
---|
| 0 | + | diff --git a/src/main/java/com/github/rnewson/couchdb/lucene/CouchDBUserRealm.java b/src/main/java/com/github/rnewson/couchdb/lucene/CouchDBUserRealm.java |
---|
| 0 | + | new file mode 100644 |
---|
| 0 | + | index 0000000..f691b9c |
---|
| 0 | + | --- /dev/null |
---|
| 0 | + | +++ b/src/main/java/com/github/rnewson/couchdb/lucene/CouchDBUserRealm.java |
---|
| 0 | + | @@ -0,0 +1,56 @@ |
---|
| 0 | + | +package com.github.rnewson.couchdb.lucene; |
---|
| 0 | + | + |
---|
| 0 | + | +import java.security.Principal; |
---|
| 0 | + | + |
---|
| 0 | + | +import org.mortbay.jetty.Request; |
---|
| 0 | + | +import org.mortbay.jetty.security.UserRealm; |
---|
| 0 | + | + |
---|
| 0 | + | +import com.github.rnewson.couchdb.lucene.util.ErrorPreservingResponseHandler; |
---|
| 0 | + | + |
---|
| 0 | + | +final class CouchDBUserRealm implements UserRealm { |
---|
| 0 | + | + |
---|
| 0 | + | + private final Config config; |
---|
| 0 | + | + |
---|
| 0 | + | + public CouchDBUserRealm(final Config config) { |
---|
| 0 | + | + this.config = config; |
---|
| 0 | + | + } |
---|
| 0 | + | + |
---|
| 0 | + | + public Principal authenticate(final String username, |
---|
| 0 | + | + final Object credentials, final Request request) { |
---|
| 0 | + | + final HttpGet get = new HttpGet(config.) |
---|
| 0 | + | + return httpClient.execute(request, new ErrorPreservingResponseHandler()); |
---|
| 0 | + | + throw new UnsupportedOperationException("authenticate not supported!"); |
---|
| 0 | + | + } |
---|
| 0 | + | + |
---|
| 0 | + | + public void disassociate(final Principal user) { |
---|
| 0 | + | + } |
---|
| 0 | + | + |
---|
| 0 | + | + public String getName() { |
---|
| 0 | + | + return "CouchDB-Lucene"; |
---|
| 0 | + | + } |
---|
| 0 | + | + |
---|
| 0 | + | + public Principal getPrincipal(final String username) { |
---|
| 0 | + | + throw new UnsupportedOperationException("getPrincipal not supported!"); |
---|
| 0 | + | + } |
---|
| 0 | + | + |
---|
| 0 | + | + public boolean isUserInRole(final Principal user, final String role) { |
---|
| 0 | + | + throw new UnsupportedOperationException("isUserInRole not supported!"); |
---|
| 0 | + | + } |
---|
| 0 | + | + |
---|
| 0 | + | + public void logout(final Principal user) { |
---|
| 0 | + | + throw new UnsupportedOperationException("logout not supported!"); |
---|
| 0 | + | + } |
---|
| 0 | + | + |
---|
| 0 | + | + public Principal popRole(final Principal user) { |
---|
| 0 | + | + throw new UnsupportedOperationException("popRole not supported!"); |
---|
| 0 | + | + } |
---|
| 0 | + | + |
---|
| 0 | + | + public Principal pushRole(final Principal user, final String role) { |
---|
| 0 | + | + throw new UnsupportedOperationException("pushRole not supported!"); |
---|
| 0 | + | + } |
---|
| 0 | + | + |
---|
| 0 | + | + public boolean reauthenticate(final Principal user) { |
---|
| 0 | + | + throw new UnsupportedOperationException("reauthenticate not supported!"); |
---|
| 0 | + | + } |
---|
| 0 | + | + |
---|
| 0 | + | +} |
---|
| 0 | + | diff --git a/src/main/java/com/github/rnewson/couchdb/lucene/Main.java b/src/main/java/com/github/rnewson/couchdb/lucene/Main.java |
---|
| 0 | + | index b209b9d..55b1fe3 100644 |
---|
| 0 | + | --- a/src/main/java/com/github/rnewson/couchdb/lucene/Main.java |
---|
| 0 | + | +++ b/src/main/java/com/github/rnewson/couchdb/lucene/Main.java |
---|
| 0 | + | @@ -23,6 +23,10 @@ import org.mortbay.jetty.Connector; |
---|
| 0 | + | import org.mortbay.jetty.Handler; |
---|
| 0 | + | import org.mortbay.jetty.Server; |
---|
| 0 | + | import org.mortbay.jetty.nio.SelectChannelConnector; |
---|
| 0 | + | +import org.mortbay.jetty.security.Constraint; |
---|
| 0 | + | +import org.mortbay.jetty.security.ConstraintMapping; |
---|
| 0 | + | +import org.mortbay.jetty.security.SecurityHandler; |
---|
| 0 | + | +import org.mortbay.jetty.security.UserRealm; |
---|
| 0 | + | import org.mortbay.jetty.servlet.Context; |
---|
| 0 | + | import org.mortbay.jetty.servlet.FilterHolder; |
---|
| 0 | + | import org.mortbay.jetty.servlet.ServletHolder; |
---|
| 0 | + | @@ -52,11 +56,25 @@ public class Main { |
---|
| 0 | + | |
---|
| 0 | + | final LuceneServlet servlet = new LuceneServlet(config.getClient(), dir, config.getConfiguration()); |
---|
| 0 | + | |
---|
| 0 | + | - final Context context = new Context(server, "/", Context.NO_SESSIONS | Context.NO_SECURITY); |
---|
| 0 | + | + final Context context = new Context(server, "/"); |
---|
| 0 | + | context.addServlet(new ServletHolder(servlet), "/*"); |
---|
| 0 | + | context.addFilter(new FilterHolder(new GzipFilter()), "/*", Handler.DEFAULT); |
---|
| 0 | + | context.setErrorHandler(new JSONErrorHandler()); |
---|
| 0 | + | server.setHandler(context); |
---|
| 0 | + | + |
---|
| 0 | + | + final Constraint constraint = new Constraint(); |
---|
| 0 | + | + constraint.setName(Constraint.__BASIC_AUTH);; |
---|
| 0 | + | + constraint.setRoles(new String[]{"user","admin","moderator"}); |
---|
| 0 | + | + constraint.setAuthenticate(true); |
---|
| 0 | + | + |
---|
| 0 | + | + final ConstraintMapping constraintMapping = new ConstraintMapping(); |
---|
| 0 | + | + constraintMapping.setConstraint(constraint); |
---|
| 0 | + | + constraintMapping.setPathSpec("/*"); |
---|
| 0 | + | + |
---|
| 0 | + | + final SecurityHandler securityHandler = new SecurityHandler(); |
---|
| 0 | + | + securityHandler.setUserRealm(new CouchDBUserRealm(config)); |
---|
| 0 | + | + securityHandler.setConstraintMappings(new ConstraintMapping[]{constraintMapping}); |
---|
| 0 | + | + context.addHandler(securityHandler); |
---|
| 0 | + | |
---|
| 0 | + | server.start(); |
---|
| 0 | + | server.join(); |
---|
... | |
---|