Example usage for com.squareup.okhttp Credentials basic

List of usage examples for com.squareup.okhttp Credentials basic

Introduction

In this page you can find the example usage for com.squareup.okhttp Credentials basic.

Prototype

public static String basic(String userName, String password) 

Source Link

Document

Returns an auth credential for the Basic scheme.

Usage

From source file:org.eyeseetea.malariacare.network.ServerAPIController.java

License:Open Source License

BasicAuthenticator() {
    credentials = Credentials.basic(ServerAPIController.DHIS_USERNAME, ServerAPIController.DHIS_PASSWORD);
}

From source file:org.hawkular.agent.commandcli.CommandCli.java

License:Apache License

private static CliWebSocketListener sendCommand(Config config) throws Exception {

    OkHttpClient httpClient = new OkHttpClient();
    httpClient.setConnectTimeout(10, TimeUnit.SECONDS);
    httpClient.setReadTimeout(5, TimeUnit.MINUTES);

    Request request = new Request.Builder().url(config.serverUrl)
            .addHeader("Authorization", Credentials.basic(config.username, config.password))
            .addHeader("Accept", "application/json").build();

    WebSocketCall wsc = WebSocketCall.create(httpClient, request);
    CliWebSocketListener listener = new CliWebSocketListener(httpClient, wsc, config);
    return listener;
}

From source file:org.hawkular.cmdgw.ws.test.EchoCommandITest.java

License:Apache License

@RunAsClient
@Test(groups = { GROUP })//  w w w. j  a va2  s. c  o  m
public void testBadPassword() throws Throwable {
    try (TestWebSocketClient testClient = TestWebSocketClient.builder().url(ClientConfig.baseGwUri + "/ui/ws") //
            .authentication(Credentials.basic(ClientConfig.testUser, "bad password")) //
            .expectMessage(ExpectedFailure.UNAUTHORIZED) //
            .build()) {
        testClient.validate(10000);
    }
}

From source file:org.hawkular.cmdgw.ws.test.EchoCommandITest.java

License:Apache License

@RunAsClient
@Test(groups = { GROUP })/*from  w w  w.  j a  v a  2  s . co  m*/
public void testBadUserAndPassword() throws Throwable {
    try (TestWebSocketClient testClient = TestWebSocketClient.builder().url(ClientConfig.baseGwUri + "/ui/ws") //
            .authentication(Credentials.basic("baduser", "bad password")) //
            .expectMessage(ExpectedFailure.UNAUTHORIZED) //
            .build()) {
        testClient.validate(10000);
    }
}

From source file:org.openlmis.core.network.LMISRestManager.java

License:Open Source License

@NonNull
private RequestInterceptor getRequestInterceptor() {
    return new RequestInterceptor() {
        @Override/*from   ww  w . j  a va2  s.  com*/
        public void intercept(RequestFacade request) {
            User user = UserInfoMgr.getInstance().getUser();
            if (user != null) {
                String basic = Credentials.basic(user.getUsername(), user.getPassword());
                request.addHeader("Authorization", basic);
                request.addHeader("UserName", user.getUsername());
                request.addHeader("FacilityName", user.getFacilityName());
            }
            addDeviceInfoToRequestHeader(request);
        }
    };
}

From source file:org.sonarqube.ws.client.HttpConnector.java

License:Open Source License

private HttpConnector(Builder builder, JavaVersion javaVersion) {
    this.baseUrl = HttpUrl.parse(builder.url.endsWith("/") ? builder.url : format("%s/", builder.url));
    this.userAgent = builder.userAgent;

    if (isNullOrEmpty(builder.login)) {
        // no login nor access token
        this.credentials = null;
    } else {/*  ww  w  .  jav  a  2s  .  com*/
        // password is null when login represents an access token. In this case
        // the Basic credentials consider an empty password.
        this.credentials = Credentials.basic(builder.login, nullToEmpty(builder.password));
    }

    if (builder.proxy != null) {
        this.okHttpClient.setProxy(builder.proxy);
    }
    // proxy credentials can be used on system-wide proxies, so even if builder.proxy is null
    if (isNullOrEmpty(builder.proxyLogin)) {
        this.proxyCredentials = null;
    } else {
        this.proxyCredentials = Credentials.basic(builder.proxyLogin, nullToEmpty(builder.proxyPassword));
    }

    this.okHttpClient.setConnectTimeout(builder.connectTimeoutMs, TimeUnit.MILLISECONDS);
    this.okHttpClient.setReadTimeout(builder.readTimeoutMs, TimeUnit.MILLISECONDS);

    ConnectionSpec tls = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS).allEnabledTlsVersions()
            .allEnabledCipherSuites().supportsTlsExtensions(true).build();
    this.okHttpClient.setConnectionSpecs(asList(tls, ConnectionSpec.CLEARTEXT));
    if (javaVersion.isJava7()) {
        // OkHttp executes SSLContext.getInstance("TLS") by default (see
        // https://github.com/square/okhttp/blob/c358656/okhttp/src/main/java/com/squareup/okhttp/OkHttpClient.java#L616)
        // As only TLS 1.0 is enabled by default in Java 7, the SSLContextFactory must be changed
        // in order to support all versions from 1.0 to 1.2.
        // Note that this is not overridden for Java 8 as TLS 1.2 is enabled by default.
        // Keeping getInstance("TLS") allows to support potential future versions of TLS on Java 8.
        try {
            this.okHttpClient.setSslSocketFactory(
                    new Tls12Java7SocketFactory((SSLSocketFactory) SSLSocketFactory.getDefault()));
        } catch (Exception e) {
            throw new IllegalStateException("Fail to init TLS context", e);
        }
    }
}

From source file:org.xbmc.kore.jsonrpc.HostConnection.java

License:Open Source License

/**
 * Initializes this class OkHttpClient/*from www . j  av a  2 s . c  o  m*/
 */
public OkHttpClient getOkHttpClient() {
    if (httpClient == null) {
        httpClient = new OkHttpClient();
        httpClient.setConnectTimeout(connectTimeout, TimeUnit.MILLISECONDS);

        httpClient.setAuthenticator(new Authenticator() {
            @Override
            public Request authenticate(Proxy proxy, Response response) throws IOException {
                if (TextUtils.isEmpty(hostInfo.getUsername()))
                    return null;

                String credential = Credentials.basic(hostInfo.getUsername(), hostInfo.getPassword());
                return response.request().newBuilder().header("Authorization", credential).build();
            }

            @Override
            public Request authenticateProxy(Proxy proxy, Response response) throws IOException {
                return null;
            }
        });
    }
    return httpClient;
}