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.database.Server;
import net.schueller.peertube.helper.APIUrlHelper; import net.schueller.peertube.helper.APIUrlHelper;
import net.schueller.peertube.network.Session;
import net.schueller.peertube.service.LoginService; 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.putString(mInflater.getContext().getString(R.string.pref_api_base_key), serverUrl);
editor.apply(); editor.apply();
// Logout if logged in
Session session = Session.getInstance();
if (session.isLoggedIn()) {
session.invalidate();
}
// attempt authentication if we have a username // attempt authentication if we have a username
if (!TextUtils.isEmpty(getServerAtPosition(position).getUsername())) { if (!TextUtils.isEmpty(getServerAtPosition(position).getUsername())) {
LoginService.Authenticate( LoginService.Authenticate(

View File

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

View File

@ -33,7 +33,7 @@ public class RetrofitInstance {
OkHttpClient.Builder okhttpClientBuilder = new OkHttpClient.Builder(); OkHttpClient.Builder okhttpClientBuilder = new OkHttpClient.Builder();
okhttpClientBuilder.addInterceptor(new AuthorizationInterceptor()); okhttpClientBuilder.addInterceptor(new AuthorizationInterceptor());
//okhttpClientBuilder.authenticator(new AccessTokenAuthenticator()); okhttpClientBuilder.authenticator(new AccessTokenAuthenticator());
retrofit = new retrofit2.Retrofit.Builder() retrofit = new retrofit2.Retrofit.Builder()
.client(okhttpClientBuilder.build()) .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() { public String refreshAccessToken() {
refreshToken(); 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_password), null);
editor.putString(context.getString(R.string.pref_auth_username), 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_access), null);
editor.putString(context.getString(R.string.pref_token_refresh), null);
editor.commit(); editor.commit();
} }

View File

@ -89,7 +89,7 @@ public class LoginService {
assert token != null; assert token != null;
editor.putString(context.getString(R.string.pref_token_access), token.getAccessToken()); 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.putString(context.getString(R.string.pref_token_type), token.getTokenType());
editor.apply(); editor.apply();
@ -162,18 +162,18 @@ public class LoginService {
assert token != null; assert token != null;
editor.putString(context.getString(R.string.pref_token_access), token.getAccessToken()); 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.putString(context.getString(R.string.pref_token_type), token.getTokenType());
editor.apply(); editor.apply();
Log.wtf(TAG, "Logged in"); 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 { } else {
Log.wtf(TAG, response.toString()); 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 @Override
public void onFailure(@NonNull Call<Token> call2, @NonNull Throwable t2) { public void onFailure(@NonNull Call<Token> call2, @NonNull Throwable t2) {
Log.wtf("err", t2.fillInStackTrace()); 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="menu_video_options_quality_automated">Automated</string>
<string name="pref_title_buildtime">Build Time</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> </resources>