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

Commit efd2b115 authored by Mohammed Althaf T's avatar Mohammed Althaf T 😊
Browse files

Revert "Add authstate checks for token refresh"

This reverts commit 048ab146.
parent e127a77c
Loading
Loading
Loading
Loading
+7 −32
Original line number Diff line number Diff line
@@ -8,7 +8,6 @@ import at.bitfire.davdroid.BuildConfig
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import foundation.e.accountmanager.token.MurenaTokenManager
import net.openid.appauth.AuthState
import net.openid.appauth.AuthorizationException
import net.openid.appauth.AuthorizationService
@@ -70,20 +69,6 @@ class OAuthInterceptor @AssistedInject constructor(
        // if possible, use cached access token
        val authState = readAuthState() ?: return null

        val authorizationException = authState.authorizationException
        if (authorizationException != null && MurenaTokenManager.isInvalidGrant(authorizationException)) {
            throw AuthorizationException.TokenRequestErrors.INVALID_GRANT
        }

        val expiresAt = authState.accessTokenExpirationTime
        if (expiresAt != null) {
            val refreshDeadline = expiresAt - MurenaTokenManager.earlyTriggerTime
            if (refreshDeadline <= System.currentTimeMillis()) {
                logger.info("Token expired or near expiry, refreshing immediately.")
                authState.needsTokenRefresh = true
            }
        }

        if (authState.isAuthorized && authState.accessToken != null && !authState.needsTokenRefresh) {
            if (BuildConfig.DEBUG)      // log sensitive information (refresh/access token) only in debug builds
                logger.log(Level.FINEST, "Using cached AuthState", authState.jsonSerializeString())
@@ -100,23 +85,13 @@ class OAuthInterceptor @AssistedInject constructor(
                if (BuildConfig.DEBUG)
                    logger.log(Level.FINEST, "Got new AuthState", authState.jsonSerializeString())

                when {
                    accessToken != null -> {
                // persist updated AuthState
                writeAuthState(authState)
                        accessTokenFuture.complete(accessToken)
                    }

                    ex != null -> {
                        logger.log(Level.WARNING, "Token refresh failed: $ex")
                if (ex != null)
                    accessTokenFuture.completeExceptionally(ex)
                    }

                    else -> {
                        // Unexpected: neither token nor exception. treat as failure.
                        accessTokenFuture.completeExceptionally(IllegalStateException("No token, no exception"))
                    }
                }
                else if (accessToken != null)
                    accessTokenFuture.complete(accessToken)
            }

            accessTokenFuture.join()
+1 −1
Original line number Diff line number Diff line
@@ -190,7 +190,7 @@ object MurenaTokenManager {
    }

    // Checks whether the given AuthorizationException indicates an invalid grant (requires re-login).
    fun isInvalidGrant(ex: AuthorizationException?): Boolean {
    private fun isInvalidGrant(ex: AuthorizationException?): Boolean {
        val invalidGrant = AuthorizationException.TokenRequestErrors.INVALID_GRANT
        return ex?.code == invalidGrant.code && ex.error == invalidGrant.error
    }