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

Unverified Commit 0f92b0fb authored by Sunik Kupfer's avatar Sunik Kupfer Committed by Ricki Hirner
Browse files

291 clean up oauth mess (bitfireAT/davx5#307)



* Move GoogleOAuth members into GoogleLoginFragment

* Require login flow capable browser and notify user if missing

* Receive AppAuth redirects only in standard and gplay flavor

* Set davx5 as user-agent for AppAuth connection builder

* Re-authentication in Account settings

* Catch unauthorized exceptions at collection refresh and notify user to re-authenticate

* Suggest email address on account creation

* Set contact groups default setting as per-contact categories for oauth logins

* Add authentication to debug info, minor other changes

* Better error handling; don't pre-set group type

---------

Co-authored-by: default avatarRicki Hirner <hirner@bitfire.at>
parent 6a2c3663
Loading
Loading
Loading
Loading
+3 −6
Original line number Diff line number Diff line
@@ -28,10 +28,6 @@ android {

        buildConfigField "String", "userAgent", "\"DAVx5\""

        manifestPlaceholders = [
            'appAuthRedirectScheme': applicationId
        ]

        testInstrumentationRunner "at.bitfire.davdroid.CustomTestRunner"

        kapt {
@@ -72,8 +68,9 @@ android {
    }

    sourceSets {
        androidTest.java.srcDirs = [ "src/androidTest/java" ]
        androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
        androidTest {
            assets.srcDirs += files("$projectDir/schemas".toString())
        }
    }

    signingConfigs {
+4 −0
Original line number Diff line number Diff line
@@ -64,6 +64,10 @@

        <service android:name=".ForegroundService"/>

        <!-- Remove the node added by AppAuth -->
        <activity android:name="net.openid.appauth.RedirectUriReceiverActivity"
            tools:node="remove"/>

        <activity android:name=".ui.intro.IntroActivity" android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".ui.AccountsActivity"
+0 −36
Original line number Diff line number Diff line
/*
 * Copyright © All Contributors. See LICENSE and AUTHORS in the root directory for details.
 */

package at.bitfire.davdroid.network

import android.net.Uri
import at.bitfire.davdroid.BuildConfig
import net.openid.appauth.AuthorizationRequest
import net.openid.appauth.AuthorizationServiceConfiguration
import net.openid.appauth.ResponseTypeValues

object GoogleOAuth {

    // davx5integration@gmail.com (for davx5-ose)
    private const val CLIENT_ID = "1069050168830-eg09u4tk1cmboobevhm4k3bj1m4fav9i.apps.googleusercontent.com"

    val SCOPES = arrayOf(
        "https://www.googleapis.com/auth/calendar",     // CalDAV
        "https://www.googleapis.com/auth/carddav"       // CardDAV
    )

    private val serviceConfig = AuthorizationServiceConfiguration(
        Uri.parse("https://accounts.google.com/o/oauth2/v2/auth"),
        Uri.parse("https://oauth2.googleapis.com/token")
    )

    fun authRequestBuilder(clientId: String?) =
        AuthorizationRequest.Builder(
            serviceConfig,
            clientId ?: CLIENT_ID,
            ResponseTypeValues.CODE,
            Uri.parse(BuildConfig.APPLICATION_ID + ":/oauth/redirect")
        )

}
+3 −2
Original line number Diff line number Diff line
@@ -293,11 +293,12 @@ class HttpClient private constructor(
    }


    private object UserAgentInterceptor: Interceptor {
    object UserAgentInterceptor: Interceptor {

        // use Locale.ROOT because numbers may be encoded as non-ASCII characters in other locales
        private val userAgentDateFormat = SimpleDateFormat("yyyy/MM/dd", Locale.ROOT)
        private val userAgentDate = userAgentDateFormat.format(Date(BuildConfig.buildTime))
        private val userAgent = "${BuildConfig.userAgent}/${BuildConfig.VERSION_NAME} ($userAgentDate; dav4jvm; " +
        val userAgent = "${BuildConfig.userAgent}/${BuildConfig.VERSION_NAME} ($userAgentDate; dav4jvm; " +
                "okhttp/${OkHttp.VERSION}) Android/${Build.VERSION.RELEASE}"

        init {
+13 −2
Original line number Diff line number Diff line
@@ -10,13 +10,24 @@ import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import net.openid.appauth.AppAuthConfiguration
import net.openid.appauth.AuthorizationService
import java.net.HttpURLConnection
import java.net.URL

@Module
@InstallIn(SingletonComponent::class)
object OAuthModule {

    @Provides
    fun authorizationService(@ApplicationContext context: Context): AuthorizationService = AuthorizationService(context)

    fun authorizationService(@ApplicationContext context: Context): AuthorizationService =
        AuthorizationService(context,
            AppAuthConfiguration.Builder()
                .setConnectionBuilder { uri ->
                    val url = URL(uri.toString())
                    (url.openConnection() as HttpURLConnection).apply {
                        setRequestProperty("User-Agent", HttpClient.UserAgentInterceptor.userAgent)
                    }
                }.build()
        )
}
 No newline at end of file
Loading