Search in sources :

Example 1 with LoginClient

use of org.wikipedia.login.LoginClient in project apps-android-wikipedia by wikimedia.

the class CsrfTokenClient method getTokenBlocking.

@NonNull
public String getTokenBlocking() throws Throwable {
    String token = "";
    Service service = cachedService.service(csrfWikiSite);
    for (int retry = 0; retry < MAX_RETRIES_OF_LOGIN_BLOCKING; retry++) {
        try {
            if (retry > 0) {
                // Log in explicitly
                new LoginClient().loginBlocking(loginWikiSite, AccountUtil.getUserName(), AccountUtil.getPassword(), "");
            }
            Response<MwQueryResponse> response = service.request().execute();
            if (response.body() == null || !response.body().success() || TextUtils.isEmpty(response.body().query().csrfToken())) {
                continue;
            }
            token = response.body().query().csrfToken();
            if (AccountUtil.isLoggedIn() && token.equals(ANON_TOKEN)) {
                throw new RuntimeException("App believes we're logged in, but got anonymous token.");
            }
            break;
        } catch (Throwable t) {
            L.w(t);
        }
    }
    if (TextUtils.isEmpty(token) || token.equals(ANON_TOKEN)) {
        throw new IOException("Invalid token, or login failure.");
    }
    return token;
}
Also used : LoginClient(org.wikipedia.login.LoginClient) WikiCachedService(org.wikipedia.dataclient.retrofit.WikiCachedService) MwCachedService(org.wikipedia.dataclient.retrofit.MwCachedService) MwQueryResponse(org.wikipedia.dataclient.mwapi.MwQueryResponse) IOException(java.io.IOException) NonNull(android.support.annotation.NonNull)

Aggregations

NonNull (android.support.annotation.NonNull)1 IOException (java.io.IOException)1 MwQueryResponse (org.wikipedia.dataclient.mwapi.MwQueryResponse)1 MwCachedService (org.wikipedia.dataclient.retrofit.MwCachedService)1 WikiCachedService (org.wikipedia.dataclient.retrofit.WikiCachedService)1 LoginClient (org.wikipedia.login.LoginClient)1