Example usage for org.apache.http.auth Credentials getUserPrincipal

List of usage examples for org.apache.http.auth Credentials getUserPrincipal

Introduction

In this page you can find the example usage for org.apache.http.auth Credentials getUserPrincipal.

Prototype

Principal getUserPrincipal();

Source Link

Usage

From source file:com.microsoft.alm.plugin.authentication.AuthHelper.java

public static AuthenticationInfo createAuthenticationInfo(final String serverUri,
        final Credentials credentials) {
    return new AuthenticationInfo(credentials.getUserPrincipal().getName(), credentials.getPassword(),
            serverUri, credentials.getUserPrincipal().getName());
}

From source file:at.orz.arangodb.http.CURLLogger.java

public static void log(String url, HttpRequestEntity requestEntity, String userAgent, Credentials credencials) {

    boolean includeBody = (requestEntity.type == RequestType.POST || requestEntity.type == RequestType.PUT
            || requestEntity.type == RequestType.PATCH)
            && (requestEntity.bodyText != null && requestEntity.bodyText.length() != 0);

    StringBuilder buffer = new StringBuilder();

    if (includeBody) {
        buffer.append("\n");
        buffer.append("cat <<-___EOB___ | ");
    }//from   www  .j  a  v a 2  s . co m

    buffer.append("curl -X ").append(requestEntity.type);
    buffer.append(" --dump -");

    // header
    if (requestEntity.headers != null && !requestEntity.headers.isEmpty()) {
        for (Entry<String, Object> header : requestEntity.headers.entrySet()) {
            buffer.append(" -H '").append(header.getKey()).append(":").append(header.getValue()).append("'");
        }
    }

    // basic auth
    if (credencials != null) {
        buffer.append(" -u ").append(credencials.getUserPrincipal().getName()).append(":")
                .append(credencials.getPassword());
    }

    // user-agent
    //buffer.append(" -A '").append(userAgent).append("'");

    if (includeBody) {
        buffer.append(" -d @-");
    }

    buffer.append(" '").append(url).append("'");

    if (includeBody) {
        buffer.append("\n");
        buffer.append(requestEntity.bodyText);
        buffer.append("\n");
        buffer.append("___EOB___");
    }

    logger.debug("[CURL]{}", buffer);

}

From source file:com.arangodb.http.CURLLogger.java

public static void log(String url, HttpRequestEntity requestEntity, String userAgent, Credentials credencials) {

    boolean includeBody = (requestEntity.type == RequestType.POST || requestEntity.type == RequestType.PUT
            || requestEntity.type == RequestType.PATCH)
            && (requestEntity.bodyText != null && requestEntity.bodyText.length() != 0);

    StringBuilder buffer = new StringBuilder();

    if (includeBody) {
        buffer.append("\n");
        buffer.append("cat <<-___EOB___ | ");
    }//ww w .j a v a  2  s . c o m

    buffer.append("curl -X ").append(requestEntity.type);
    buffer.append(" --dump -");

    // header
    if (requestEntity.headers != null && !requestEntity.headers.isEmpty()) {
        for (Entry<String, Object> header : requestEntity.headers.entrySet()) {
            buffer.append(" -H '").append(header.getKey()).append(":").append(header.getValue()).append("'");
        }
    }

    // basic auth
    if (credencials != null) {
        buffer.append(" -u ").append(credencials.getUserPrincipal().getName()).append(":")
                .append(credencials.getPassword());
    }

    // user-agent
    // buffer.append(" -A '").append(userAgent).append("'");

    if (includeBody) {
        buffer.append(" -d @-");
    }

    buffer.append(" '").append(url).append("'");

    if (includeBody) {
        buffer.append("\n");
        buffer.append(requestEntity.bodyText);
        buffer.append("\n");
        buffer.append("___EOB___");
    }

    logger.debug("[CURL]{}", buffer);

}

From source file:org.opentravel.schemacompiler.repository.impl.NTLMSystemCredentialsProvider.java

private NTCredentials traslateToNTLMCredentials(Credentials credentials) {
    String fullUserName = credentials.getUserPrincipal().getName();
    String[] tokens = fullUserName.split("\\\\", 2);
    String userName = "";
    String domain = null;//from   w  ww.  j a  va  2  s  .  c o m
    if (tokens.length == 2) {
        if (tokens[0] != null && !tokens[0].isEmpty()) {
            domain = tokens[0];
        }
        userName = tokens[1];
    } else {
        userName = tokens[0];
    }
    String workstation = null; // how to support workstation ???
    return new NTCredentials(userName, credentials.getPassword(), workstation, domain);
}

From source file:bad.robot.http.apache.BearerScheme.java

@Override
public Header authenticate(Credentials credentials, HttpRequest request) throws AuthenticationException {
    return new BasicHeader("Authorization", "Bearer " + credentials.getUserPrincipal().getName());
}

From source file:com.cisco.cta.taxii.adapter.httpclient.CredentialsProviderFactoryTest.java

@Test
public void proxyWithBasicAuth() throws MalformedURLException {
    ProxySettings proxySettings = new ProxySettings();
    proxySettings.setAuthenticationType(ProxyAuthenticationType.BASIC);
    proxySettings.setUrl(new URL("http://localhost:8001/"));
    proxySettings.setUsername("tester");
    proxySettings.setPassword("testPass");
    CredentialsProviderFactory factory = new CredentialsProviderFactory(taxiiSettings, proxySettings);
    CredentialsProvider credsProvider = factory.build();
    Credentials creds = credsProvider.getCredentials(new AuthScope("localhost", 8001));
    assertThat(creds.getUserPrincipal().getName(), is("tester"));
    assertThat(creds.getPassword(), is("testPass"));
}

From source file:com.cisco.cta.taxii.adapter.httpclient.CredentialsProviderFactoryTest.java

@Test
public void proxyWithNTLMAuth() throws MalformedURLException {
    ProxySettings proxySettings = new ProxySettings();
    proxySettings.setAuthenticationType(ProxyAuthenticationType.NTLM);
    proxySettings.setUrl(new URL("http://localhost:8001/"));
    proxySettings.setUsername("tester");
    proxySettings.setPassword("testPass");
    CredentialsProviderFactory factory = new CredentialsProviderFactory(taxiiSettings, proxySettings);
    CredentialsProvider credsProvider = factory.build();
    Credentials creds = credsProvider.getCredentials(new AuthScope("localhost", 8001));
    assertThat(creds.getUserPrincipal().getName(), is("tester"));
    assertThat(creds.getPassword(), is("testPass"));
}

From source file:org.apache.nifi.hadoop.KerberosKeytabSPNegoScheme.java

@Override
public byte[] generateToken(byte[] input, String authServer, Credentials credentials) {
    Set<Principal> principals = new HashSet<>();
    principals.add(credentials.getUserPrincipal());
    Subject subject = new Subject(false, principals, new HashSet<>(), new HashSet<>());

    try {/*w  ww  . jav a2 s.co  m*/
        LoginContext loginContext = new LoginContext("", subject, null,
                new KerberosConfiguration(credentials.getUserPrincipal().getName(),
                        ((KerberosKeytabCredentials) credentials).getKeytab()));
        loginContext.login();
        Subject loggedInSubject = loginContext.getSubject();

        return Subject.doAs(loggedInSubject, new PrivilegedExceptionAction<byte[]>() {

            public byte[] run() throws UnknownHostException, ClassNotFoundException, GSSException,
                    IllegalAccessException, NoSuchFieldException {
                GSSManager gssManager = GSSManager.getInstance();
                String servicePrincipal = KerberosUtil.getServicePrincipal("HTTP", authServer);
                Oid serviceOid = KerberosUtil.getOidInstance("NT_GSS_KRB5_PRINCIPAL");
                GSSName serviceName = gssManager.createName(servicePrincipal, serviceOid);
                Oid mechOid = KerberosUtil.getOidInstance("GSS_KRB5_MECH_OID");
                GSSContext gssContext = gssManager.createContext(serviceName, mechOid, null, 0);
                gssContext.requestCredDeleg(true);
                gssContext.requestMutualAuth(true);
                return gssContext.initSecContext(input, 0, input.length);
            }

        });
    } catch (PrivilegedActionException | LoginException e) {
        throw new RuntimeException(e);
    }
}

From source file:de.unioninvestment.eai.portal.portlet.crud.domain.model.authentication.RealmTest.java

@Test
public void shouldApplySettingsToHttpClient() {
    when(cryptorFactoryMock.getCryptor("pgp")).thenReturn(cryptorMock);
    AuthenticationRealmConfig config = createRealmConfig();
    when(preferencesMock.getValue("userKey", null)).thenReturn("user1");
    when(preferencesMock.getValue("passKey", null)).thenReturn("pass1");
    when(cryptorMock.decrypt("pass1")).thenReturn("pass2");
    Realm realm = new Realm(config, cryptorFactoryMock);

    DefaultHttpClient httpClient = new DefaultHttpClient();
    realm.applyBasicAuthentication(httpClient);

    Credentials credentials = httpClient.getCredentialsProvider().getCredentials(AuthScope.ANY);
    assertThat(credentials.getUserPrincipal().getName(), is("user1"));
    assertThat(credentials.getPassword(), is("pass2"));
}

From source file:org.janusgraph.diskstorage.es.rest.util.BasicAuthHttpClientConfigCallbackTest.java

@Test
public void testSetDefaultCredentialsProviderWithRealm() throws Exception {

    final CredentialsProvider cp = basicAuthTestBase(HTTP_REALM);

    // expected: will match any host in that specific realm
    final Credentials credentialsForRealm1 = cp.getCredentials(new AuthScope("dummyhost1", 1234, HTTP_REALM));
    assertEquals(HTTP_USER, credentialsForRealm1.getUserPrincipal().getName());
    assertEquals(HTTP_PASSWORD, credentialsForRealm1.getPassword());

    // ...but not in any other realms
    final Credentials credentialsForRealm3 = cp
            .getCredentials(new AuthScope("dummyhost1", 1234, "Not_" + HTTP_REALM));
    assertNull(credentialsForRealm3);//from w  ww.  j  a  v  a2  s.c o  m
}