Search in sources :

Example 46 with Credentials

use of okhttp3.Credentials in project instagram-java-scraper by postaddictme.

the class AuthenticatedInstaTest method setUp.

@BeforeClass
public static void setUp() throws Exception {
    Credentials credentials = new Credentials();
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
    OkHttpClient httpClient = new OkHttpClient.Builder().addNetworkInterceptor(loggingInterceptor).addInterceptor(new FakeBrowserInterceptor(UserAgents.OSX_CHROME)).addInterceptor(new ErrorInterceptor()).cookieJar(new DefaultCookieJar(new CookieHashSet())).build();
    client = new Instagram(httpClient);
    client.basePage();
    client.login(credentials.getLogin(), credentials.getPassword());
    client.basePage();
}
Also used : CookieHashSet(me.postaddict.instagram.scraper.cookie.CookieHashSet) OkHttpClient(okhttp3.OkHttpClient) ErrorInterceptor(me.postaddict.instagram.scraper.interceptor.ErrorInterceptor) DefaultCookieJar(me.postaddict.instagram.scraper.cookie.DefaultCookieJar) FakeBrowserInterceptor(me.postaddict.instagram.scraper.interceptor.FakeBrowserInterceptor) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) BeforeClass(org.junit.BeforeClass)

Example 47 with Credentials

use of okhttp3.Credentials in project java-cloudant by cloudant.

the class CloudantClientTests method testBasicAuthFromCredentials.

/**
 * Test that configuring the Basic Authentication interceptor from credentials and adding to
 * the CloudantClient works.
 */
@Test
public void testBasicAuthFromCredentials() throws Exception {
    BasicAuthInterceptor interceptor = BasicAuthInterceptor.createFromCredentials("username", "password");
    // send back a mock OK 200
    server.enqueue(new MockResponse());
    CloudantClient client = CloudantClientHelper.newMockWebServerClientBuilder(server).interceptors(interceptor).build();
    client.getAllDbs();
    // expected 'username:password'
    assertEquals("Basic dXNlcm5hbWU6cGFzc3dvcmQ=", server.takeRequest().getHeader("Authorization"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) BasicAuthInterceptor(com.cloudant.http.interceptors.BasicAuthInterceptor) CloudantClient(com.cloudant.client.api.CloudantClient) Test(org.junit.jupiter.api.Test)

Example 48 with Credentials

use of okhttp3.Credentials in project java-cloudant by cloudant.

the class HttpTest method badCredsCookieThrows.

/**
 * Test that cookie authentication throws a CouchDbException if the credentials were bad.
 *
 * @throws Exception
 */
@TestTemplate
public void badCredsCookieThrows() {
    mockWebServer.enqueue(new MockResponse().setResponseCode(401));
    CloudantClient c = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).username("bad").password("worse").build();
    CouchDbException re = assertThrows(CouchDbException.class, () -> c.executeRequest(Http.GET(c.getBaseUri())).responseAsString(), "Bad credentials should throw a CouchDbException.");
    assertTrue(re.getMessage().startsWith("401 Credentials are incorrect for server"), "The " + "exception should have been for bad creds.");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) CouchDbException(com.cloudant.client.org.lightcouch.CouchDbException) CloudantClient(com.cloudant.client.api.CloudantClient) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 49 with Credentials

use of okhttp3.Credentials in project azure-sdk-for-java by Azure.

the class ProviderRegistrationInterceptor method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    Response response = chain.proceed(chain.request());
    if (!response.isSuccessful()) {
        String content = errorBody(response.body());
        AzureJacksonAdapter jacksonAdapter = new AzureJacksonAdapter();
        CloudError cloudError = jacksonAdapter.deserialize(content, CloudError.class);
        if (cloudError != null && "MissingSubscriptionRegistration".equals(cloudError.code())) {
            Pattern pattern = Pattern.compile("/subscriptions/([\\w-]+)/", Pattern.CASE_INSENSITIVE);
            Matcher matcher = pattern.matcher(chain.request().url().toString());
            matcher.find();
            RestClient restClient = new RestClient.Builder().withBaseUrl("https://" + chain.request().url().host()).withCredentials(credentials).withSerializerAdapter(jacksonAdapter).withResponseBuilderFactory(new AzureResponseBuilder.Factory()).build();
            ResourceManager resourceManager = ResourceManager.authenticate(restClient).withSubscription(matcher.group(1));
            pattern = Pattern.compile(".*'(.*)'");
            matcher = pattern.matcher(cloudError.message());
            matcher.find();
            Provider provider = registerProvider(matcher.group(1), resourceManager);
            while (provider.registrationState().equalsIgnoreCase("Unregistered") || provider.registrationState().equalsIgnoreCase("Registering")) {
                SdkContext.sleep(5 * 1000);
                provider = resourceManager.providers().getByName(provider.namespace());
            }
            // Retry
            response = chain.proceed(chain.request());
        }
    }
    return response;
}
Also used : Response(okhttp3.Response) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) AzureJacksonAdapter(com.microsoft.azure.serializer.AzureJacksonAdapter) RestClient(com.microsoft.rest.RestClient) CloudError(com.microsoft.azure.CloudError) ResourceManager(com.microsoft.azure.management.resources.implementation.ResourceManager) Provider(com.microsoft.azure.management.resources.Provider)

Example 50 with Credentials

use of okhttp3.Credentials in project azure-sdk-for-java by Azure.

the class KeyVaultClientImpl method setCertificateIssuerWithServiceResponseAsync.

/**
     * Sets the specified certificate issuer.
     *
     * @param vaultBaseUrl The vault name, for example https://myvault.vault.azure.net.
     * @param issuerName The name of the issuer.
     * @param provider The issuer provider.
     * @param credentials The credentials to be used for the issuer.
     * @param organizationDetails Details of the organization as provided to the issuer.
     * @param attributes Attributes of the issuer object.
     * @return the observable to the IssuerBundle object
     */
public Observable<ServiceResponse<IssuerBundle>> setCertificateIssuerWithServiceResponseAsync(String vaultBaseUrl, String issuerName, String provider, IssuerCredentials credentials, OrganizationDetails organizationDetails, IssuerAttributes attributes) {
    if (vaultBaseUrl == null) {
        throw new IllegalArgumentException("Parameter vaultBaseUrl is required and cannot be null.");
    }
    if (issuerName == null) {
        throw new IllegalArgumentException("Parameter issuerName is required and cannot be null.");
    }
    if (this.apiVersion() == null) {
        throw new IllegalArgumentException("Parameter this.apiVersion() is required and cannot be null.");
    }
    if (provider == null) {
        throw new IllegalArgumentException("Parameter provider is required and cannot be null.");
    }
    Validator.validate(credentials);
    Validator.validate(organizationDetails);
    Validator.validate(attributes);
    CertificateIssuerSetParameters parameter = new CertificateIssuerSetParameters();
    parameter.withProvider(provider);
    parameter.withCredentials(credentials);
    parameter.withOrganizationDetails(organizationDetails);
    parameter.withAttributes(attributes);
    String parameterizedHost = Joiner.on(", ").join("{vaultBaseUrl}", vaultBaseUrl);
    return service.setCertificateIssuer(issuerName, this.apiVersion(), this.acceptLanguage(), parameter, parameterizedHost, this.userAgent()).flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<IssuerBundle>>>() {

        @Override
        public Observable<ServiceResponse<IssuerBundle>> call(Response<ResponseBody> response) {
            try {
                ServiceResponse<IssuerBundle> clientResponse = setCertificateIssuerDelegate(response);
                return Observable.just(clientResponse);
            } catch (Throwable t) {
                return Observable.error(t);
            }
        }
    });
}
Also used : Response(retrofit2.Response) ServiceResponse(com.microsoft.rest.ServiceResponse) IssuerBundle(com.microsoft.azure.keyvault.models.IssuerBundle) ServiceResponse(com.microsoft.rest.ServiceResponse) CertificateIssuerSetParameters(com.microsoft.azure.keyvault.models.CertificateIssuerSetParameters) Observable(rx.Observable) ResponseBody(okhttp3.ResponseBody)

Aggregations

Request (okhttp3.Request)39 Response (okhttp3.Response)29 OkHttpClient (okhttp3.OkHttpClient)22 IOException (java.io.IOException)20 Test (org.junit.Test)16 HttpUrl (okhttp3.HttpUrl)10 RequestBody (okhttp3.RequestBody)9 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)9 ResponseBody (okhttp3.ResponseBody)8 NonNull (androidx.annotation.NonNull)7 MockResponse (okhttp3.mockwebserver.MockResponse)7 BasicAuthenticator (com.burgstaller.okhttp.basic.BasicAuthenticator)6 CachingAuthenticator (com.burgstaller.okhttp.digest.CachingAuthenticator)6 Credentials (com.burgstaller.okhttp.digest.Credentials)6 DigestAuthenticator (com.burgstaller.okhttp.digest.DigestAuthenticator)6 Observable (rx.Observable)6 AuthenticationCacheInterceptor (com.burgstaller.okhttp.AuthenticationCacheInterceptor)5 CachingAuthenticatorDecorator (com.burgstaller.okhttp.CachingAuthenticatorDecorator)5 PokemonGo (com.pokegoapi.api.PokemonGo)5 PtcCredentialProvider (com.pokegoapi.auth.PtcCredentialProvider)5