Search in sources :

Example 1 with UserAccessToken

use of rxreddit.model.UserAccessToken in project RxReddit by damien5314.

the class RedditAuthService method refreshUserAccessToken.

@Override
public Observable<UserAccessToken> refreshUserAccessToken() {
    return Observable.defer(() -> {
        UserAccessToken token = getUserAccessToken();
        if (token == null) {
            return Observable.error(new IllegalStateException("No user access token available"));
        }
        if (token.secondsUntilExpiration() > EXPIRATION_THRESHOLD) {
            return Observable.just(token);
        }
        String refreshToken = token.getRefreshToken();
        if (refreshToken == null) {
            clearUserAccessToken();
            return Observable.error(new IllegalStateException("No refresh token available"));
        }
        String grantType = "refresh_token";
        return authService.refreshUserAuthToken(grantType, refreshToken).flatMap(RxRedditUtil::responseToBody).doOnNext(this::saveUserAccessToken).doOnError(error -> {
            if (error instanceof HttpException && ((HttpException) error).code() == 403) {
                // 403 means our refresh token is no longer good, just discard it
                clearUserAccessToken();
            }
        });
    });
}
Also used : UserAccessToken(rxreddit.model.UserAccessToken) HttpException(retrofit2.HttpException) RxRedditUtil(rxreddit.util.RxRedditUtil)

Example 2 with UserAccessToken

use of rxreddit.model.UserAccessToken in project RxReddit by damien5314.

the class AndroidAccessTokenManager method getSavedUserAccessToken.

private UserAccessToken getSavedUserAccessToken() {
    SharedPreferences sp = context.getSharedPreferences(PREFS_USER_ACCESS_TOKEN, Context.MODE_PRIVATE);
    if (!sp.contains(PREF_ACCESS_TOKEN))
        return null;
    UserAccessToken token = new UserAccessToken();
    token.setToken(sp.getString(PREF_ACCESS_TOKEN, null));
    token.setTokenType(sp.getString(PREF_TOKEN_TYPE, null));
    token.setExpiration(sp.getLong(PREF_EXPIRATION, 0));
    token.setScope(sp.getString(PREF_SCOPE, null));
    token.setRefreshToken(sp.getString(PREF_REFRESH_TOKEN, null));
    return token;
}
Also used : SharedPreferences(android.content.SharedPreferences) UserAccessToken(rxreddit.model.UserAccessToken)

Example 3 with UserAccessToken

use of rxreddit.model.UserAccessToken in project RxReddit by damien5314.

the class RedditAuthService method revokeUserAuthentication.

@Override
public Completable revokeUserAuthentication() {
    AccessToken token = getUserAccessToken();
    userAccessToken = null;
    accessTokenManager.clearSavedUserAccessToken();
    return revokeAuthToken(token);
}
Also used : AccessToken(rxreddit.model.AccessToken) ApplicationAccessToken(rxreddit.model.ApplicationAccessToken) UserAccessToken(rxreddit.model.UserAccessToken)

Example 4 with UserAccessToken

use of rxreddit.model.UserAccessToken in project RxReddit by damien5314.

the class RedditAuthService method saveUserAccessToken.

private void saveUserAccessToken(UserAccessToken token) {
    if (token.getRefreshToken() == null) {
        UserAccessToken storedToken = accessTokenManager.getUserAccessToken();
        if (storedToken != null) {
            // Refresh token doesn't come back in a refresh, so we have to use the one stored
            token.setRefreshToken(storedToken.getRefreshToken());
        }
    }
    userAccessToken = token;
    accessTokenManager.saveUserAccessToken(token);
}
Also used : UserAccessToken(rxreddit.model.UserAccessToken)

Aggregations

UserAccessToken (rxreddit.model.UserAccessToken)4 SharedPreferences (android.content.SharedPreferences)1 HttpException (retrofit2.HttpException)1 AccessToken (rxreddit.model.AccessToken)1 ApplicationAccessToken (rxreddit.model.ApplicationAccessToken)1 RxRedditUtil (rxreddit.util.RxRedditUtil)1