Fix authentication with external browser does not grant admin scope

This commit is contained in:
Thomas 2022-11-27 09:53:06 +01:00
parent 58dcdbf050
commit 72f123d26b
3 changed files with 9 additions and 17 deletions

View File

@ -49,7 +49,7 @@ public class LoginActivity extends BaseActivity {
public static Account.API apiLogin;
public static String currentInstanceLogin, client_idLogin, client_secretLogin, softwareLogin;
private final int PICK_IMPORT = 5557;
private boolean requestedAdmin;
public static boolean requestedAdmin;
private void manageItent(Intent intent) {
@ -114,19 +114,11 @@ public class LoginActivity extends BaseActivity {
setContentView(new FrameLayout(this));
FragmentLoginMain fragmentLoginMain = new FragmentLoginMain();
Helper.addFragment(getSupportFragmentManager(), android.R.id.content, fragmentLoginMain, null, null, null);
requestedAdmin = false;
//The activity handles a redirect URI, it will extract token code and will proceed to authentication
//That happens when the user wants to use an external browser
manageItent(getIntent());
}
public boolean requestedAdmin() {
return requestedAdmin;
}
public boolean setAdmin(boolean askAdmin) {
return requestedAdmin = askAdmin;
}
@Override
protected void onResume() {

View File

@ -20,6 +20,7 @@ import static app.fedilab.android.activities.LoginActivity.apiLogin;
import static app.fedilab.android.activities.LoginActivity.client_idLogin;
import static app.fedilab.android.activities.LoginActivity.client_secretLogin;
import static app.fedilab.android.activities.LoginActivity.currentInstanceLogin;
import static app.fedilab.android.activities.LoginActivity.requestedAdmin;
import static app.fedilab.android.activities.LoginActivity.softwareLogin;
import static app.fedilab.android.helper.Helper.PREF_USER_TOKEN;
@ -69,7 +70,6 @@ public class WebviewConnectActivity extends BaseActivity {
private ActivityWebviewConnectBinding binding;
private AlertDialog alert;
private String login_url;
private boolean requestedAdmin;
@SuppressWarnings("deprecation")

View File

@ -19,6 +19,7 @@ import static app.fedilab.android.activities.LoginActivity.apiLogin;
import static app.fedilab.android.activities.LoginActivity.client_idLogin;
import static app.fedilab.android.activities.LoginActivity.client_secretLogin;
import static app.fedilab.android.activities.LoginActivity.currentInstanceLogin;
import static app.fedilab.android.activities.LoginActivity.requestedAdmin;
import static app.fedilab.android.activities.LoginActivity.softwareLogin;
import android.content.Intent;
@ -49,7 +50,6 @@ import java.net.URLEncoder;
import app.fedilab.android.BaseMainActivity;
import app.fedilab.android.R;
import app.fedilab.android.activities.LoginActivity;
import app.fedilab.android.activities.ProxyActivity;
import app.fedilab.android.activities.WebviewConnectActivity;
import app.fedilab.android.client.entities.app.Account;
@ -190,7 +190,7 @@ public class FragmentLoginMain extends Fragment {
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(requireActivity());
boolean embedded_browser = sharedpreferences.getBoolean(getString(R.string.SET_EMBEDDED_BROWSER), true);
customTabItem.setChecked(!embedded_browser);
adminTabItem.setChecked(((LoginActivity) requireActivity()).requestedAdmin());
adminTabItem.setChecked(requestedAdmin);
popupMenu.setOnMenuItemClickListener(item -> {
int itemId = item.getItemId();
if (itemId == R.id.action_proxy) {
@ -216,9 +216,9 @@ public class FragmentLoginMain extends Fragment {
});
editor.apply();
} else if (itemId == R.id.action_request_admin) {
boolean checked = !((LoginActivity) requireActivity()).requestedAdmin();
((LoginActivity) requireActivity()).setAdmin(checked);
item.setChecked(!item.isChecked());
requestedAdmin = item.isChecked();
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
item.setActionView(new View(requireContext()));
item.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@ -257,7 +257,7 @@ public class FragmentLoginMain extends Fragment {
} catch (UnsupportedEncodingException e) {
currentInstanceLogin = host;
}
String scopes = ((LoginActivity) requireActivity()).requestedAdmin() ? Helper.OAUTH_SCOPES_ADMIN : Helper.OAUTH_SCOPES;
String scopes = requestedAdmin ? Helper.OAUTH_SCOPES_ADMIN : Helper.OAUTH_SCOPES;
AppsVM appsVM = new ViewModelProvider(requireActivity()).get(AppsVM.class);
appsVM.createApp(currentInstanceLogin, getString(R.string.app_name),
Helper.REDIRECT_CONTENT_WEB,
@ -267,13 +267,13 @@ public class FragmentLoginMain extends Fragment {
if (app != null) {
client_idLogin = app.client_id;
client_secretLogin = app.client_secret;
String redirectUrl = MastodonHelper.authorizeURL(currentInstanceLogin, client_idLogin, ((LoginActivity) requireActivity()).requestedAdmin());
String redirectUrl = MastodonHelper.authorizeURL(currentInstanceLogin, client_idLogin, requestedAdmin);
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(requireActivity());
boolean embedded_browser = sharedpreferences.getBoolean(getString(R.string.SET_EMBEDDED_BROWSER), true);
if (embedded_browser) {
Intent i = new Intent(requireActivity(), WebviewConnectActivity.class);
i.putExtra("login_url", redirectUrl);
i.putExtra("requestedAdmin", ((LoginActivity) requireActivity()).requestedAdmin());
i.putExtra("requestedAdmin", requestedAdmin);
startActivity(i);
requireActivity().finish();
} else {