From d039c4870f4ac3e48fb597690db732d96faa0d72 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Thu, 9 Jan 2020 14:13:05 +0100 Subject: [PATCH] support registration via pars tokens --- .../conversations/ui/MagicCreateActivity.java | 184 ++++++++++-------- .../conversations/utils/SignupUtils.java | 12 ++ src/conversations/res/layout/magic_create.xml | 181 +++++++++-------- src/conversations/res/values/strings.xml | 2 +- .../siacs/conversations/entities/Account.java | 4 + .../conversations/ui/EditAccountActivity.java | 13 ++ .../conversations/ui/UriHandlerActivity.java | 14 ++ .../eu/siacs/conversations/utils/XmppUri.java | 5 + .../eu/siacs/conversations/xml/Namespace.java | 2 + .../conversations/xmpp/XmppConnection.java | 27 ++- src/main/res/values/strings.xml | 1 + 11 files changed, 280 insertions(+), 165 deletions(-) diff --git a/src/conversations/java/eu/siacs/conversations/ui/MagicCreateActivity.java b/src/conversations/java/eu/siacs/conversations/ui/MagicCreateActivity.java index 52c067258..bc9b06d5f 100644 --- a/src/conversations/java/eu/siacs/conversations/ui/MagicCreateActivity.java +++ b/src/conversations/java/eu/siacs/conversations/ui/MagicCreateActivity.java @@ -2,7 +2,9 @@ package eu.siacs.conversations.ui; import android.content.Intent; import android.content.pm.ActivityInfo; +import android.databinding.DataBindingUtil; import android.os.Bundle; +import android.support.v7.widget.Toolbar; import android.text.Editable; import android.text.TextWatcher; import android.view.View; @@ -15,102 +17,124 @@ import java.security.SecureRandom; import eu.siacs.conversations.Config; import eu.siacs.conversations.R; +import eu.siacs.conversations.databinding.MagicCreateBinding; import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.utils.CryptoHelper; import rocks.xmpp.addr.Jid; public class MagicCreateActivity extends XmppActivity implements TextWatcher { - private TextView mFullJidDisplay; - private EditText mUsername; + public static final String EXTRA_DOMAIN = "domain"; + public static final String EXTRA_PRE_AUTH = "pre_auth"; - @Override - protected void refreshUiReal() { + private MagicCreateBinding binding; + private String domain; + private String preAuth; - } + @Override + protected void refreshUiReal() { - @Override - void onBackendConnected() { + } - } + @Override + void onBackendConnected() { - @Override - public void onStart() { - super.onStart(); - final int theme = findTheme(); - if (this.mTheme != theme) { - recreate(); - } - } + } - @Override - protected void onCreate(final Bundle savedInstanceState) { - if (getResources().getBoolean(R.bool.portrait_only)) { - setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); - } - super.onCreate(savedInstanceState); - setContentView(R.layout.magic_create); - setSupportActionBar(findViewById(R.id.toolbar)); - configureActionBar(getSupportActionBar()); - mFullJidDisplay = findViewById(R.id.full_jid); - mUsername = findViewById(R.id.username); - Button next = findViewById(R.id.create_account); - next.setOnClickListener(v -> { - try { - String username = mUsername.getText().toString(); - Jid jid = Jid.of(username.toLowerCase(), Config.MAGIC_CREATE_DOMAIN, null); - if (!jid.getEscapedLocal().equals(jid.getLocal())|| username.length() < 3) { - mUsername.setError(getString(R.string.invalid_username)); - mUsername.requestFocus(); - } else { - mUsername.setError(null); - Account account = xmppConnectionService.findAccountByJid(jid); - if (account == null) { - account = new Account(jid, CryptoHelper.createPassword(new SecureRandom())); - account.setOption(Account.OPTION_REGISTER, true); - account.setOption(Account.OPTION_DISABLED, true); - account.setOption(Account.OPTION_MAGIC_CREATE, true); - xmppConnectionService.createAccount(account); - } - Intent intent = new Intent(MagicCreateActivity.this, EditAccountActivity.class); - intent.putExtra("jid", account.getJid().asBareJid().toString()); - intent.putExtra("init", true); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); - Toast.makeText(MagicCreateActivity.this, R.string.secure_password_generated, Toast.LENGTH_SHORT).show(); - StartConversationActivity.addInviteUri(intent, getIntent()); - startActivity(intent); - } - } catch (IllegalArgumentException e) { - mUsername.setError(getString(R.string.invalid_username)); - mUsername.requestFocus(); - } - }); - mUsername.addTextChangedListener(this); - } + @Override + public void onStart() { + super.onStart(); + final int theme = findTheme(); + if (this.mTheme != theme) { + recreate(); + } + } - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { + @Override + protected void onCreate(final Bundle savedInstanceState) { + final Intent data = getIntent(); + this.domain = data == null ? null : data.getStringExtra(EXTRA_DOMAIN); + this.preAuth = data == null ? null : data.getStringExtra(EXTRA_PRE_AUTH); + if (getResources().getBoolean(R.bool.portrait_only)) { + setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); + } + super.onCreate(savedInstanceState); + this.binding = DataBindingUtil.setContentView(this, R.layout.magic_create); + setSupportActionBar((Toolbar) this.binding.toolbar); + configureActionBar(getSupportActionBar(), this.domain == null); + if (domain != null) { + binding.instructions.setText(getString(R.string.magic_create_text_on_x, domain)); + binding.finePrint.setVisibility(View.INVISIBLE); + } + binding.createAccount.setOnClickListener(v -> { + try { + final String username = binding.username.getText().toString(); + final Jid jid; + if (this.domain == null) { + jid = Jid.ofLocalAndDomain(username, Config.MAGIC_CREATE_DOMAIN); + } else { + jid = Jid.ofLocalAndDomain(username, this.domain); + } + if (!jid.getEscapedLocal().equals(jid.getLocal()) || username.length() < 3) { + binding.username.setError(getString(R.string.invalid_username)); + binding.username.requestFocus(); + } else { + binding.username.setError(null); + Account account = xmppConnectionService.findAccountByJid(jid); + if (account == null) { + account = new Account(jid, CryptoHelper.createPassword(new SecureRandom())); + account.setOption(Account.OPTION_REGISTER, true); + account.setOption(Account.OPTION_DISABLED, true); + account.setOption(Account.OPTION_MAGIC_CREATE, true); + if (this.preAuth != null) { + account.setKey(Account.PRE_AUTH_REGISTRATION_TOKEN, this.preAuth); + } + xmppConnectionService.createAccount(account); + } + Intent intent = new Intent(MagicCreateActivity.this, EditAccountActivity.class); + intent.putExtra("jid", account.getJid().asBareJid().toString()); + intent.putExtra("init", true); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); + Toast.makeText(MagicCreateActivity.this, R.string.secure_password_generated, Toast.LENGTH_SHORT).show(); + StartConversationActivity.addInviteUri(intent, getIntent()); + startActivity(intent); + } + } catch (IllegalArgumentException e) { + binding.username.setError(getString(R.string.invalid_username)); + binding.username.requestFocus(); + } + }); + binding.username.addTextChangedListener(this); + } - } + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { + } - } + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { - @Override - public void afterTextChanged(Editable s) { - if (s.toString().trim().length() > 0) { - try { - mFullJidDisplay.setVisibility(View.VISIBLE); - Jid jid = Jid.of(s.toString().toLowerCase(), Config.MAGIC_CREATE_DOMAIN, null); - mFullJidDisplay.setText(getString(R.string.your_full_jid_will_be, jid.toEscapedString())); - } catch (IllegalArgumentException e) { - mFullJidDisplay.setVisibility(View.INVISIBLE); - } + } - } else { - mFullJidDisplay.setVisibility(View.INVISIBLE); - } - } + @Override + public void afterTextChanged(Editable s) { + if (s.toString().trim().length() > 0) { + try { + binding.fullJid.setVisibility(View.VISIBLE); + final Jid jid; + if (this.domain == null) { + jid = Jid.ofLocalAndDomain(s.toString(), Config.MAGIC_CREATE_DOMAIN); + } else { + jid = Jid.ofLocalAndDomain(s.toString(), this.domain); + } + binding.fullJid.setText(getString(R.string.your_full_jid_will_be, jid.toEscapedString())); + } catch (IllegalArgumentException e) { + binding.fullJid.setVisibility(View.INVISIBLE); + } + + } else { + binding.fullJid.setVisibility(View.INVISIBLE); + } + } } diff --git a/src/conversations/java/eu/siacs/conversations/utils/SignupUtils.java b/src/conversations/java/eu/siacs/conversations/utils/SignupUtils.java index fc5d874d3..f03a9a7bc 100644 --- a/src/conversations/java/eu/siacs/conversations/utils/SignupUtils.java +++ b/src/conversations/java/eu/siacs/conversations/utils/SignupUtils.java @@ -8,6 +8,7 @@ import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.ui.ConversationsActivity; import eu.siacs.conversations.ui.EditAccountActivity; +import eu.siacs.conversations.ui.MagicCreateActivity; import eu.siacs.conversations.ui.ManageAccountActivity; import eu.siacs.conversations.ui.PickServerActivity; import eu.siacs.conversations.ui.StartConversationActivity; @@ -15,6 +16,17 @@ import eu.siacs.conversations.ui.WelcomeActivity; public class SignupUtils { + public static boolean isSupportTokenRegistry() { + return true; + } + + public static Intent getTokenRegistrationIntent(final Activity activity, String domain, String preauth) { + final Intent intent = new Intent(activity, MagicCreateActivity.class); + intent.putExtra(MagicCreateActivity.EXTRA_DOMAIN, domain); + intent.putExtra(MagicCreateActivity.EXTRA_PRE_AUTH, preauth); + return intent; + } + public static Intent getSignUpIntent(final Activity activity) { return getSignUpIntent(activity, false); } diff --git a/src/conversations/res/layout/magic_create.xml b/src/conversations/res/layout/magic_create.xml index e08dd84c8..51ef104ea 100644 --- a/src/conversations/res/layout/magic_create.xml +++ b/src/conversations/res/layout/magic_create.xml @@ -1,97 +1,112 @@ - + - + - + + + + + android:fillViewport="true"> - - - - - - -