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

61 lines
1.9 KiB
Java
Raw Normal View History

2018-12-29 22:10:13 +01:00
/*
* Copyright 2018 Stefan Schüller <sschueller@techdroid.com>
*
* License: GPL-3.0+
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2018-12-17 18:11:41 +01:00
package net.schueller.peertube.network;
2018-12-29 22:10:13 +01:00
import android.util.Log;
2018-12-17 18:11:41 +01:00
import java.io.IOException;
import okhttp3.Interceptor;
2018-12-29 22:10:13 +01:00
import okhttp3.Request;
2018-12-17 18:11:41 +01:00
import okhttp3.Response;
public class AuthorizationInterceptor implements Interceptor {
public AuthorizationInterceptor() {
}
@Override
public Response intercept(Chain chain) throws IOException {
2018-12-29 22:10:13 +01:00
Session session = Session.getInstance();
2018-12-17 18:11:41 +01:00
Response mainResponse = chain.proceed(chain.request());
2018-12-29 22:10:13 +01:00
Request mainRequest = chain.request();
2018-12-17 18:11:41 +01:00
2018-12-29 22:10:13 +01:00
if (session.isLoggedIn()) {
2018-12-31 20:32:22 +01:00
if (mainResponse.code() == 401 || mainResponse.code() == 403) {
2018-12-29 22:10:13 +01:00
// session.invalidate();
// return mainResponse;
2018-12-31 20:32:22 +01:00
Log.v("Authorization", "Intercept code: " + mainResponse.code());
2018-12-29 22:10:13 +01:00
2018-12-31 20:32:22 +01:00
}
// add authentication header to each request if we are logged in
2018-12-29 22:10:13 +01:00
Request.Builder builder = mainRequest.newBuilder().header("Authorization", session.getToken()).
method(mainRequest.method(), mainRequest.body());
mainResponse = chain.proceed(builder.build());
Log.v("Authorization", "Intercept: " + session.getToken());
2018-12-17 18:11:41 +01:00
}
2018-12-29 22:10:13 +01:00
2018-12-17 18:11:41 +01:00
return mainResponse;
}
2018-12-29 22:10:13 +01:00
2018-12-17 18:11:41 +01:00
}