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

Commit 6f36a11b authored by Fahim M. Choudhury's avatar Fahim M. Choudhury
Browse files

Merge branch '6198-microsoft-account-integration' into 'main'

Configure Microsoft account integration for official, community and test platforms

See merge request !169
parents 0003ca80 781f3f16
Loading
Loading
Loading
Loading
Loading
+31 −4
Original line number Diff line number Diff line
@@ -126,10 +126,23 @@ android {
                "\"dj0yJmk9dUNqYXZhYWxOYkdRJmQ9WVdrOU1YQnZVRFZoY1ZrbWNHbzlNQT09JnM9Y29uc3VtZXJzZWNyZXQmc3Y9MCZ4PWIw\"",
            )
            buildConfigField("String", "OAUTH_MICROSOFT_CLIENT_ID", "\"${getProperty("MICROSOFT_CLIENT_ID")}\"")

            buildConfigField(
                "String",
                "MICROSOFT_REDIRECT_URI_SUFFIX_COMMUNITY",
                "\"${getProperty("MICROSOFT_REDIRECT_URI_SUFFIX_COMMUNITY")}\"",
            )

            buildConfigField(
                "String",
                "OAUTH_MICROSOFT_REDIRECT_URI",
                "\"msauth://foundation.e.mail/${getProperty("MICROSOFT_REDIRECT_URI_SUFFIX")}\"",
                "MICROSOFT_REDIRECT_URI_SUFFIX_OFFICIAL",
                "\"${getProperty("MICROSOFT_REDIRECT_URI_SUFFIX_OFFICIAL")}\"",
            )

            buildConfigField(
                "String",
                "MICROSOFT_REDIRECT_URI_SUFFIX_TEST",
                "\"${getProperty("MICROSOFT_REDIRECT_URI_SUFFIX_TEST")}\"",
            )

            manifestPlaceholders["appAuthRedirectScheme"] = "foundation.e.mail"
@@ -157,11 +170,25 @@ android {
                "OAUTH_AOL_CLIENT_ID",
                "\"dj0yJmk9cHYydkJkTUxHcXlYJmQ9WVdrOWVHZHhVVXN4VVV3bWNHbzlNQT09JnM9Y29uc3VtZXJzZWNyZXQmc3Y9MCZ4PTdm\"",
            )

            buildConfigField("String", "OAUTH_MICROSOFT_CLIENT_ID", "\"${getProperty("MICROSOFT_CLIENT_ID")}\"")
            
            buildConfigField(
                "String",
                "MICROSOFT_REDIRECT_URI_SUFFIX_COMMUNITY",
                "\"${getProperty("MICROSOFT_REDIRECT_URI_SUFFIX_COMMUNITY")}\"",
            )

            buildConfigField(
                "String",
                "MICROSOFT_REDIRECT_URI_SUFFIX_OFFICIAL",
                "\"${getProperty("MICROSOFT_REDIRECT_URI_SUFFIX_OFFICIAL")}\"",
            )

            buildConfigField(
                "String",
                "OAUTH_MICROSOFT_REDIRECT_URI",
                "\"msauth://foundation.e.mail/${getProperty("MICROSOFT_REDIRECT_URI_SUFFIX")}\"",
                "MICROSOFT_REDIRECT_URI_SUFFIX_TEST",
                "\"${getProperty("MICROSOFT_REDIRECT_URI_SUFFIX_TEST")}\"",
            )

            manifestPlaceholders["appAuthRedirectScheme"] = "foundation.e.mail.debug"
+1 −1
Original line number Diff line number Diff line
@@ -52,7 +52,7 @@ class AppOAuthConfigurationFactory : OAuthConfigurationFactory {
            ),
            authorizationEndpoint = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize",
            tokenEndpoint = "https://login.microsoftonline.com/common/oauth2/v2.0/token",
            redirectUri = BuildConfig.OAUTH_MICROSOFT_REDIRECT_URI,
            redirectUri = MicrosoftRedirectUriGenerator.generate(),
        )
    }

+42 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 e Foundation
 *
 * 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 com.fsck.k9.auth

import com.fsck.k9.BuildConfig
import com.fsck.k9.auth.ReleaseType.Community
import com.fsck.k9.auth.ReleaseType.Official
import com.fsck.k9.auth.ReleaseType.Test
import com.fsck.k9.auth.ReleaseType.Unavailable

object MicrosoftRedirectUriGenerator {
    fun generate(): String {
        val releaseType = ReleaseTypeHelper.getReleaseType()
        val redirectUriSuffix = when (releaseType) {
            Community -> BuildConfig.MICROSOFT_REDIRECT_URI_SUFFIX_COMMUNITY
            Official -> BuildConfig.MICROSOFT_REDIRECT_URI_SUFFIX_OFFICIAL
            Test, Unavailable -> BuildConfig.MICROSOFT_REDIRECT_URI_SUFFIX_TEST
        }

        // For debug build (foundation.e.mail.debug), update the hash of your debug keystore in Microsoft Azure portal,
        // and based on that, set the redirectUriSuffix as it is a combination of both
        // the applicationId and signature hash of the keystore.

        return "msauth://${BuildConfig.APPLICATION_ID}/$redirectUriSuffix"
    }
}
+61 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 e Foundation
 *
 * 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 com.fsck.k9.auth

import android.annotation.SuppressLint
import com.fsck.k9.auth.ReleaseType.Community
import com.fsck.k9.auth.ReleaseType.Official
import com.fsck.k9.auth.ReleaseType.Test
import com.fsck.k9.auth.ReleaseType.Unavailable
import com.fsck.k9.logging.Timber

object ReleaseTypeHelper {

    private const val KEY_SYSTEM_PROPERTY_RELEASE_TYPE = "ro.lineage.releasetype"

    fun getReleaseType(): ReleaseType {
        val property = getSystemProperty(KEY_SYSTEM_PROPERTY_RELEASE_TYPE)

        return when (property) {
            Community.value -> Community
            Official.value -> Official
            Test.value -> Test
            else -> Unavailable
        }
    }
}

enum class ReleaseType(val value: String) {
    Community("community"),
    Official("official"),
    Test("test"),
    Unavailable("")
}

@SuppressLint("PrivateApi")
private fun getSystemProperty(key: String): String {
    return try {
        Class.forName("android.os.SystemProperties")
            .getMethod("get", String::class.java)
            .invoke(null, key) as String
    } catch (e: Exception) {
        Timber.e("Unable to determine system property for $key", e)
        Unavailable.value
    }
}