Skip to content

Commit

Permalink
Update regex for email address
Browse files Browse the repository at this point in the history
  • Loading branch information
yostyle committed Oct 23, 2023
1 parent 8bfd5f7 commit 6630c63
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,23 @@

package org.matrix.android.sdk.api.extensions

import java.util.regex.Pattern

const val emailPattern = "^[a-zA-Z0-9_!#\$%&'*+/=?`{|}~^-]+(?:\\.[a-zA-Z0-9_!#\$%&'*+/=?`{|}~^-]+)*@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*\$"
val emailAddress: Pattern = Pattern.compile(emailPattern)

fun CharSequence.ensurePrefix(prefix: CharSequence): CharSequence {
return when {
startsWith(prefix) -> this
else -> "$prefix$this"
}
}

/**
* Check if a CharSequence is an email.
*/
fun CharSequence.isEmail() = emailAddress.matcher(this).matches()

/**
* Append a new line and then the provided string.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

package org.matrix.android.sdk.internal.auth.login

import android.util.Patterns
import org.matrix.android.sdk.api.auth.LoginType
import org.matrix.android.sdk.api.auth.login.LoginProfileInfo
import org.matrix.android.sdk.api.auth.login.LoginWizard
import org.matrix.android.sdk.api.auth.registration.RegisterThreePid
import org.matrix.android.sdk.api.extensions.isEmail
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.util.JsonDict
import org.matrix.android.sdk.internal.auth.AuthAPI
Expand Down Expand Up @@ -59,7 +59,7 @@ internal class DefaultLoginWizard(
initialDeviceName: String,
deviceId: String?
): Session {
val loginParams = if (Patterns.EMAIL_ADDRESS.matcher(login).matches()) {
val loginParams = if (login.isEmail()) {
PasswordLoginParams.thirdPartyIdentifier(
medium = ThreePidMedium.EMAIL,
address = login,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.google.i18n.phonenumbers.NumberParseException
import com.google.i18n.phonenumbers.PhoneNumberUtil
import org.matrix.android.sdk.api.MatrixPatterns
import org.matrix.android.sdk.api.extensions.ensurePrefix
import org.matrix.android.sdk.api.extensions.isEmail

fun Boolean.toOnOff() = if (this) "ON" else "OFF"

Expand All @@ -29,7 +30,7 @@ inline fun <T> T.ooi(block: (T) -> Unit): T = also(block)
/**
* Check if a CharSequence is an email.
*/
fun CharSequence.isEmail() = Patterns.EMAIL_ADDRESS.matcher(this).matches()
fun CharSequence.isEmail() = this.isEmail()

fun CharSequence.isMatrixId() = MatrixPatterns.isUserId(this.toString())

Expand Down

0 comments on commit 6630c63

Please sign in to comment.