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

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

Merge branch '1975-ignore_eeo_one_account_username_modification' into 'main'

1975-ignore_eeo_one_account_username_modification

See merge request !115
parents a19d5c1d e2029278
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -219,7 +219,7 @@ dependencies {
    implementation "commons-httpclient:commons-httpclient:3.1@jar" // remove after entire switch to lib v2
    implementation 'org.apache.jackrabbit:jackrabbit-webdav:2.13.5' // remove after entire switch to lib v2
    implementation 'com.google.code.gson:gson:2.10.1'
    implementation("foundation.e:Nextcloud-Android-Library:1.0.5-release") {
    implementation("foundation.e:Nextcloud-Android-Library:1.0.6-release") {
        exclude group: 'com.gitlab.bitfireAT', module: 'dav4jvm'
        exclude group: 'org.ogce', module: 'xpp3' // unused in Android and brings wrong Junit version
        exclude group: 'com.squareup.okhttp3'
+9 −1
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

@@ -115,13 +116,20 @@ public class SsoGrantPermissionActivity extends AppCompatActivity {

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

        saveToken(token, account.name);
        setResultData(token, userId, serverUrl);
        finish();
    }

    @NonNull
    private String getUserId() {
        final AccountManager accountManager = AccountManager.get(this);
        final String baseUrl = accountManager.getUserData(account, AccountUtils.Constants.KEY_OC_BASE_URL);
        return SsoUtils.INSTANCE.sanitizeUserId(account.name, baseUrl);
    }

    private void setResultData(String token, String userId, String serverUrl) {
        final Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
+33 −11
Original line number Diff line number Diff line
@@ -126,21 +126,18 @@ class AccountSettings(
        @Volatile
        var currentlyUpdating = false

        fun initialUserData(credentials: Credentials?, baseURL: String? = null, cookies: String? = null): Bundle {
        fun initialUserData(credentials: Credentials?, url: String? = null, cookies: String? = null, email: String? = null): Bundle {
            val bundle = Bundle()
            bundle.putString(KEY_SETTINGS_VERSION, CURRENT_VERSION.toString())

            var userName: String? = null

            if (credentials != null) {
                if (credentials.userName != null) {
                    bundle.putString(KEY_USERNAME, credentials.userName)
                    bundle.putString(NCAccountUtils.Constants.KEY_DISPLAY_NAME, credentials.userName)

                    if (credentials.userName.contains("@")) {
                        bundle.putString(KEY_EMAIL_ADDRESS, credentials.userName)
                    }

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

                if (credentials.certificateAlias != null) {
@@ -156,17 +153,42 @@ class AccountSettings(
                }
            }

            if (!baseURL.isNullOrEmpty()) {
                bundle.putString(NCAccountUtils.Constants.KEY_OC_BASE_URL, AccountUtils.getOwnCloudBaseUrl(baseURL))
            }

            if (!cookies.isNullOrEmpty()) {
                bundle.putString(COOKIE_KEY, cookies)
            }

            var baseUrl : String? = null
            if (!url.isNullOrEmpty()) {
                baseUrl = AccountUtils.getOwnCloudBaseUrl(url)
                bundle.putString(NCAccountUtils.Constants.KEY_OC_BASE_URL, baseUrl)
            }

            addUserIdToBundle(bundle, userName, baseUrl)
            addEmailToBundle(bundle, email, userName)

            return bundle
        }

        private fun addUserIdToBundle(bundle: Bundle, userName: String?, baseUrl: String?) {
            userName?.let {
                val userId = SsoUtils.sanitizeUserId(it, baseUrl)
                bundle.putString(NCAccountUtils.Constants.KEY_USER_ID, userId)
            }
        }

        private fun addEmailToBundle(bundle: Bundle, email: String?, userName: String?) {
            if (email != null) {
                bundle.putString(KEY_EMAIL_ADDRESS, email)
                return
            }

            userName?.let {
                if (it.contains("@")) {
                    bundle.putString(KEY_EMAIL_ADDRESS, it)
                }
            }
        }

    }


+1 −1
Original line number Diff line number Diff line
@@ -299,7 +299,7 @@ class AccountDetailsFragment : Fragment() {
                val account = Account(name, accountType)

                // create Android account
                val userData = AccountSettings.initialUserData(credentials, baseURL, config.cookies)
                val userData = AccountSettings.initialUserData(credentials, baseURL, config.cookies, config.calDAV?.emails?.firstOrNull())
                Logger.log.log(Level.INFO, "Creating Android account with initial config", arrayOf(account, userData))

                val accountManager = AccountManager.get(context)
+19 −12
Original line number Diff line number Diff line
@@ -16,26 +16,33 @@

package at.bitfire.davdroid.util

object SsoUtils {
import java.net.URI

    private const val EELO_EMAIL_DOMAIN = "@e.email"
object SsoUtils {

    /**
     * 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
     * This method removes the baseUrl's host part from the accountName to retrieve userId
     * for ex: accountName: abc@murena.io; baseUrl: https://murena.io; so userID: abc
     */
    fun sanitizeUserId(param: String): String {
        val userId = param.trim()
        if (userId.endsWith(EELO_EMAIL_DOMAIN, ignoreCase = true)) {
    fun sanitizeUserId(accountName: String, baseUrl: String?): String {
        val userId = accountName.trim()

        val host = getHost(baseUrl) ?: return userId
        val userNameEndPart = "@$host"

        if (!userId.endsWith(userNameEndPart, ignoreCase = true)) {
            return userId
        }

        if (userId.lastIndexOf("@") < 0) { // not email address
            return userId
        return userId.substring(0, userId.lastIndexOf(userNameEndPart, ignoreCase = true))
    }

    private fun getHost(baseUrl: String?): String? {
        if (baseUrl == null) {
            return null
        }

        return userId.substringBefore("@")
        val uri = URI.create(baseUrl.trim())
        return uri.host
    }
}
Loading