Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

acquireTokenSilent fails to return or throw an exception. (when used in Coil's ImageRequest). #1898

Open
Innova133 opened this issue Sep 8, 2023 · 1 comment
Assignees
Labels
Inevstigation Engineering team as looked into the issue and needs deeper investigation to provide a conclusion
Projects

Comments

@Innova133
Copy link

Innova133 commented Sep 8, 2023

Describe the bug
I need to load an image that requires a Bearer token as a request header. When I try to retrieve the token from MSAL, the call never returns, and does not throw an exception either. This forces me to cache the token in my app, and used a cached, possibly expired token when I request the image.

Smartphone (please complete the following information):

I don't think any of these parameters matter, but I tested on:

  • Device: Pixel Fold and Pixel 6 Pro
  • Android Version: Android 13, TQ3C.230805.001.B2 and TQ3A.230805.001
  • Browser Chrome
  • MSAL Version 4.7.0

Stacktrace
Application Not Responding error happens.

To Reproduce
Steps to reproduce the behavior:

Have an AsyncImage in your Jetpack Compose layout:

                AsyncImage(
                    model = ImageRequest.Builder(LocalContext.current)
                        .data(imageUrl)
                        .crossfade(true)
                        .setHeader("authorization", "Bearer ${MSALAuth.getToken(LocalContext.current)}")    //If you delete this line, the call will complete successfully.
                        .build(),
                    contentDescription = "Test",
                    contentScale = ContentScale.Crop,
                    modifier = Modifier.clip(CircleShape)
                )

The MSALAuth.getToken() method is:

fun getToken(context: Context): String {
            try {
                println("Getting Token from MSAL")
                if (account == null) {
                    singleAccountApp =
                        PublicClientApplication.createSingleAccountPublicClientApplication(
                            context,
                            R.raw.msal
                        )
                    val acctResult = singleAccountApp.currentAccount
                    account = acctResult?.currentAccount
                }
                println("About to get token silently.") //. This is the last line that prints out, then application hangs here.
                val authResult = singleAccountApp.acquireTokenSilent(
                    getTokenParams(TOKEN_SCOPE)
                )!!
                println("MSAL Token retrieved.")
                return authResult.accessToken
            } catch (exception: Exception) {
                println(exception.localizedMessage)
                exception.printStackTrace()
                return ""
            }
        }

If related to user experience, use the format:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

If related to development, please provide relevant configuration details necessary to understand your problem including any relevant traces, logs, or otherwise.

Expected behavior
A token should be returned, or exception thrown if interactive auth is required.

Actual Behavior
The application will get a not responding error.

Screenshots
If applicable, add screenshots to help explain your problem.

Additional context

I have created a simple project that can reproduce this (with proper Azure setup), and attached. In order to finish configuring, add the correct values in:

  1. app/src/main/res/raw/msal.json (client_id, redirect_uri, tenant_id)
  2. app/src/main/AndroidManifest.xml (android:host, android:path in the BrowserTabActivity)
  3. app/src/main/authentication/MSALAuth.kt (Token Scope)
    SilentTokenHang.zip

Please note: Do not include sensitive information like PII, OII, credentials, secrets, and tokens.

For privacy/security issues please see instructions here

@negoe negoe added this to In Progress in Bug Triage Oct 4, 2023
@negoe negoe self-assigned this Oct 4, 2023
@negoe negoe added the Issue Triage The engineering team has looked into the issue, understood the issue, labelled/classified the issue label Oct 4, 2023
@negoe negoe added Inevstigation Engineering team as looked into the issue and needs deeper investigation to provide a conclusion and removed Issue Triage The engineering team has looked into the issue, understood the issue, labelled/classified the issue labels Oct 16, 2023
@sam-innes
Copy link

sam-innes commented Nov 20, 2023

@Innova133 I've been having this issue too and I've found that running the method in a new thread resolved it - could you try and make the request to getToken in a new thread and see if that fixed the issue?

For @negoe this appears to be an issue either in the way that the method is called in a way that's unexpected (i.e. from the main thread, in which case it's probably worth a documentation update) or a threading issue where the PublicClientApplication is using a promise that's blocking the main thread (line 1816 will hang forever if run in the main thread - gets stuck at the red line):
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Inevstigation Engineering team as looked into the issue and needs deeper investigation to provide a conclusion
Projects
Bug Triage
  
In Progress
Development

No branches or pull requests

3 participants