Thorium-android-app/app/src/main/java/net/schueller/peertube/network/AccessTokenAuthenticator.java

84 lines
3.0 KiB
Java
Raw Normal View History

2020-11-28 22:36:47 +01:00
/*
* Copyright (C) 2020 Stefan Schüller <sschueller@techdroid.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2020-07-05 20:51:53 +02:00
package net.schueller.peertube.network;
2020-07-09 21:17:21 +02:00
import android.util.Log;
2020-07-05 20:51:53 +02:00
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import okhttp3.Authenticator;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Route;
public class AccessTokenAuthenticator implements Authenticator {
2020-07-09 21:17:21 +02:00
private static final String TAG = "ATAuthenticator";
2020-07-05 20:51:53 +02:00
public AccessTokenAuthenticator() {
}
@Nullable
@Override
public Request authenticate(Route route, @NonNull Response response) {
Session session = Session.getInstance();
2020-07-09 21:17:21 +02:00
// check if we are using tokens
2020-07-05 20:51:53 +02:00
final String accessToken = session.getToken();
if (!isRequestWithAccessToken(response) || accessToken == null) {
return null;
}
synchronized (this) {
final String newAccessToken = session.getToken();
// Access token is refreshed in another thread.
if (!accessToken.equals(newAccessToken)) {
2020-07-09 21:17:21 +02:00
Log.v(TAG, "Access token is refreshed in another thread");
2020-07-05 20:51:53 +02:00
return newRequestWithAccessToken(response.request(), newAccessToken);
}
2020-07-09 21:17:21 +02:00
// 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());
2020-07-05 20:51:53 +02:00
// Need to refresh an access token
2020-07-09 21:17:21 +02:00
Log.v(TAG, "Need to refresh an access token");
2020-07-05 20:51:53 +02:00
final String updatedAccessToken = session.refreshAccessToken();
2020-07-09 21:17:21 +02:00
if (updatedAccessToken != null) {
return newRequestWithAccessToken(response.request(), updatedAccessToken);
}
Log.v(TAG, "Refresh failed");
return null;
2020-07-05 20:51:53 +02:00
}
}
private boolean isRequestWithAccessToken(@NonNull Response response) {
String header = response.request().header("Authorization");
return header != null && header.startsWith("Bearer");
}
@NonNull
private Request newRequestWithAccessToken(@NonNull Request request, @NonNull String accessToken) {
return request.newBuilder()
.header("Authorization", accessToken)
.build();
}
}