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();
}
});
});
}
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;
}
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);
}
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);
}
Aggregations