Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2cc52015 authored by Fahim Salam Chowdhury's avatar Fahim Salam Chowdhury 👽
Browse files

chore: update according to review

parent 1259f7de
Loading
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -147,6 +147,7 @@ configurations {
dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib:${versions.kotlin}"
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3'
    testImplementation 'org.junit.jupiter:junit-jupiter:5.8.1'
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.3'

    implementation "com.google.dagger:hilt-android:${versions.hilt}"
+2 −2
Original line number Diff line number Diff line
@@ -48,7 +48,7 @@ import java.util.logging.Level;

import at.bitfire.davdroid.R;
import at.bitfire.davdroid.log.Logger;
import at.bitfire.davdroid.util.SSOUtils;
import at.bitfire.davdroid.util.SsoUtils;

public class SsoGrantPermissionActivity extends AppCompatActivity {

@@ -115,7 +115,7 @@ public class SsoGrantPermissionActivity extends AppCompatActivity {

        // create token
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        String userId = SSOUtils.INSTANCE.sanitizeUserId(account.name);
        String userId = SsoUtils.INSTANCE.sanitizeUserId(account.name);

        saveToken(token, account.name);
        setResultData(token, userId, serverUrl);
+2 −2
Original line number Diff line number Diff line
@@ -19,7 +19,7 @@ import at.bitfire.davdroid.resource.LocalAddressBook
import at.bitfire.davdroid.syncadapter.AccountUtils
import at.bitfire.davdroid.syncadapter.PeriodicSyncWorker
import at.bitfire.davdroid.syncadapter.SyncUtils
import at.bitfire.davdroid.util.SSOUtils
import at.bitfire.davdroid.util.SsoUtils
import at.bitfire.davdroid.util.setAndVerifyUserData
import at.bitfire.ical4android.TaskProvider
import at.bitfire.vcard4android.GroupMethod
@@ -139,7 +139,7 @@ class AccountSettings(
                        bundle.putString(KEY_EMAIL_ADDRESS, credentials.userName)
                    }

                    val userId = SSOUtils.sanitizeUserId(credentials.userName)
                    val userId = SsoUtils.sanitizeUserId(credentials.userName)
                    bundle.putString(NCAccountUtils.Constants.KEY_USER_ID, userId)
                }

+8 −7
Original line number Diff line number Diff line
@@ -16,17 +16,19 @@

package at.bitfire.davdroid.util

object SSOUtils {
object SsoUtils {

    private const val EELO_EMAIL_DOMAIN = "@e.email"

    /**
     * Murena account's userId is set same as it's email address.
     * For old accounts (@e.email) userId = email.
     * For new accounts (@murena.io) & other NC accounts userId is first part of email (ex: for email abc@murena.io, userId is abc).
     * For api requests, we needed to pass the actual userId. This method remove the unwanted part (ex: @murena.io) from the userId
     */
    fun sanitizeUserId(userId: String): String {
        val eeloMailEndPart = "@e.email"

        if (userId.endsWith(eeloMailEndPart)) {
    fun sanitizeUserId(param: String): String {
        val userId = param.trim()
        if (userId.endsWith(EELO_EMAIL_DOMAIN, ignoreCase = true)) {
            return userId
        }

@@ -34,7 +36,6 @@ object SSOUtils {
            return userId
        }

       return userId.split("@").dropLastWhile { it.isEmpty() }
           .toTypedArray()[0]
        return userId.substringBefore("@")
    }
}
+56 −0
Original line number Diff line number Diff line
/*
 * Copyright MURENA SAS 2024
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package at.bitfire.davdroid.util

import org.junit.jupiter.api.Assertions.*

import org.junit.jupiter.api.Test

class SsoUtilsTest {

    @Test
    fun `test sanitizeUserId with empty input`() {
        val userId = ""
        val expected = ""
        val actual = SsoUtils.sanitizeUserId(userId)
        assertEquals(expected, actual)
    }

    @Test
    fun `test sanitizeUserId with input without '@'`() {
        val userId = "username"
        val expected = "username"
        val actual = SsoUtils.sanitizeUserId(userId)
        assertEquals(expected, actual)
    }

    @Test
    fun `test sanitizeUserId with case sensitivity`() {
        val userId = "User@E.EMAIL"
        val expected = "User@E.EMAIL"
        val actual = SsoUtils.sanitizeUserId(userId)
        assertEquals(expected, actual)
    }

    @Test
    fun `test sanitizeUserId with leading or trailing spaces`() {
        val userId = " user@domain.com "
        val expected = "user"
        val actual = SsoUtils.sanitizeUserId(userId)
        assertEquals(expected, actual)
    }
}