2018-12-29 14:19:54 +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/>.
|
|
|
|
*/
|
2020-06-21 17:01:15 +02:00
|
|
|
package net.schueller.peertube.service;
|
2018-03-03 02:46:01 +01:00
|
|
|
|
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;
|
2018-03-03 02:46:01 +01:00
|
|
|
|
|
|
|
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;
|
2018-03-03 02:46:01 +01:00
|
|
|
|
2020-06-21 17:01:15 +02:00
|
|
|
public class LoginService {
|
2018-03-03 02:46:01 +01:00
|
|
|
|
2020-06-21 17:01:15 +02:00
|
|
|
private static final String TAG = "LoginService";
|
2018-12-23 16:42:01 +01:00
|
|
|
|
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();
|
2018-03-03 02:46:01 +01:00
|
|
|
|
2020-06-21 17:01:15 +02:00
|
|
|
String apiBaseURL = APIUrlHelper.getUrlWithVersion(context);
|
2018-03-03 02:46:01 +01:00
|
|
|
|
2020-06-21 17:01:15 +02:00
|
|
|
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
|
2018-03-03 02:46:01 +01:00
|
|
|
|
2018-11-13 22:16:30 +01:00
|
|
|
AuthenticationService service = RetrofitInstance.getRetrofitInstance(apiBaseURL).create(AuthenticationService.class);
|
2018-03-03 02:46:01 +01:00
|
|
|
|
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());
|
|
|
|
editor.putString(context.getString(R.string.pref_token_refresh), token.getExpiresIn());
|
|
|
|
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-03-03 02:46:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-03-03 02:46:01 +01:00
|
|
|
}
|
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);
|
|
|
|
AuthenticationService service = RetrofitInstance.getRetrofitInstance(apiBaseURL).create(AuthenticationService.class);
|
|
|
|
|
|
|
|
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());
|
|
|
|
editor.putString(context.getString(R.string.pref_token_refresh), token.getExpiresIn());
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
Log.wtf(TAG, response.toString());
|
|
|
|
Toast.makeText(context, context.getString(R.string.authentication_login_failed), Toast.LENGTH_LONG).show();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@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();
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-03-03 02:46:01 +01:00
|
|
|
}
|