Fix authentication

This commit is contained in:
Thomas 2023-01-23 17:05:54 +01:00
parent 82a5bfebb4
commit 1b429a31a2
5 changed files with 43 additions and 15 deletions

View file

@ -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, * You should have received a copy of the GNU General Public License along with TubeLab; if not,
* see <http://www.gnu.org/licenses>. */ * see <http://www.gnu.org/licenses>. */
import static app.fedilab.android.mastodon.helper.Helper.TAG;
import static app.fedilab.android.peertube.client.RetrofitPeertubeAPI.updateCredential; import static app.fedilab.android.peertube.client.RetrofitPeertubeAPI.updateCredential;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
@ -24,6 +25,7 @@ import android.text.SpannableString;
import android.text.Spanned; import android.text.Spanned;
import android.text.style.ForegroundColorSpan; import android.text.style.ForegroundColorSpan;
import android.text.style.UnderlineSpan; import android.text.style.UnderlineSpan;
import android.util.Log;
import android.util.Patterns; import android.util.Patterns;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
@ -161,7 +163,8 @@ public class LoginActivity extends BaseBarActivity {
} }
client_id = oauth.getClient_id(); client_id = oauth.getClient_id();
client_secret = oauth.getClient_secret(); 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 sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit(); SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.CLIENT_ID, client_id); editor.putString(Helper.CLIENT_ID, client_id);
@ -180,6 +183,7 @@ public class LoginActivity extends BaseBarActivity {
} }
try { try {
Token token = new RetrofitPeertubeAPI(LoginActivity.this, finalInstance, null).manageToken(oauthParams); Token token = new RetrofitPeertubeAPI(LoginActivity.this, finalInstance, null).manageToken(oauthParams);
Log.v(TAG, "token: " + token);
proceedLogin(token, finalInstance); proceedLogin(token, finalInstance);
} catch (final Exception e) { } catch (final Exception e) {
oauthParams.setUsername(binding.loginUid.getText().toString().toLowerCase().trim()); oauthParams.setUsername(binding.loginUid.getText().toString().toLowerCase().trim());
@ -191,6 +195,11 @@ public class LoginActivity extends BaseBarActivity {
} }
proceedLogin(token, finalInstance); proceedLogin(token, finalInstance);
} catch (Error e) { } 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(); e.printStackTrace();
} }
} }

View file

@ -48,6 +48,7 @@ import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager; import androidx.viewpager.widget.ViewPager;
import com.google.android.material.bottomnavigation.BottomNavigationView; import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.kobakei.ratethisapp.RateThisApp;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -59,6 +60,7 @@ import java.util.TreeSet;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import app.fedilab.android.BuildConfig;
import app.fedilab.android.R; import app.fedilab.android.R;
import app.fedilab.android.activities.AboutActivity; import app.fedilab.android.activities.AboutActivity;
import app.fedilab.android.activities.PeertubeBaseMainActivity; import app.fedilab.android.activities.PeertubeBaseMainActivity;
@ -89,7 +91,7 @@ import app.fedilab.android.sqlite.Sqlite;
import es.dmoral.toasty.Toasty; import es.dmoral.toasty.Toasty;
public abstract class PeertubeMainActivity extends PeertubeBaseMainActivity { public class PeertubeMainActivity extends PeertubeBaseMainActivity {
public static int PICK_INSTANCE = 5641; public static int PICK_INSTANCE = 5641;
@ -188,7 +190,6 @@ public abstract class PeertubeMainActivity extends PeertubeBaseMainActivity {
alert.show(); alert.show();
} }
protected abstract void rateThisApp();
private void setTitleCustom(int titleRId) { private void setTitleCustom(int titleRId) {
Toolbar toolbar = findViewById(R.id.toolbar); Toolbar toolbar = findViewById(R.id.toolbar);
@ -313,7 +314,11 @@ public abstract class PeertubeMainActivity extends PeertubeBaseMainActivity {
peertubeInformation.setTranslations(new LinkedHashMap<>()); peertubeInformation.setTranslations(new LinkedHashMap<>());
startInForeground(); 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); final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);

View file

@ -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_INSTANCE;
import static app.fedilab.android.mastodon.helper.Helper.PREF_USER_ID; 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.PREF_USER_TOKEN;
import static app.fedilab.android.mastodon.helper.Helper.TAG;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
@ -26,6 +27,7 @@ import android.database.sqlite.SQLiteDatabase;
import android.net.Uri; import android.net.Uri;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.util.Log;
import android.webkit.MimeTypeMap; import android.webkit.MimeTypeMap;
import android.webkit.URLUtil; import android.webkit.URLUtil;
@ -156,6 +158,7 @@ public class RetrofitPeertubeAPI {
String instance = host; String instance = host;
try { try {
UserMe userMe = new RetrofitPeertubeAPI(activity, instance, token).verifyCredentials(); UserMe userMe = new RetrofitPeertubeAPI(activity, instance, token).verifyCredentials();
Log.v(TAG, "userMe: " + userMe);
peertubeAccount = userMe.getAccount(); peertubeAccount = userMe.getAccount();
} catch (Error error) { } catch (Error error) {
Error.displayError(activity, error); Error.displayError(activity, error);
@ -173,6 +176,7 @@ public class RetrofitPeertubeAPI {
account.client_secret = client_secret; account.client_secret = client_secret;
account.refresh_token = refresh_token; account.refresh_token = refresh_token;
account.instance = instance; account.instance = instance;
account.api = Account.API.PEERTUBE;
account.peertube_account = peertubeAccount; account.peertube_account = peertubeAccount;
SQLiteDatabase db = Sqlite.getInstance(activity.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); SQLiteDatabase db = Sqlite.getInstance(activity.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
boolean userExists = false; boolean userExists = false;
@ -238,14 +242,13 @@ public class RetrofitPeertubeAPI {
public Token manageToken(OauthParams oauthParams) throws Error { public Token manageToken(OauthParams oauthParams) throws Error {
PeertubeService peertubeService = init(); PeertubeService peertubeService = init();
Call<Token> refreshTokenCall = null; Call<Token> refreshTokenCall = null;
if (oauthParams.getExternalAuthToken() != null) { if (oauthParams.getGrant_type().compareTo("password") == 0) {
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) {
refreshTokenCall = peertubeService.createToken(oauthParams.getClient_id(), oauthParams.getClient_secret(), oauthParams.getGrant_type(), oauthParams.getUsername(), oauthParams.getPassword()); 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) { } else if (oauthParams.getGrant_type().compareTo("refresh_token") == 0) {
refreshTokenCall = peertubeService.refreshToken(oauthParams.getClient_id(), oauthParams.getClient_secret(), oauthParams.getRefresh_token(), oauthParams.getGrant_type()); refreshTokenCall = peertubeService.refreshToken(oauthParams.getClient_id(), oauthParams.getClient_secret(), oauthParams.getRefresh_token(), oauthParams.getGrant_type());
} }
if (refreshTokenCall != null) { if (refreshTokenCall != null) {
Log.v(TAG, "request: " + refreshTokenCall.request());
try { try {
Response<Token> response = refreshTokenCall.execute(); Response<Token> response = refreshTokenCall.execute();
if (response.isSuccessful()) { if (response.isSuccessful()) {
@ -264,6 +267,7 @@ public class RetrofitPeertubeAPI {
} else { } else {
Error error = new Error(); Error error = new Error();
error.setStatusCode(response.code()); error.setStatusCode(response.code());
Log.v(TAG, "err: " + response.errorBody().string());
if (response.errorBody() != null) { if (response.errorBody() != null) {
error.setError(response.errorBody().string()); error.setError(response.errorBody().string());
} else { } else {

View file

@ -56,7 +56,7 @@ public class UserMe {
@SerializedName("nsfwPolicy") @SerializedName("nsfwPolicy")
private String nsfwPolicy; private String nsfwPolicy;
@SerializedName("role") @SerializedName("role")
private int role; private Role role;
@SerializedName("roleLabel") @SerializedName("roleLabel")
private String roleLabel; private String roleLabel;
@SerializedName("username") @SerializedName("username")
@ -74,6 +74,11 @@ public class UserMe {
@SerializedName("webTorrentEnabled") @SerializedName("webTorrentEnabled")
private boolean webTorrentEnabled; private boolean webTorrentEnabled;
public Role getRole() {
return role;
}
public PeertubeAccount getAccount() { public PeertubeAccount getAccount() {
return account; return account;
} }
@ -186,12 +191,15 @@ public class UserMe {
this.nsfwPolicy = nsfwPolicy; this.nsfwPolicy = nsfwPolicy;
} }
public int getRole() { public void setRole(Role role) {
return role; this.role = role;
} }
public void setRole(int role) { public static class Role {
this.role = role; @SerializedName("id")
String id;
@SerializedName("label")
String label;
} }
public String getRoleLabel() { public String getRoleLabel() {

View file

@ -115,7 +115,6 @@ public class Helper {
public static final String REDIRECT_CONTENT_WEB = "tubelab://backtotubelab"; public static final String REDIRECT_CONTENT_WEB = "tubelab://backtotubelab";
public static final int DEFAULT_VIDEO_CACHE_MB = 100; public static final int DEFAULT_VIDEO_CACHE_MB = 100;
@SuppressWarnings({"unused", "RedundantSuppression"}) @SuppressWarnings({"unused", "RedundantSuppression"})
public static final String TAG = "TubeLab";
public static final String ID = "id"; public static final String ID = "id";
public static final String CLIENT_ID = "client_id"; public static final String CLIENT_ID = "client_id";
public static final String CLIENT_SECRET = "client_secret"; public static final String CLIENT_SECRET = "client_secret";
@ -668,11 +667,14 @@ public class Helper {
public static boolean isVideoOwner(Context context, VideoData.Video video) { public static boolean isVideoOwner(Context context, VideoData.Video video) {
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
String userId = sharedpreferences.getString(PREF_USER_ID, ""); String userId = sharedpreferences.getString(PREF_USER_ID, null);
String instance = sharedpreferences.getString(PREF_INSTANCE, ""); String instance = sharedpreferences.getString(PREF_INSTANCE, null);
if (video == null) { if (video == null) {
return false; return false;
} }
if (userId == null || instance == null) {
return false;
}
AccountData.PeertubeAccount account = video.getAccount(); AccountData.PeertubeAccount account = video.getAccount();
ChannelData.Channel channel = video.getChannel(); ChannelData.Channel channel = video.getChannel();
if (account != null) { if (account != null) {