Search in sources :

Example 6 with AuthPolicy

use of org.apache.jena.fuseki.auth.AuthPolicy in project jena by apache.

the class TestAuthorized method auth_anyLoggedIn_2.

@Test
public void auth_anyLoggedIn_2() {
    AuthPolicy auth = Auth.policyAllowSpecific("*");
    assertFalse(auth.isAllowed(null));
    assertTrue(auth.isAllowed("user1"));
}
Also used : AuthPolicy(org.apache.jena.fuseki.auth.AuthPolicy) Test(org.junit.Test)

Example 7 with AuthPolicy

use of org.apache.jena.fuseki.auth.AuthPolicy in project jena by apache.

the class TestSecurityBuilderSetup method beforeClass.

@BeforeClass
public static void beforeClass() {
    int port = WebLib.choosePort();
    authSetup1 = new AuthSetup("localhost", port, "user1", "pw1", "TripleStore");
    authSetup2 = new AuthSetup("localhost", port, "user2", "pw2", "TripleStore");
    authSetupX = new AuthSetup("localhost", port, "userX", "pwX", "TripleStore");
    // Two authorized users.
    UserStore userStore = new UserStore();
    JettyLib.addUser(userStore, authSetup1.user, authSetup1.password);
    JettyLib.addUser(userStore, authSetup2.user, authSetup2.password);
    try {
        userStore.start();
    } catch (Exception ex) {
        throw new RuntimeException("UserStore", ex);
    }
    ConstraintSecurityHandler sh = JettyLib.makeSecurityHandler(authSetup1.realm, userStore);
    // Secure these areas.
    // User needs to be logged in.
    JettyLib.addPathConstraint(sh, "/ds");
    // Allow auth control even through there isn't anything there
    JettyLib.addPathConstraint(sh, "/nowhere");
    // user1 only.
    JettyLib.addPathConstraint(sh, "/ctl");
    // Not controlled: "/open"
    AuthPolicy reqAuth = Auth.policyAllowSpecific("user1");
    DataService dSrv = DataService.newBuilder(DatasetGraphFactory.createTxnMem()).withStdServices(false).setAuthPolicy(reqAuth).build();
    fusekiServer = FusekiServer.create().port(port).add("/ds", DatasetFactory.createTxnMem()).add("/open", DatasetFactory.createTxnMem()).add("/ctl", dSrv).securityHandler(sh).build();
    fusekiServer.start();
    serverURL = fusekiServer.serverURL();
}
Also used : AuthSetup(org.apache.jena.web.AuthSetup) UserStore(org.eclipse.jetty.security.UserStore) AuthPolicy(org.apache.jena.fuseki.auth.AuthPolicy) ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler) HttpException(org.apache.jena.atlas.web.HttpException) DataService(org.apache.jena.fuseki.server.DataService) BeforeClass(org.junit.BeforeClass)

Example 8 with AuthPolicy

use of org.apache.jena.fuseki.auth.AuthPolicy in project jena by apache.

the class FusekiConfig method accEndpointOldStyle.

// Old style.
// fuseki:serviceQuery "sparql";
// or
// fuseki:serviceQuery [ fuseki:name "sparql" ; fuseki:allowedUsers (..) ];
private static void accEndpointOldStyle(Collection<Endpoint> endpoints, Operation operation, Resource svc, Property property) {
    String p = "<" + property.getURI() + ">";
    ResultSet rs = BuildLib.query("SELECT * { ?svc " + p + " ?ep}", svc.getModel(), "svc", svc);
    for (; rs.hasNext(); ) {
        QuerySolution soln = rs.next();
        // No policy yet - set below if one is found.
        AuthPolicy authPolicy = null;
        RDFNode ep = soln.get("ep");
        String endpointName = null;
        if (ep.isLiteral())
            // fuseki:serviceQuery "sparql"
            endpointName = soln.getLiteral("ep").getLexicalForm();
        else if (ep.isResource()) {
            Resource r = (Resource) ep;
            try {
                // [ fuseki:name ""; fuseki:allowedUsers ( "" "" ) ]
                Statement stmt = r.getProperty(FusekiVocab.pEndpointName);
                if (stmt == null)
                    throw new FusekiConfigException("Expected property <" + FusekiVocab.pEndpointName + "> with <" + property.getURI() + "> for <" + svc + ">");
                endpointName = stmt.getString();
                List<RDFNode> x = GraphUtils.multiValue(r, FusekiVocab.pAllowedUsers);
                if (x.size() > 1)
                    throw new FusekiConfigException("Multiple fuseki:" + FusekiVocab.pAllowedUsers.getLocalName() + " for " + r);
                if (!x.isEmpty())
                    authPolicy = allowedUsers(r);
            } catch (JenaException | ClassCastException ex) {
                throw new FusekiConfigException("Failed to parse endpoint: " + r);
            }
        } else {
            throw new FusekiConfigException("Unrecognized: " + ep);
        }
        if (StringUtils.isEmpty(endpointName))
            endpointName = null;
        Endpoint endpoint = Endpoint.create(operation, endpointName, authPolicy);
        endpoints.add(endpoint);
    }
}
Also used : FusekiConfigException(org.apache.jena.fuseki.FusekiConfigException) QuerySolution(org.apache.jena.query.QuerySolution) AuthPolicy(org.apache.jena.fuseki.auth.AuthPolicy) ResultSet(org.apache.jena.query.ResultSet) Collectors.toList(java.util.stream.Collectors.toList) AuthPolicyList(org.apache.jena.fuseki.auth.AuthPolicyList)

Example 9 with AuthPolicy

use of org.apache.jena.fuseki.auth.AuthPolicy in project jena by apache.

the class TestAuthorized method auth_parse_1.

@Test
public void auth_parse_1() {
    Resource r = model.createResource("http://example/r1");
    AuthPolicy auth = FusekiConfig.allowedUsers(r);
    assertNotNull(auth);
    assertFalse(auth.isAllowed(null));
    assertTrue(auth.isAllowed("user1"));
    assertTrue(auth.isAllowed("user2"));
    assertFalse(auth.isAllowed("user3"));
}
Also used : AuthPolicy(org.apache.jena.fuseki.auth.AuthPolicy) Resource(org.apache.jena.rdf.model.Resource) Test(org.junit.Test)

Example 10 with AuthPolicy

use of org.apache.jena.fuseki.auth.AuthPolicy in project jena by apache.

the class TestAuthorized method auth_anyLoggedIn_1.

@Test
public void auth_anyLoggedIn_1() {
    AuthPolicy auth = Auth.ANY_USER;
    assertFalse(auth.isAllowed(null));
    assertTrue(auth.isAllowed("user1"));
}
Also used : AuthPolicy(org.apache.jena.fuseki.auth.AuthPolicy) Test(org.junit.Test)

Aggregations

AuthPolicy (org.apache.jena.fuseki.auth.AuthPolicy)16 Test (org.junit.Test)10 Resource (org.apache.jena.rdf.model.Resource)5 FusekiConfigException (org.apache.jena.fuseki.FusekiConfigException)3 Collectors.toList (java.util.stream.Collectors.toList)2 FusekiException (org.apache.jena.fuseki.FusekiException)2 AuthPolicyList (org.apache.jena.fuseki.auth.AuthPolicyList)2 DataService (org.apache.jena.fuseki.server.DataService)2 ActionService (org.apache.jena.fuseki.servlets.ActionService)2 Node (org.apache.jena.graph.Node)2 QuerySolution (org.apache.jena.query.QuerySolution)2 ResultSet (org.apache.jena.query.ResultSet)2 Context (org.apache.jena.sparql.util.Context)2 File (java.io.File)1 IOException (java.io.IOException)1 String.format (java.lang.String.format)1 Method (java.lang.reflect.Method)1 DirectoryStream (java.nio.file.DirectoryStream)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1