Search in sources :

Example 26 with Authenticator

use of java.net.Authenticator in project jena by apache.

the class ExFuseki_06_DataAccessCtl method main.

public static void main(String... a) {
    FusekiLogging.setLogging();
    int port = WebLib.choosePort();
    String datasetName = "/ds";
    String URL = format("http://localhost:%d%s", port, datasetName);
    // ---- Set up the registry.
    AuthorizationService authorizeSvc;
    {
        SecurityRegistry reg = new SecurityRegistry();
        // user1 can see the default graph and :g1
        reg.put("user1", new SecurityContextView("http://example/g1", Quad.defaultGraphIRI.getURI()));
        // user2 can see :g1
        reg.put("user2", new SecurityContextView("http://example/g1"));
        // user3 can see :g1 and :g2
        reg.put("user3", new SecurityContextView("http://example/g1", "http://example/g2"));
        // Hide implementation.
        authorizeSvc = reg;
    }
    // ---- Some data
    DatasetGraph dsg = createData();
    // ---- User authentication database (Jetty specific)
    UserStore userStore = new UserStore();
    addUserPassword(userStore, "user1", "pw1", "**");
    addUserPassword(userStore, "user2", "pw2", "**");
    try {
        userStore.start();
    } catch (Exception ex) {
        throw new RuntimeException("UserStore", ex);
    }
    // ---- Build server, start server.
    FusekiServer server = fuseki(port, userStore, authorizeSvc, datasetName, dsg);
    server.start();
    // ---- HttpClient connection with user and password basic authentication.
    Authenticator authenticator = AuthLib.authenticator("user1", "pw1");
    HttpClient client = HttpClient.newBuilder().authenticator(authenticator).connectTimeout(Duration.ofSeconds(10)).build();
    // ---- Use it.
    try (RDFConnection conn = RDFConnectionRemote.newBuilder().destination(URL).httpClient(client).build()) {
        // What can we see of the database? user1 can see g1 and the default graph
        System.out.println("\nFetch dataset");
        Dataset ds1 = conn.fetchDataset();
        RDFDataMgr.write(System.out, ds1, RDFFormat.TRIG_FLAT);
        // Get a graph.
        System.out.println("\nFetch named graph");
        Model m1 = conn.fetch("http://example/g1");
        RDFDataMgr.write(System.out, m1, RDFFormat.TURTLE_FLAT);
        // Get a graph. user tries to get a graph they have no permission for ==> 404
        System.out.println("\nFetch unexistent named graph");
        try {
            Model m2 = conn.fetch("http://example/g2");
        } catch (HttpException ex) {
            System.out.println(ex.getMessage());
        }
    }
    // Need to exit the JVM : there is a background server
    System.exit(0);
}
Also used : Dataset(org.apache.jena.query.Dataset) SecurityContextView(org.apache.jena.fuseki.access.SecurityContextView) FusekiServer(org.apache.jena.fuseki.main.FusekiServer) HttpException(org.apache.jena.atlas.web.HttpException) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) UserStore(org.eclipse.jetty.security.UserStore) AuthorizationService(org.apache.jena.fuseki.access.AuthorizationService) RDFConnection(org.apache.jena.rdfconnection.RDFConnection) HttpClient(java.net.http.HttpClient) Model(org.apache.jena.rdf.model.Model) HttpException(org.apache.jena.atlas.web.HttpException) Authenticator(java.net.Authenticator) SecurityRegistry(org.apache.jena.fuseki.access.SecurityRegistry)

Example 27 with Authenticator

use of java.net.Authenticator in project jena by apache.

the class AbstractTestWebappAuth_JDK method withAuthJDK.

public static QueryExecutionHTTP withAuthJDK(QueryExecutionHTTPBuilder builder, String user, String passwd) {
    Authenticator authenticator = AuthLib.authenticator(user, passwd);
    HttpClient hc = HttpClient.newBuilder().authenticator(authenticator).build();
    return builder.httpClient(hc).build();
}
Also used : HttpClient(java.net.http.HttpClient) Authenticator(java.net.Authenticator)

Example 28 with Authenticator

use of java.net.Authenticator in project jena by apache.

the class ExAuth02_QueryExecutionPW method exampleQueryAuthWithHttpClient.

// HttpClient
public static void exampleQueryAuthWithHttpClient() {
    System.out.println();
    System.out.println("HttpClient + QueryExecutionHTTP");
    Authenticator authenticator = AuthLib.authenticator("u", "p");
    HttpClient httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).authenticator(authenticator).build();
    try (QueryExecution qexec = QueryExecutionHTTP.service(dataURL).httpClient(httpClient).endpoint(dataURL).queryString("ASK{}").build()) {
        qexec.execAsk();
    }
}
Also used : HttpClient(java.net.http.HttpClient) Authenticator(java.net.Authenticator) QueryExecution(org.apache.jena.query.QueryExecution)

Example 29 with Authenticator

use of java.net.Authenticator in project jena by apache.

the class ExAuth04_ServicePW method exampleServiceByHttpClient.

private static void exampleServiceByHttpClient() {
    System.out.println();
    System.out.println("Custom HttpClient + SERVICE call");
    Authenticator authenticator = AuthLib.authenticator("u", "p");
    HttpClient httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).authenticator(authenticator).build();
    Context cxt = ContextAccumulator.newBuilder().set(ARQ.httpQueryClient, httpClient).context();
    Query query = QueryFactory.create("SELECT * { SERVICE <" + dataURL + "> { ?s ?p ?o } }");
    Dataset emptyLocal = DatasetFactory.empty();
    try (QueryExecution qExec = QueryExecution.create().query(query).dataset(emptyLocal).context(cxt).build()) {
        System.out.println("Call using SERVICE...");
        ResultSet rs = qExec.execSelect();
        ResultSetFormatter.out(rs);
    }
}
Also used : Context(org.apache.jena.sparql.util.Context) HttpClient(java.net.http.HttpClient) Authenticator(java.net.Authenticator)

Example 30 with Authenticator

use of java.net.Authenticator in project hudson-2.x by hudson.

the class Launcher method run.

public void run() throws Exception {
    if (auth != null) {
        final int idx = auth.indexOf(':');
        if (idx < 0)
            throw new CmdLineException(null, "No ':' in the -auth option");
        Authenticator.setDefault(new Authenticator() {

            @Override
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(auth.substring(0, idx), auth.substring(idx + 1).toCharArray());
            }
        });
    }
    if (connectionTarget != null) {
        runAsTcpClient();
        System.exit(0);
    } else if (slaveJnlpURL != null) {
        List<String> jnlpArgs = parseJnlpArguments();
        try {
            hudson.remoting.jnlp.Main._main(jnlpArgs.toArray(new String[jnlpArgs.size()]));
        } catch (CmdLineException e) {
            System.err.println("JNLP file " + slaveJnlpURL + " has invalid arguments: " + jnlpArgs);
            System.err.println("Most likely a configuration error in the master");
            System.err.println(e.getMessage());
            System.exit(1);
        }
    } else if (tcpPortFile != null) {
        runAsTcpServer();
        System.exit(0);
    } else {
        runWithStdinStdout();
        System.exit(0);
    }
}
Also used : ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) CmdLineException(org.kohsuke.args4j.CmdLineException) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Aggregations

Authenticator (java.net.Authenticator)83 PasswordAuthentication (java.net.PasswordAuthentication)52 URL (java.net.URL)18 Proxy (java.net.Proxy)14 InetSocketAddress (java.net.InetSocketAddress)12 HttpClient (java.net.http.HttpClient)11 Field (java.lang.reflect.Field)10 HttpURLConnection (java.net.HttpURLConnection)10 IOException (java.io.IOException)8 Test (org.junit.Test)7 Method (java.lang.reflect.Method)6 File (java.io.File)5 SocketAddress (java.net.SocketAddress)5 InputStream (java.io.InputStream)4 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 ServerSocket (java.net.ServerSocket)3 URISyntaxException (java.net.URISyntaxException)3 ArrayList (java.util.ArrayList)3 Content (org.sonatype.goodies.httpfixture.server.jetty.behaviour.Content)3