From 6689a9c3b4a6cee483af11ceedecefb89ba1402f Mon Sep 17 00:00:00 2001 From: Thomas Date: Tue, 30 Jun 2020 11:38:06 +0200 Subject: [PATCH] login issue --- .../fedilab/fedilabtube/LoginActivity.java | 67 +++- .../UpdateAccountInfoAsyncTask.java | 2 +- .../fedilabtube/client/PeertubeAPI.java | 24 +- .../fedilab/fedilabtube/helper/Helper.java | 35 +++ app/src/main/res/layout/activity_login.xml | 296 ++++++++++++------ app/src/main/res/values/strings.xml | 2 + 6 files changed, 301 insertions(+), 125 deletions(-) diff --git a/app/src/main/java/app/fedilab/fedilabtube/LoginActivity.java b/app/src/main/java/app/fedilab/fedilabtube/LoginActivity.java index 0294936..d799f49 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/LoginActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/LoginActivity.java @@ -2,6 +2,7 @@ package app.fedilab.fedilabtube; import android.content.Intent; import android.content.SharedPreferences; +import android.database.sqlite.SQLiteDatabase; import android.os.AsyncTask; import android.os.Bundle; import android.text.SpannableString; @@ -9,11 +10,15 @@ import android.text.Spanned; import android.text.style.ForegroundColorSpan; import android.text.style.UnderlineSpan; import android.view.MenuItem; +import android.view.View; import android.widget.Button; import android.widget.EditText; +import android.widget.ImageView; +import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; +import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.core.content.ContextCompat; @@ -24,10 +29,14 @@ import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.Arrays; import java.util.HashMap; +import java.util.List; import app.fedilab.fedilabtube.asynctasks.UpdateAccountInfoAsyncTask; import app.fedilab.fedilabtube.client.HttpsConnection; +import app.fedilab.fedilabtube.client.entities.Account; import app.fedilab.fedilabtube.helper.Helper; +import app.fedilab.fedilabtube.sqlite.AccountDAO; +import app.fedilab.fedilabtube.sqlite.Sqlite; import es.dmoral.toasty.Toasty; @@ -72,6 +81,57 @@ public class LoginActivity extends AppCompatActivity { login_passwd = findViewById(R.id.login_passwd); connectionButton = findViewById(R.id.login_button); + LinearLayout connected = findViewById(R.id.connected); + LinearLayout not_connected = findViewById(R.id.not_connected); + + if( Helper.isLoggedIn(LoginActivity.this)) { + not_connected.setVisibility(View.GONE); + SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); + SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); + String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); + String instance = sharedpreferences.getString(Helper.PREF_INSTANCE, Helper.getLiveInstance(LoginActivity.this)); + + TextView instanceView = findViewById(R.id.instance); + Account account = new AccountDAO(LoginActivity.this, db).getUniqAccount(userId, instance); + if( account == null) { + account = new AccountDAO(LoginActivity.this, db).getUniqAccount(userId, Helper.getPeertubeUrl(instance)); + } + if( account != null) { + ImageView profile_picture = findViewById(R.id.profile_picture); + TextView username = findViewById(R.id.username); + TextView displayname = findViewById(R.id.displayname); + + Helper.loadGiF(LoginActivity.this, account, profile_picture); + username.setText(String.format("@%s", account.getUsername())); + displayname.setText(account.getDisplay_name()); + + instanceView.setText(account.getInstance()); + + Button logout_button = findViewById(R.id.logout_button); + Account finalAccount = account; + logout_button.setOnClickListener(v -> { + AlertDialog.Builder dialogBuilderLogoutAccount = new AlertDialog.Builder(LoginActivity.this); + dialogBuilderLogoutAccount.setMessage(getString(R.string.logout_account_confirmation, finalAccount.getUsername())); + dialogBuilderLogoutAccount.setPositiveButton(R.string.action_logout, (dialog, id) -> { + Helper.logoutCurrentUser(LoginActivity.this, finalAccount); + dialog.dismiss(); + }); + dialogBuilderLogoutAccount.setNegativeButton(R.string.cancel, (dialog, id) -> dialog.dismiss()); + AlertDialog alertDialogLogoutAccount = dialogBuilderLogoutAccount.create(); + alertDialogLogoutAccount.show(); + }); + }else{ + Helper.logoutCurrentUser(LoginActivity.this, null); + } + + + + + + }else{ + connected.setVisibility(View.GONE); + } + connectionButton.setOnClickListener(v -> { @@ -136,11 +196,10 @@ public class LoginActivity extends AppCompatActivity { JSONObject resobjLogin; try { resobjLogin = new JSONObject(responseLogin); - String token = resobjLogin.get("access_token").toString(); - String refresh_token = null; - if (resobjLogin.has("refresh_token")) - refresh_token = resobjLogin.getString("refresh_token"); + String token = resobjLogin.getString("access_token"); + String refresh_token = resobjLogin.getString("refresh_token"); editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, token); + editor.putString(Helper.PREF_INSTANCE, host); editor.apply(); //Update the account with the token; new UpdateAccountInfoAsyncTask(LoginActivity.this, token, client_id, client_secret, refresh_token, host).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); diff --git a/app/src/main/java/app/fedilab/fedilabtube/asynctasks/UpdateAccountInfoAsyncTask.java b/app/src/main/java/app/fedilab/fedilabtube/asynctasks/UpdateAccountInfoAsyncTask.java index 2388b34..8ea6cf5 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/asynctasks/UpdateAccountInfoAsyncTask.java +++ b/app/src/main/java/app/fedilab/fedilabtube/asynctasks/UpdateAccountInfoAsyncTask.java @@ -41,7 +41,7 @@ public class UpdateAccountInfoAsyncTask extends AsyncTask { return null; } String instance = Helper.getPeertubeUrl(host); - account = new PeertubeAPI(this.contextReference.get(), instance, null).verifyCredentials(); + account = new PeertubeAPI(this.contextReference.get()).verifyCredentials(token, instance); if (account == null) return null; try { diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java b/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java index b3400eb..f80eac6 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java @@ -711,34 +711,27 @@ public class PeertubeAPI { * Verifiy credential of the authenticated user *synchronously* * @return Account */ - public Account verifyCredentials() { + public Account verifyCredentials(String token, String instance) { account = new Account(); - InstanceNodeInfo nodeinfo = displayNodeInfo(instance); - String social = null; - if (nodeinfo != null) { - social = nodeinfo.getName(); - } try { - String response = new HttpsConnection(context).get(getAbsoluteUrl("/users/me"), 60, null, prefKeyOauthTokenT); + String response = new HttpsConnection(context).get("https://"+instance+"/api/v1/users/me", 60, null, token); JSONObject accountObject = new JSONObject(response).getJSONObject("account"); account = parseAccountResponsePeertube(accountObject); - if (social != null) { - account.setSocial(social.toUpperCase()); - } } catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) { e.printStackTrace(); } catch (HttpsConnection.HttpsConnectionException e) { + if (e.getStatusCode() == 401 || e.getStatusCode() == 403) { SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); - Account targetedAccount = new AccountDAO(context, db).getAccountByToken(prefKeyOauthTokenT); + Account targetedAccount = new AccountDAO(context, db).getAccountByToken(token); if (targetedAccount != null) { HashMap values = refreshToken(targetedAccount.getClient_id(), targetedAccount.getClient_secret(), targetedAccount.getRefresh_token()); if (values.containsKey("access_token") && values.get("access_token") != null) { targetedAccount.setToken(values.get("access_token")); SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); - String token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null); + String tokenShared = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null); //This account is currently logged in, the token is updated - if (prefKeyOauthTokenT != null && prefKeyOauthTokenT.equals(token)) { + if (tokenShared != null && token.compareTo(tokenShared) == 0) { SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, targetedAccount.getToken()); editor.apply(); @@ -750,12 +743,9 @@ public class PeertubeAPI { String response; try { - response = new HttpsConnection(context).get(getAbsoluteUrl("/users/me"), 60, null, targetedAccount.getToken()); + response = new HttpsConnection(context).get("https://"+instance+"/api/v1/users/me", 60, null, targetedAccount.getToken()); JSONObject accountObject = new JSONObject(response).getJSONObject("account"); account = parseAccountResponsePeertube(accountObject); - if (social != null) { - account.setSocial(social.toUpperCase()); - } } catch (IOException | NoSuchAlgorithmException | KeyManagementException | JSONException e1) { e1.printStackTrace(); } catch (HttpsConnection.HttpsConnectionException e1) { diff --git a/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java b/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java index 851f3ce..3b69533 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java +++ b/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java @@ -7,11 +7,13 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Resources; +import android.database.sqlite.SQLiteDatabase; import android.graphics.Bitmap; import android.graphics.Color; import android.graphics.PorterDuff; import android.graphics.drawable.Drawable; import android.net.Uri; +import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.os.Environment; @@ -43,9 +45,12 @@ import java.util.Locale; import java.util.TimeZone; import java.util.regex.Pattern; +import app.fedilab.fedilabtube.MainActivity; import app.fedilab.fedilabtube.R; import app.fedilab.fedilabtube.WebviewActivity; import app.fedilab.fedilabtube.client.entities.Account; +import app.fedilab.fedilabtube.sqlite.AccountDAO; +import app.fedilab.fedilabtube.sqlite.Sqlite; import app.fedilab.fedilabtube.webview.CustomWebview; import app.fedilab.fedilabtube.webview.ProxyHelper; import es.dmoral.toasty.Toasty; @@ -550,6 +555,36 @@ public class Helper { } + /** + * Log out the authenticated user by removing its token + * + * @param activity Activity + */ + public static void logoutCurrentUser(Activity activity, Account account) { + SharedPreferences sharedpreferences = activity.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + + SQLiteDatabase db = Sqlite.getInstance(activity.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); + if (account != null) { + new AccountDAO(activity, db).removeUser(account); + } + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, null); + editor.putString(Helper.CLIENT_ID, null); + editor.putString(Helper.CLIENT_SECRET, null); + editor.putString(Helper.PREF_KEY_ID, null); + editor.putBoolean(Helper.PREF_IS_MODERATOR, false); + editor.putBoolean(Helper.PREF_IS_ADMINISTRATOR, false); + editor.putString(Helper.PREF_INSTANCE, null); + editor.putString(Helper.ID, null); + editor.apply(); + Intent mainActivity = new Intent(activity, MainActivity.class); + mainActivity.putExtra(Helper.INTENT_ACTION, Helper.ADD_USER_INTENT); + activity.startActivity(mainActivity); + activity.finish(); + + } + + public static int getAttColor(Context context, int attColor) { TypedValue typedValue = new TypedValue(); context.getTheme().resolveAttribute(attColor, typedValue, true); diff --git a/app/src/main/res/layout/activity_login.xml b/app/src/main/res/layout/activity_login.xml index 842d004..cd9d178 100644 --- a/app/src/main/res/layout/activity_login.xml +++ b/app/src/main/res/layout/activity_login.xml @@ -4,125 +4,215 @@ android:layout_width="match_parent" android:layout_height="match_parent"> - - - + + android:layout_height="match_parent"> + - + - + - + - + + + + + + + + + + + + + + + +