Search in sources :

Example 41 with UsernamePasswordCredentials

use of org.apache.http.auth.UsernamePasswordCredentials in project jena by apache.

the class TestFusekiTestAuth method testServer_auth_bad_user.

@Test(expected = HttpException.class)
public void testServer_auth_bad_user() {
    BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
    Credentials credentials = new UsernamePasswordCredentials("USERUSER", PASSWORD);
    credsProvider.setCredentials(AuthScope.ANY, credentials);
    HttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    try (TypedInputStream in = HttpOp.execHttpGet(FusekiTestAuth.urlDataset(), "*/*", client, null)) {
    } catch (HttpException ex) {
        throw assertAuthHttpException(ex);
    }
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpClient(org.apache.http.client.HttpClient) HttpException(org.apache.jena.atlas.web.HttpException) FusekiTestAuth.assertAuthHttpException(org.apache.jena.fuseki.embedded.FusekiTestAuth.assertAuthHttpException) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Test(org.junit.Test)

Example 42 with UsernamePasswordCredentials

use of org.apache.http.auth.UsernamePasswordCredentials in project karaf by apache.

the class SyncopeLoginModule method login.

public boolean login() throws LoginException {
    Callback[] callbacks = new Callback[2];
    callbacks[0] = new NameCallback("Username: ");
    callbacks[1] = new PasswordCallback("Password: ", false);
    try {
        callbackHandler.handle(callbacks);
    } catch (IOException ioException) {
        throw new LoginException(ioException.getMessage());
    } catch (UnsupportedCallbackException unsupportedCallbackException) {
        throw new LoginException(unsupportedCallbackException.getMessage() + " not available to obtain information from user.");
    }
    user = ((NameCallback) callbacks[0]).getName();
    char[] tmpPassword = ((PasswordCallback) callbacks[1]).getPassword();
    if (tmpPassword == null) {
        tmpPassword = new char[0];
    }
    String password = new String(tmpPassword);
    principals = new HashSet<>();
    // authenticate the user on Syncope
    LOGGER.debug("Authenticate user {} on Syncope located {}", user, address);
    DefaultHttpClient client = new DefaultHttpClient();
    Credentials creds = new UsernamePasswordCredentials(user, password);
    client.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);
    HttpGet get = new HttpGet(address + "/users/self");
    get.setHeader("Content-Type", "application/xml");
    List<String> roles = new ArrayList<>();
    try {
        CloseableHttpResponse response = client.execute(get);
        LOGGER.debug("Syncope HTTP response status code: {}", response.getStatusLine().getStatusCode());
        if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            LOGGER.warn("User {} not authenticated", user);
            return false;
        }
        LOGGER.debug("User {} authenticated", user);
        LOGGER.debug("Populating principals with user");
        principals.add(new UserPrincipal(user));
        LOGGER.debug("Retrieving user {} roles", user);
        roles = extractingRoles(EntityUtils.toString(response.getEntity()));
    } catch (Exception e) {
        LOGGER.error("User {} authentication failed", user, e);
        throw new LoginException("User " + user + " authentication failed: " + e.getMessage());
    }
    LOGGER.debug("Populating principals with roles");
    for (String role : roles) {
        principals.add(new RolePrincipal(role));
    }
    return true;
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) IOException(java.io.IOException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) UserPrincipal(org.apache.karaf.jaas.boot.principal.UserPrincipal) LoginException(javax.security.auth.login.LoginException) IOException(java.io.IOException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) LoginException(javax.security.auth.login.LoginException) RolePrincipal(org.apache.karaf.jaas.boot.principal.RolePrincipal) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Credentials(org.apache.http.auth.Credentials)

Example 43 with UsernamePasswordCredentials

use of org.apache.http.auth.UsernamePasswordCredentials in project jena by apache.

the class TestAuth method withBasicAuth.

private static HttpClient withBasicAuth(AuthScope scope, String user, String passwd) {
    BasicCredentialsProvider provider = new BasicCredentialsProvider();
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(user, passwd);
    provider.setCredentials(scope, credentials);
    return HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 44 with UsernamePasswordCredentials

use of org.apache.http.auth.UsernamePasswordCredentials in project jena by apache.

the class TestRemoteEndpointConnectionWithAuth method setup.

/**
     * Setup for the tests by allocating a Fuseki instance to work with
     * @throws IOException 
     */
@BeforeClass
public static void setup() throws IOException {
    SecurityHandler sh = FusekiTestAuth.makeSimpleSecurityHandler("/*", USER, PASSWORD);
    FusekiTestAuth.setupServer(true, sh);
    BasicCredentialsProvider credsProv = new BasicCredentialsProvider();
    credsProv.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(USER, PASSWORD));
    client = HttpClients.custom().setDefaultCredentialsProvider(credsProv).build();
}
Also used : SecurityHandler(org.eclipse.jetty.security.SecurityHandler) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) BeforeClass(org.junit.BeforeClass)

Example 45 with UsernamePasswordCredentials

use of org.apache.http.auth.UsernamePasswordCredentials in project wildfly by wildfly.

the class JASPIHttpSchemeServerAuthModelTestCase method createHttpClient.

private DefaultHttpClient createHttpClient(final URL webAppURL, final String userName, final String userPassword) {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(userName, userPassword);
    httpClient.getCredentialsProvider().setCredentials(new AuthScope(webAppURL.getHost(), webAppURL.getPort(), DEPLOYMENT_REALM_NAME), credentials);
    return httpClient;
}
Also used : AuthScope(org.apache.http.auth.AuthScope) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Aggregations

UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)134 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)70 AuthScope (org.apache.http.auth.AuthScope)63 CredentialsProvider (org.apache.http.client.CredentialsProvider)55 HttpGet (org.apache.http.client.methods.HttpGet)28 HttpHost (org.apache.http.HttpHost)27 IOException (java.io.IOException)21 Test (org.junit.Test)21 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)20 HttpResponse (org.apache.http.HttpResponse)19 Credentials (org.apache.http.auth.Credentials)18 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)16 HttpClient (org.apache.http.client.HttpClient)14 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)14 BasicScheme (org.apache.http.impl.auth.BasicScheme)12 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)11 AuthCache (org.apache.http.client.AuthCache)10 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)10 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)10 BasicHeader (org.apache.http.message.BasicHeader)9