2018-12-29 22:10:13 +01:00
|
|
|
/*
|
2020-11-28 22:36:47 +01:00
|
|
|
* Copyright (C) 2020 Stefan Schüller <sschueller@techdroid.com>
|
2018-12-29 22:10:13 +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 22:10:13 +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 22:10:13 +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 22:10:13 +01:00
|
|
|
*/
|
|
|
|
package net.schueller.peertube.network;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.preference.PreferenceManager;
|
|
|
|
|
|
|
|
import net.schueller.peertube.R;
|
|
|
|
import net.schueller.peertube.application.AppApplication;
|
|
|
|
|
2020-07-05 20:51:53 +02:00
|
|
|
import static net.schueller.peertube.service.LoginService.refreshToken;
|
|
|
|
|
2018-12-29 22:10:13 +01:00
|
|
|
public class Session {
|
|
|
|
|
|
|
|
private static volatile Session sSoleInstance;
|
|
|
|
private static SharedPreferences sharedPreferences;
|
|
|
|
|
|
|
|
//private constructor.
|
2020-07-05 20:51:53 +02:00
|
|
|
private Session() {
|
2018-12-29 22:10:13 +01:00
|
|
|
|
|
|
|
Context context = AppApplication.getContext();
|
|
|
|
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
|
|
|
|
//Prevent form the reflection api.
|
2020-07-05 20:51:53 +02:00
|
|
|
if (sSoleInstance != null) {
|
2018-12-29 22:10:13 +01:00
|
|
|
throw new RuntimeException("Use getInstance() method to get the single instance of this class.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Session getInstance() {
|
|
|
|
if (sSoleInstance == null) { //if there is no instance available... create new one
|
|
|
|
synchronized (Session.class) {
|
|
|
|
if (sSoleInstance == null) sSoleInstance = new Session();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sSoleInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Make singleton from serialize and deserialize operation.
|
|
|
|
protected Session readResolve() {
|
|
|
|
return getInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public boolean isLoggedIn() {
|
|
|
|
// check if token exist or not
|
|
|
|
// return true if exist otherwise false
|
|
|
|
// assuming that token exists
|
|
|
|
|
|
|
|
//Log.v("Session", "isLoggedIn: " + (getToken() != null));
|
|
|
|
|
|
|
|
return getToken() != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getToken() {
|
|
|
|
// return the token that was saved earlier
|
|
|
|
|
|
|
|
String token = sharedPreferences.getString(AppApplication.getContext().getString(R.string.pref_token_access), null);
|
|
|
|
String type = sharedPreferences.getString(AppApplication.getContext().getString(R.string.pref_token_type), "Bearer");
|
|
|
|
|
|
|
|
if (token != null) {
|
|
|
|
return type + " " + token;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-07-05 20:51:53 +02:00
|
|
|
public String getPassword() {
|
|
|
|
return sharedPreferences.getString(AppApplication.getContext().getString(R.string.pref_auth_password), null);
|
2018-12-29 22:10:13 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-07-09 21:17:21 +02:00
|
|
|
public String getRefreshToken() {
|
|
|
|
return sharedPreferences.getString(AppApplication.getContext().getString(R.string.pref_token_refresh), null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-07-05 20:51:53 +02:00
|
|
|
public String refreshAccessToken() {
|
2018-12-29 22:10:13 +01:00
|
|
|
|
2020-07-05 20:51:53 +02:00
|
|
|
refreshToken();
|
|
|
|
// refresh token
|
2018-12-29 22:10:13 +01:00
|
|
|
|
2020-07-05 20:51:53 +02:00
|
|
|
return this.getToken();
|
2018-12-29 22:10:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void invalidate() {
|
|
|
|
// get called when user become logged out
|
|
|
|
// delete token and other user info
|
|
|
|
// (i.e: email, password)
|
|
|
|
// from the storage
|
|
|
|
|
|
|
|
Context context = AppApplication.getContext();
|
|
|
|
|
|
|
|
SharedPreferences.Editor editor = sharedPreferences.edit();
|
|
|
|
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_token_access), null);
|
2020-07-09 21:17:21 +02:00
|
|
|
editor.putString(context.getString(R.string.pref_token_refresh), null);
|
2018-12-29 22:10:13 +01:00
|
|
|
|
|
|
|
editor.commit();
|
|
|
|
}
|
|
|
|
}
|