use of okhttp3.Credentials in project PokeGOAPI-Java by Grover-c13.
the class PtcCredentialProvider method login.
/**
* Starts a login flow for pokemon.com (PTC) using a username and password,
* this uses pokemon.com's oauth endpoint and returns a usable AuthInfo without user interaction
*
* @param username PTC username
* @param password PTC password
* @param attempt the current attempt index
* @throws LoginFailedException if an exception occurs while attempting to log in
* @throws InvalidCredentialsException if invalid credentials are used
*/
private void login(String username, String password, int attempt) throws LoginFailedException, InvalidCredentialsException {
try {
// TODO: stop creating an okhttp client per request
Response getResponse;
try {
getResponse = client.newCall(new Request.Builder().url(HttpUrl.parse("https://sso.pokemon.com/sso/oauth2.0/authorize").newBuilder().addQueryParameter("client_id", CLIENT_ID).addQueryParameter("redirect_uri", REDIRECT_URI).addQueryParameter("locale", "en").build()).get().build()).execute();
} catch (IOException e) {
throw new LoginFailedException("Failed to receive contents from server", e);
}
Moshi moshi = new Moshi.Builder().build();
PtcAuthJson ptcAuth;
try {
ptcAuth = moshi.adapter(PtcAuthJson.class).fromJson(getResponse.body().string());
} catch (IOException e) {
throw new LoginFailedException("Looks like the servers are down", e);
}
Response postResponse;
try {
FormBody postForm = new Builder().add("lt", ptcAuth.lt).add("execution", ptcAuth.execution).add("_eventId", "submit").add("username", username).add("password", password).add("locale", "en_US").build();
HttpUrl loginPostUrl = HttpUrl.parse(LOGIN_URL).newBuilder().addQueryParameter("service", SERVICE_URL).build();
Request postRequest = new Request.Builder().url(loginPostUrl).post(postForm).build();
// Need a new client for this to not follow redirects
postResponse = client.newBuilder().followRedirects(false).followSslRedirects(false).build().newCall(postRequest).execute();
} catch (IOException e) {
throw new LoginFailedException("Network failure", e);
}
String postBody;
try {
postBody = postResponse.body().string();
} catch (IOException e) {
throw new LoginFailedException("Response body fetching failed", e);
}
List<Cookie> cookies = client.cookieJar().loadForRequest(HttpUrl.parse(LOGIN_URL));
for (Cookie cookie : cookies) {
if (cookie.name().startsWith("CASTGC")) {
this.tokenId = cookie.value();
expiresTimestamp = time.currentTimeMillis() + 7140000L;
return;
}
}
if (postBody.length() > 0) {
try {
String[] errors = moshi.adapter(PtcAuthError.class).fromJson(postBody).errors;
if (errors != null && errors.length > 0) {
throw new InvalidCredentialsException(errors[0]);
}
} catch (IOException e) {
throw new LoginFailedException("Failed to parse ptc error json");
}
}
} catch (LoginFailedException e) {
if (shouldRetry && attempt < MAXIMUM_RETRIES) {
login(username, password, ++attempt);
} else {
throw new LoginFailedException("Exceeded maximum login retries", e);
}
}
}
use of okhttp3.Credentials in project sonarqube by SonarSource.
the class HttpConnectorTest method use_basic_authentication.
@Test
public void use_basic_authentication() throws Exception {
answerHelloWorld();
underTest = HttpConnector.newBuilder().url(serverUrl).credentials("theLogin", "thePassword").build();
GetRequest request = new GetRequest("api/issues/search");
underTest.call(request);
RecordedRequest recordedRequest = server.takeRequest();
assertThat(recordedRequest.getHeader("Authorization")).isEqualTo(basic("theLogin", "thePassword"));
}
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.");
}
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"));
}
use of okhttp3.Credentials in project instagram-java-scraper by postaddictme.
the class Instagram method login.
public void login(String username, String password) throws IOException {
if (username == null || password == null) {
throw new InstagramAuthException("Specify username and password");
} else if (this.csrf_token.isEmpty()) {
throw new NullPointerException("Please run before base()");
}
RequestBody formBody = new FormBody.Builder().add("username", username).add("password", password).add("queryParams", "{}").add("optIntoOneTap", "true").build();
Request request = new Request.Builder().url(Endpoint.LOGIN_URL).header(Endpoint.REFERER, Endpoint.BASE_URL + "/accounts/login/").post(formBody).build();
Response response = executeHttpRequest(withCsrfToken(request));
try (InputStream jsonStream = response.body().byteStream()) {
if (!mapper.isAuthenticated(jsonStream)) {
throw new InstagramAuthException("Credentials rejected by instagram", ErrorType.UNAUTHORIZED);
}
}
}
Aggregations