Fix token refresh

This commit is contained in:
Stefan Schüller 2020-07-09 21:17:21 +02:00
parent dda3c7cbc0
commit f1825ae75e
6 changed files with 41 additions and 7 deletions

View File

@ -37,6 +37,7 @@ import net.schueller.peertube.R;
import net.schueller.peertube.database.Server;
import net.schueller.peertube.helper.APIUrlHelper;
import net.schueller.peertube.network.Session;
import net.schueller.peertube.service.LoginService;
@ -89,6 +90,12 @@ public class ServerListAdapter extends RecyclerView.Adapter<ServerListAdapter.Se
editor.putString(mInflater.getContext().getString(R.string.pref_api_base_key), serverUrl);
editor.apply();
// Logout if logged in
Session session = Session.getInstance();
if (session.isLoggedIn()) {
session.invalidate();
}
// attempt authentication if we have a username
if (!TextUtils.isEmpty(getServerAtPosition(position).getUsername())) {
LoginService.Authenticate(

View File

@ -1,5 +1,7 @@
package net.schueller.peertube.network;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -10,6 +12,8 @@ import okhttp3.Route;
public class AccessTokenAuthenticator implements Authenticator {
private static final String TAG = "ATAuthenticator";
public AccessTokenAuthenticator() {
}
@ -18,6 +22,7 @@ public class AccessTokenAuthenticator implements Authenticator {
public Request authenticate(Route route, @NonNull Response response) {
Session session = Session.getInstance();
// check if we are using tokens
final String accessToken = session.getToken();
if (!isRequestWithAccessToken(response) || accessToken == null) {
return null;
@ -26,13 +31,27 @@ public class AccessTokenAuthenticator implements Authenticator {
final String newAccessToken = session.getToken();
// Access token is refreshed in another thread.
if (!accessToken.equals(newAccessToken)) {
Log.v(TAG, "Access token is refreshed in another thread");
return newRequestWithAccessToken(response.request(), newAccessToken);
}
// do we have a refresh token?
if (session.getRefreshToken() == null) {
Log.v(TAG, "No refresh token available");
return null;
}
Log.v(TAG, "refresh token: " + session.getRefreshToken());
// Need to refresh an access token
Log.v(TAG, "Need to refresh an access token");
final String updatedAccessToken = session.refreshAccessToken();
if (updatedAccessToken != null) {
return newRequestWithAccessToken(response.request(), updatedAccessToken);
}
Log.v(TAG, "Refresh failed");
return null;
}
}
private boolean isRequestWithAccessToken(@NonNull Response response) {

View File

@ -33,7 +33,7 @@ public class RetrofitInstance {
OkHttpClient.Builder okhttpClientBuilder = new OkHttpClient.Builder();
okhttpClientBuilder.addInterceptor(new AuthorizationInterceptor());
//okhttpClientBuilder.authenticator(new AccessTokenAuthenticator());
okhttpClientBuilder.authenticator(new AccessTokenAuthenticator());
retrofit = new retrofit2.Retrofit.Builder()
.client(okhttpClientBuilder.build())

View File

@ -87,6 +87,11 @@ public class Session {
}
public String getRefreshToken() {
return sharedPreferences.getString(AppApplication.getContext().getString(R.string.pref_token_refresh), null);
}
public String refreshAccessToken() {
refreshToken();
@ -107,6 +112,7 @@ public class Session {
editor.putString(context.getString(R.string.pref_auth_password), null);
editor.putString(context.getString(R.string.pref_auth_username), null);
editor.putString(context.getString(R.string.pref_token_access), null);
editor.putString(context.getString(R.string.pref_token_refresh), null);
editor.commit();
}

View File

@ -89,7 +89,7 @@ public class LoginService {
assert token != null;
editor.putString(context.getString(R.string.pref_token_access), token.getAccessToken());
editor.putString(context.getString(R.string.pref_token_refresh), token.getExpiresIn());
editor.putString(context.getString(R.string.pref_token_refresh), token.getRefreshToken());
editor.putString(context.getString(R.string.pref_token_type), token.getTokenType());
editor.apply();
@ -162,18 +162,18 @@ public class LoginService {
assert token != null;
editor.putString(context.getString(R.string.pref_token_access), token.getAccessToken());
editor.putString(context.getString(R.string.pref_token_refresh), token.getExpiresIn());
editor.putString(context.getString(R.string.pref_token_refresh), token.getRefreshToken());
editor.putString(context.getString(R.string.pref_token_type), token.getTokenType());
editor.apply();
Log.wtf(TAG, "Logged in");
Toast.makeText(context, context.getString(R.string.authentication_login_success), Toast.LENGTH_LONG).show();
Toast.makeText(context, context.getString(R.string.authentication_token_refresh_success), Toast.LENGTH_LONG).show();
} else {
Log.wtf(TAG, response.toString());
Toast.makeText(context, context.getString(R.string.authentication_login_failed), Toast.LENGTH_LONG).show();
Toast.makeText(context, context.getString(R.string.authentication_token_refresh_failed), Toast.LENGTH_LONG).show();
}
}
@ -181,7 +181,7 @@ public class LoginService {
@Override
public void onFailure(@NonNull Call<Token> call2, @NonNull Throwable t2) {
Log.wtf("err", t2.fillInStackTrace());
Toast.makeText(context, context.getString(R.string.authentication_login_failed), Toast.LENGTH_LONG).show();
Toast.makeText(context, context.getString(R.string.authentication_token_refresh_failed), Toast.LENGTH_LONG).show();
}
});

View File

@ -358,6 +358,8 @@
<string name="menu_video_options_quality_automated">Automated</string>
<string name="pref_title_buildtime">Build Time</string>
<string name="authentication_token_refresh_failed">Token refresh failed</string>
<string name="authentication_token_refresh_success">Token refresh successful</string>
</resources>