Skip to content

Commit

Permalink
Fix email address pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
yostyle committed Oct 18, 2023
1 parent f088d7c commit 37a4661
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2020 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package fr.gouv.tchap.android.sdk.api.auth.login

import org.matrix.android.sdk.api.auth.LoginType
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.internal.auth.AuthAPI
import org.matrix.android.sdk.internal.auth.PendingSessionStore
import org.matrix.android.sdk.internal.auth.SessionCreator
import org.matrix.android.sdk.internal.auth.data.PasswordLoginParams
import org.matrix.android.sdk.internal.auth.data.ThreePidMedium
import org.matrix.android.sdk.internal.auth.db.PendingSessionData
import org.matrix.android.sdk.internal.auth.login.DefaultLoginWizard
import org.matrix.android.sdk.internal.network.executeRequest

internal class TchapLoginWizard(
private val authAPI: AuthAPI,
private val sessionCreator: SessionCreator,
private val pendingSessionStore: PendingSessionStore
) : DefaultLoginWizard(authAPI, sessionCreator, pendingSessionStore) {

private var pendingSessionData: PendingSessionData = pendingSessionStore.getPendingSessionData() ?: error("Pending session data should exist here")

override suspend fun login(
login: String,
password: String,
initialDeviceName: String,
deviceId: String?
): Session {
val loginParams = PasswordLoginParams.thirdPartyIdentifier(
medium = ThreePidMedium.EMAIL,
address = login,
password = password,
deviceDisplayName = initialDeviceName,
deviceId = deviceId
)
val credentials = executeRequest(null) {
authAPI.login(loginParams)
}

return sessionCreator.createSession(credentials, pendingSessionData.homeServerConnectionConfig, LoginType.PASSWORD)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package fr.gouv.tchap.android.sdk.internal.session.room

import android.util.Patterns
import org.matrix.android.sdk.api.query.QueryStringValue
import org.matrix.android.sdk.api.session.events.model.EventType.STATE_ROOM_CREATE
import org.matrix.android.sdk.api.session.room.model.Membership
Expand All @@ -27,6 +26,7 @@ import org.matrix.android.sdk.internal.database.query.where
import org.matrix.android.sdk.internal.session.SessionScope
import org.matrix.android.sdk.internal.session.room.DefaultRoomGetter
import org.matrix.android.sdk.internal.session.room.RoomFactory
import java.util.regex.Pattern
import javax.inject.Inject

@SessionScope
Expand All @@ -35,6 +35,10 @@ internal class TchapRoomGetter @Inject constructor(
private val roomFactory: RoomFactory
) : DefaultRoomGetter(realmSessionProvider, roomFactory) {

// Regular expression in accordance with RFC 5322 for restricting consecutive, leading and trailing dots
private val emailPattern = "^[a-zA-Z0-9_!#\$%&'*+/=?`{|}~^-]+(?:\\.[a-zA-Z0-9_!#\$%&'*+/=?`{|}~^-]+)*@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*\$"
private val emailAddress = Pattern.compile(emailPattern)

override fun getDirectRoomWith(otherUserId: String): String? {
val directRoomMemberships = realmSessionProvider.withRealm { realm ->
// get direct room entities matching the given user id
Expand Down Expand Up @@ -63,7 +67,7 @@ internal class TchapRoomGetter @Inject constructor(
?: directRoomMemberships.firstOrNull { it.first == Membership.INVITE && it.second == Membership.JOIN }?.roomId // invite - join
?: directRoomMemberships.firstOrNull { it.first == Membership.JOIN && it.second == Membership.INVITE }?.roomId // join - invite
?: directRoomMemberships // otherUserId is an email
.takeIf { Patterns.EMAIL_ADDRESS.matcher(otherUserId).matches() }
.takeIf { emailAddress.matcher(otherUserId).matches() }
?.firstOrNull { it.first == Membership.JOIN }
?.roomId
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package org.matrix.android.sdk.internal.auth

import android.net.Uri
import dagger.Lazy
import fr.gouv.tchap.android.sdk.api.auth.login.TchapLoginWizard
import okhttp3.OkHttpClient
import org.matrix.android.sdk.api.MatrixPatterns
import org.matrix.android.sdk.api.MatrixPatterns.getServerName
Expand All @@ -39,7 +40,6 @@ import org.matrix.android.sdk.api.util.appendParamToUrl
import org.matrix.android.sdk.internal.SessionManager
import org.matrix.android.sdk.internal.auth.data.WebClientConfig
import org.matrix.android.sdk.internal.auth.db.PendingSessionData
import org.matrix.android.sdk.internal.auth.login.DefaultLoginWizard
import org.matrix.android.sdk.internal.auth.login.DirectLoginTask
import org.matrix.android.sdk.internal.auth.login.QrLoginTokenTask
import org.matrix.android.sdk.internal.auth.registration.DefaultRegistrationWizard
Expand Down Expand Up @@ -338,7 +338,7 @@ internal class DefaultAuthenticationService @Inject constructor(
return currentLoginWizard
?: let {
pendingSessionData?.homeServerConnectionConfig?.let {
DefaultLoginWizard(
TchapLoginWizard(
buildAuthAPI(it),
sessionCreator,
pendingSessionStore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import org.matrix.android.sdk.internal.network.executeRequest
import org.matrix.android.sdk.internal.session.content.DefaultContentUrlResolver
import org.matrix.android.sdk.internal.session.contentscanner.DisabledContentScannerService

internal class DefaultLoginWizard(
internal open class DefaultLoginWizard(
private val authAPI: AuthAPI,
private val sessionCreator: SessionCreator,
private val pendingSessionStore: PendingSessionStore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@

package im.vector.app.core.extensions

import android.util.Patterns
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 java.util.regex.Pattern

// Tchap: regular expression in accordance with RFC 5322 for restricting consecutive, leading and trailing dots
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 Boolean.toOnOff() = if (this) "ON" else "OFF"

Expand All @@ -29,7 +33,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() = emailAddress.matcher(this).matches()

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

Expand Down

0 comments on commit 37a4661

Please sign in to comment.