use of com.firenio.codec.http11.Cookie in project okhttp-OkGo by jeasonlzy.
the class DBCookieStore method loadCookie.
/**
* 根据当前url获取所有需要的cookie,只返回没有过期的cookie
*/
@Override
public synchronized List<Cookie> loadCookie(HttpUrl url) {
List<Cookie> ret = new ArrayList<>();
if (!cookies.containsKey(url.host()))
return ret;
List<SerializableCookie> query = CookieManager.getInstance().query("host=?", new String[] { url.host() });
for (SerializableCookie serializableCookie : query) {
Cookie cookie = serializableCookie.getCookie();
if (isCookieExpired(cookie)) {
removeCookie(url, cookie);
} else {
ret.add(cookie);
}
}
return ret;
}
use of com.firenio.codec.http11.Cookie 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 com.firenio.codec.http11.Cookie in project OkHttp3 by MrZhousf.
the class SharedPrefsCookiePersistor method loadAll.
@Override
public List<Cookie> loadAll() {
List<Cookie> cookies = new ArrayList<>(sharedPreferences.getAll().size());
for (Map.Entry<String, ?> entry : sharedPreferences.getAll().entrySet()) {
String serializedCookie = (String) entry.getValue();
Cookie cookie = new SerializableCookie().decode(serializedCookie);
cookies.add(cookie);
}
return cookies;
}
use of com.firenio.codec.http11.Cookie in project OkHttp3 by MrZhousf.
the class SharedPrefsCookiePersistor method saveAll.
@Override
public void saveAll(Collection<Cookie> cookies) {
SharedPreferences.Editor editor = sharedPreferences.edit();
for (Cookie cookie : cookies) {
if (cookie.persistent()) {
editor.putString(createCookieKey(cookie), new SerializableCookie().encode(cookie));
}
}
editor.apply();
}
use of com.firenio.codec.http11.Cookie in project OkHttp3 by MrZhousf.
the class SharedPrefsCookiePersistor method removeAll.
@Override
public void removeAll(Collection<Cookie> cookies) {
SharedPreferences.Editor editor = sharedPreferences.edit();
for (Cookie cookie : cookies) {
editor.remove(createCookieKey(cookie));
}
editor.apply();
}
Aggregations