Thorium-android-app/app/src/main/java/net/schueller/peertube/service/LoginService.java

189 lines
8.0 KiB
Java
Raw Normal View History

2018-12-29 14:19:54 +01:00
/*
2020-11-28 22:36:47 +01:00
* Copyright (C) 2020 Stefan Schüller <sschueller@techdroid.com>
2018-12-29 14:19:54 +01:00
*
* This program is free software: you can redistribute it and/or modify
2020-11-28 22:36:47 +01:00
* 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.
2018-12-29 14:19:54 +01:00
*
* 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
2020-11-28 22:36:47 +01:00
* GNU Affero General Public License for more details.
2018-12-29 14:19:54 +01:00
*
2020-11-28 22:36:47 +01:00
* 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/>.
2018-12-29 14:19:54 +01:00
*/
2020-06-21 17:01:15 +02:00
package net.schueller.peertube.service;
2018-12-29 22:10:13 +01:00
import android.content.Context;
2018-12-17 18:11:41 +01:00
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
2018-11-13 22:16:30 +01:00
import android.util.Log;
2019-01-01 23:56:26 +01:00
import android.widget.Toast;
2020-06-21 17:01:15 +02:00
import androidx.annotation.NonNull;
import net.schueller.peertube.R;
2020-07-05 20:51:53 +02:00
import net.schueller.peertube.application.AppApplication;
2018-11-13 22:16:30 +01:00
import net.schueller.peertube.helper.APIUrlHelper;
import net.schueller.peertube.model.OauthClient;
import net.schueller.peertube.model.Token;
import net.schueller.peertube.network.AuthenticationService;
import net.schueller.peertube.network.RetrofitInstance;
import retrofit2.Call;
import retrofit2.Callback;
2018-12-17 18:11:41 +01:00
import retrofit2.Response;
2018-11-13 22:16:30 +01:00
2020-06-21 17:01:15 +02:00
import static net.schueller.peertube.application.AppApplication.getContext;
2020-06-21 17:01:15 +02:00
public class LoginService {
2020-06-21 17:01:15 +02:00
private static final String TAG = "LoginService";
2020-07-05 20:51:53 +02:00
public static void Authenticate(String username, String password) {
2020-06-21 17:01:15 +02:00
Context context = getContext();
2020-06-21 17:01:15 +02:00
String apiBaseURL = APIUrlHelper.getUrlWithVersion(context);
2020-06-21 17:01:15 +02:00
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
2020-11-22 13:54:54 +01:00
AuthenticationService service = RetrofitInstance.getRetrofitInstance(apiBaseURL, APIUrlHelper.useInsecureConnection(context)).create(AuthenticationService.class);
2018-11-13 22:16:30 +01:00
Call<OauthClient> call = service.getOauthClientLocal();
2018-12-17 18:11:41 +01:00
2018-11-13 22:16:30 +01:00
call.enqueue(new Callback<OauthClient>() {
@Override
2018-12-17 18:11:41 +01:00
public void onResponse(@NonNull Call<OauthClient> call, @NonNull Response<OauthClient> response) {
2018-11-13 22:16:30 +01:00
2018-12-17 18:11:41 +01:00
if (response.isSuccessful()) {
OauthClient oauthClient = response.body();
2018-11-13 22:16:30 +01:00
2020-07-05 20:51:53 +02:00
SharedPreferences.Editor editor = sharedPref.edit();
assert oauthClient != null;
editor.putString(context.getString(R.string.pref_client_id), oauthClient.getClientId());
editor.putString(context.getString(R.string.pref_client_secret), oauthClient.getClientSecret());
editor.apply();
2018-11-13 22:16:30 +01:00
Call<Token> call2 = service.getAuthenticationToken(
2018-12-17 18:11:41 +01:00
oauthClient.getClientId(),
oauthClient.getClientSecret(),
2018-11-13 22:16:30 +01:00
"code",
"password",
"upload",
2020-06-21 17:01:15 +02:00
username,
2018-11-13 22:16:30 +01:00
password
);
call2.enqueue(new Callback<Token>() {
@Override
public void onResponse(@NonNull Call<Token> call2, @NonNull retrofit2.Response<Token> response2) {
2018-12-17 18:11:41 +01:00
if (response2.isSuccessful()) {
Token token = response2.body();
2020-07-05 20:51:53 +02:00
assert token != null;
2020-06-21 17:01:15 +02:00
editor.putString(context.getString(R.string.pref_token_access), token.getAccessToken());
2020-07-09 21:17:21 +02:00
editor.putString(context.getString(R.string.pref_token_refresh), token.getRefreshToken());
2020-06-21 17:01:15 +02:00
editor.putString(context.getString(R.string.pref_token_type), token.getTokenType());
editor.apply();
2018-12-17 18:11:41 +01:00
2018-12-29 22:10:13 +01:00
Log.wtf(TAG, "Logged in");
2020-06-21 17:01:15 +02:00
Toast.makeText(context, context.getString(R.string.authentication_login_success), Toast.LENGTH_LONG).show();
2018-12-29 22:10:13 +01:00
2019-01-01 23:56:26 +01:00
2018-11-13 22:16:30 +01:00
} else {
Log.wtf(TAG, response2.toString());
2020-06-21 17:01:15 +02:00
Toast.makeText(context, context.getString(R.string.authentication_login_failed), Toast.LENGTH_LONG).show();
2019-01-01 23:56:26 +01:00
2018-11-13 22:16:30 +01:00
}
}
@Override
public void onFailure(@NonNull Call<Token> call2, @NonNull Throwable t2) {
Log.wtf("err", t2.fillInStackTrace());
2020-06-21 17:01:15 +02:00
Toast.makeText(context, context.getString(R.string.authentication_login_failed), Toast.LENGTH_LONG).show();
2018-11-13 22:16:30 +01:00
}
});
} else {
Log.wtf(TAG, response.toString());
2020-06-21 17:01:15 +02:00
Toast.makeText(context, context.getString(R.string.authentication_login_failed), Toast.LENGTH_LONG).show();
}
}
2018-11-13 22:16:30 +01:00
@Override
public void onFailure(@NonNull Call<OauthClient> call, @NonNull Throwable t) {
Log.wtf("err", t.fillInStackTrace());
2020-06-21 17:01:15 +02:00
Toast.makeText(context, context.getString(R.string.authentication_login_failed), Toast.LENGTH_LONG).show();
}
2018-11-13 22:16:30 +01:00
});
}
2020-07-05 20:51:53 +02:00
public static void refreshToken() {
Context context = getContext();
String apiBaseURL = APIUrlHelper.getUrlWithVersion(context);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
2020-11-22 13:54:54 +01:00
AuthenticationService service = RetrofitInstance.getRetrofitInstance(apiBaseURL, APIUrlHelper.useInsecureConnection(context)).create(AuthenticationService.class);
2020-07-05 20:51:53 +02:00
String refreshToken = sharedPreferences.getString(AppApplication.getContext().getString(R.string.pref_token_refresh), null);
String userName = sharedPreferences.getString(AppApplication.getContext().getString(R.string.pref_auth_username), null);
String clientId = sharedPreferences.getString(AppApplication.getContext().getString(R.string.pref_client_id), null);
String clientSecret = sharedPreferences.getString(AppApplication.getContext().getString(R.string.pref_client_secret), null);
Call<Token> call = service.refreshToken(
clientId,
clientSecret,
"refresh_token",
"code",
userName,
refreshToken
);
call.enqueue(new Callback<Token>() {
@Override
public void onResponse(@NonNull Call<Token> call, @NonNull retrofit2.Response<Token> response) {
if (response.isSuccessful()) {
Token token = response.body();
SharedPreferences.Editor editor = sharedPreferences.edit();
assert token != null;
editor.putString(context.getString(R.string.pref_token_access), token.getAccessToken());
2020-07-09 21:17:21 +02:00
editor.putString(context.getString(R.string.pref_token_refresh), token.getRefreshToken());
2020-07-05 20:51:53 +02:00
editor.putString(context.getString(R.string.pref_token_type), token.getTokenType());
editor.apply();
Log.wtf(TAG, "Logged in");
2020-07-09 21:17:21 +02:00
Toast.makeText(context, context.getString(R.string.authentication_token_refresh_success), Toast.LENGTH_LONG).show();
2020-07-05 20:51:53 +02:00
} else {
Log.wtf(TAG, response.toString());
2020-07-09 21:17:21 +02:00
Toast.makeText(context, context.getString(R.string.authentication_token_refresh_failed), Toast.LENGTH_LONG).show();
2020-07-05 20:51:53 +02:00
}
}
@Override
public void onFailure(@NonNull Call<Token> call2, @NonNull Throwable t2) {
Log.wtf("err", t2.fillInStackTrace());
2020-07-09 21:17:21 +02:00
Toast.makeText(context, context.getString(R.string.authentication_token_refresh_failed), Toast.LENGTH_LONG).show();
2020-07-05 20:51:53 +02:00
}
});
}
}