From 1b429a31a2ce29016200ba4d5da82f69b25dea94 Mon Sep 17 00:00:00 2001 From: Thomas Date: Mon, 23 Jan 2023 17:05:54 +0100 Subject: [PATCH] Fix authentication --- .../peertube/activities/LoginActivity.java | 11 ++++++++++- .../activities/PeertubeMainActivity.java | 11 ++++++++--- .../peertube/client/RetrofitPeertubeAPI.java | 10 +++++++--- .../peertube/client/entities/UserMe.java | 18 +++++++++++++----- .../android/peertube/helper/Helper.java | 8 +++++--- 5 files changed, 43 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/peertube/activities/LoginActivity.java b/app/src/main/java/app/fedilab/android/peertube/activities/LoginActivity.java index dd890d3a..62506e1e 100644 --- a/app/src/main/java/app/fedilab/android/peertube/activities/LoginActivity.java +++ b/app/src/main/java/app/fedilab/android/peertube/activities/LoginActivity.java @@ -14,6 +14,7 @@ package app.fedilab.android.peertube.activities; * You should have received a copy of the GNU General Public License along with TubeLab; if not, * see . */ +import static app.fedilab.android.mastodon.helper.Helper.TAG; import static app.fedilab.android.peertube.client.RetrofitPeertubeAPI.updateCredential; import android.annotation.SuppressLint; @@ -24,6 +25,7 @@ import android.text.SpannableString; import android.text.Spanned; import android.text.style.ForegroundColorSpan; import android.text.style.UnderlineSpan; +import android.util.Log; import android.util.Patterns; import android.view.MenuItem; import android.view.View; @@ -161,7 +163,8 @@ public class LoginActivity extends BaseBarActivity { } client_id = oauth.getClient_id(); client_secret = oauth.getClient_secret(); - + Log.v(TAG, "client_id: " + client_id); + Log.v(TAG, "client_secret: " + client_secret); SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putString(Helper.CLIENT_ID, client_id); @@ -180,6 +183,7 @@ public class LoginActivity extends BaseBarActivity { } try { Token token = new RetrofitPeertubeAPI(LoginActivity.this, finalInstance, null).manageToken(oauthParams); + Log.v(TAG, "token: " + token); proceedLogin(token, finalInstance); } catch (final Exception e) { oauthParams.setUsername(binding.loginUid.getText().toString().toLowerCase().trim()); @@ -191,6 +195,11 @@ public class LoginActivity extends BaseBarActivity { } proceedLogin(token, finalInstance); } catch (Error e) { + runOnUiThread(() -> { + Toasty.error(LoginActivity.this, e.getError() != null && !e.getError().isEmpty() ? e.getError() : getString(R.string.toast_error), Toasty.LENGTH_SHORT).show(); + binding.loginButton.setEnabled(true); + }); + e.printStackTrace(); } } diff --git a/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeMainActivity.java b/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeMainActivity.java index 27a97e8b..d2e24f09 100644 --- a/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeMainActivity.java +++ b/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeMainActivity.java @@ -48,6 +48,7 @@ import androidx.viewpager.widget.PagerAdapter; import androidx.viewpager.widget.ViewPager; import com.google.android.material.bottomnavigation.BottomNavigationView; +import com.kobakei.ratethisapp.RateThisApp; import org.jetbrains.annotations.NotNull; @@ -59,6 +60,7 @@ import java.util.TreeSet; import java.util.regex.Matcher; import java.util.regex.Pattern; +import app.fedilab.android.BuildConfig; import app.fedilab.android.R; import app.fedilab.android.activities.AboutActivity; import app.fedilab.android.activities.PeertubeBaseMainActivity; @@ -89,7 +91,7 @@ import app.fedilab.android.sqlite.Sqlite; import es.dmoral.toasty.Toasty; -public abstract class PeertubeMainActivity extends PeertubeBaseMainActivity { +public class PeertubeMainActivity extends PeertubeBaseMainActivity { public static int PICK_INSTANCE = 5641; @@ -188,7 +190,6 @@ public abstract class PeertubeMainActivity extends PeertubeBaseMainActivity { alert.show(); } - protected abstract void rateThisApp(); private void setTitleCustom(int titleRId) { Toolbar toolbar = findViewById(R.id.toolbar); @@ -313,7 +314,11 @@ public abstract class PeertubeMainActivity extends PeertubeBaseMainActivity { peertubeInformation.setTranslations(new LinkedHashMap<>()); startInForeground(); - rateThisApp(); + //noinspection ConstantConditions + if (BuildConfig.FLAVOR.compareTo("playstore") == 0) { + RateThisApp.onCreate(this); + RateThisApp.showRateDialogIfNeeded(this); + } final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); diff --git a/app/src/main/java/app/fedilab/android/peertube/client/RetrofitPeertubeAPI.java b/app/src/main/java/app/fedilab/android/peertube/client/RetrofitPeertubeAPI.java index c10aa3cd..82658f4c 100644 --- a/app/src/main/java/app/fedilab/android/peertube/client/RetrofitPeertubeAPI.java +++ b/app/src/main/java/app/fedilab/android/peertube/client/RetrofitPeertubeAPI.java @@ -17,6 +17,7 @@ package app.fedilab.android.peertube.client; import static app.fedilab.android.mastodon.helper.Helper.PREF_INSTANCE; import static app.fedilab.android.mastodon.helper.Helper.PREF_USER_ID; import static app.fedilab.android.mastodon.helper.Helper.PREF_USER_TOKEN; +import static app.fedilab.android.mastodon.helper.Helper.TAG; import android.app.Activity; import android.content.Context; @@ -26,6 +27,7 @@ import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import android.os.Handler; import android.os.Looper; +import android.util.Log; import android.webkit.MimeTypeMap; import android.webkit.URLUtil; @@ -156,6 +158,7 @@ public class RetrofitPeertubeAPI { String instance = host; try { UserMe userMe = new RetrofitPeertubeAPI(activity, instance, token).verifyCredentials(); + Log.v(TAG, "userMe: " + userMe); peertubeAccount = userMe.getAccount(); } catch (Error error) { Error.displayError(activity, error); @@ -173,6 +176,7 @@ public class RetrofitPeertubeAPI { account.client_secret = client_secret; account.refresh_token = refresh_token; account.instance = instance; + account.api = Account.API.PEERTUBE; account.peertube_account = peertubeAccount; SQLiteDatabase db = Sqlite.getInstance(activity.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); boolean userExists = false; @@ -238,14 +242,13 @@ public class RetrofitPeertubeAPI { public Token manageToken(OauthParams oauthParams) throws Error { PeertubeService peertubeService = init(); Call refreshTokenCall = null; - if (oauthParams.getExternalAuthToken() != null) { - refreshTokenCall = peertubeService.createOpenIdToken(oauthParams.getClient_id(), oauthParams.getClient_secret(), oauthParams.getResponse_type(), oauthParams.getGrant_type(), oauthParams.getScope(), oauthParams.getUsername(), oauthParams.getPassword(), oauthParams.getExternalAuthToken()); - } else if (oauthParams.getGrant_type().compareTo("password") == 0) { + if (oauthParams.getGrant_type().compareTo("password") == 0) { refreshTokenCall = peertubeService.createToken(oauthParams.getClient_id(), oauthParams.getClient_secret(), oauthParams.getGrant_type(), oauthParams.getUsername(), oauthParams.getPassword()); } else if (oauthParams.getGrant_type().compareTo("refresh_token") == 0) { refreshTokenCall = peertubeService.refreshToken(oauthParams.getClient_id(), oauthParams.getClient_secret(), oauthParams.getRefresh_token(), oauthParams.getGrant_type()); } if (refreshTokenCall != null) { + Log.v(TAG, "request: " + refreshTokenCall.request()); try { Response response = refreshTokenCall.execute(); if (response.isSuccessful()) { @@ -264,6 +267,7 @@ public class RetrofitPeertubeAPI { } else { Error error = new Error(); error.setStatusCode(response.code()); + Log.v(TAG, "err: " + response.errorBody().string()); if (response.errorBody() != null) { error.setError(response.errorBody().string()); } else { diff --git a/app/src/main/java/app/fedilab/android/peertube/client/entities/UserMe.java b/app/src/main/java/app/fedilab/android/peertube/client/entities/UserMe.java index 4df9ed00..3cc63954 100644 --- a/app/src/main/java/app/fedilab/android/peertube/client/entities/UserMe.java +++ b/app/src/main/java/app/fedilab/android/peertube/client/entities/UserMe.java @@ -56,7 +56,7 @@ public class UserMe { @SerializedName("nsfwPolicy") private String nsfwPolicy; @SerializedName("role") - private int role; + private Role role; @SerializedName("roleLabel") private String roleLabel; @SerializedName("username") @@ -74,6 +74,11 @@ public class UserMe { @SerializedName("webTorrentEnabled") private boolean webTorrentEnabled; + public Role getRole() { + return role; + } + + public PeertubeAccount getAccount() { return account; } @@ -186,12 +191,15 @@ public class UserMe { this.nsfwPolicy = nsfwPolicy; } - public int getRole() { - return role; + public void setRole(Role role) { + this.role = role; } - public void setRole(int role) { - this.role = role; + public static class Role { + @SerializedName("id") + String id; + @SerializedName("label") + String label; } public String getRoleLabel() { diff --git a/app/src/main/java/app/fedilab/android/peertube/helper/Helper.java b/app/src/main/java/app/fedilab/android/peertube/helper/Helper.java index ff4d7914..365017f1 100644 --- a/app/src/main/java/app/fedilab/android/peertube/helper/Helper.java +++ b/app/src/main/java/app/fedilab/android/peertube/helper/Helper.java @@ -115,7 +115,6 @@ public class Helper { public static final String REDIRECT_CONTENT_WEB = "tubelab://backtotubelab"; public static final int DEFAULT_VIDEO_CACHE_MB = 100; @SuppressWarnings({"unused", "RedundantSuppression"}) - public static final String TAG = "TubeLab"; public static final String ID = "id"; public static final String CLIENT_ID = "client_id"; public static final String CLIENT_SECRET = "client_secret"; @@ -668,11 +667,14 @@ public class Helper { public static boolean isVideoOwner(Context context, VideoData.Video video) { SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); - String userId = sharedpreferences.getString(PREF_USER_ID, ""); - String instance = sharedpreferences.getString(PREF_INSTANCE, ""); + String userId = sharedpreferences.getString(PREF_USER_ID, null); + String instance = sharedpreferences.getString(PREF_INSTANCE, null); if (video == null) { return false; } + if (userId == null || instance == null) { + return false; + } AccountData.PeertubeAccount account = video.getAccount(); ChannelData.Channel channel = video.getChannel(); if (account != null) {