question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hooking MSAL into OkHttp's Interceptors

See original GitHub issue

Is your feature request related to a problem? Please describe.

I’d like to be able to fetch the MSAL auth token synchronously, so I can hook the authentication mechanism into OkHttp through an Interceptor.

Right now, on my app, all my repositories inherit from a BaseRepository, this BaseRepository has a function named fetchMsalToken(). This fetchMsalToken() function runs the aquireTokenSilentAsync() through RxJava. Thanks to that I can flat map this function before calling any of my endpoints, so I can be sure that I’m refreshing the auth token before each call to the backend.

Ideally, I’d like to do all this within an Interceptor, somehow run the aquireTokenSilentAsync() synchronously, and set the auth token on the headers of the request.

Describe the solution you’d like Is it there any way to run the aquireTokenSilentAsync() synchronously within an Interceptor, so I can refresh the auth token there and keep any code related to this, out of my repositories.

Describe alternatives you’ve considered As I mentioned, what I’ve done so far it’s creating a RxJava function that runs the aquireTokenSilentAsync(), and using RxJava’s flatmap function, I hook that into my API calls.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
4gus71ncommented, Jun 17, 2020

Thanks for the quick response @iambmelt ! 🙌 Okay, cool, I’m going to try to handle things from my side then, with that “patch”. It’d be nice having the library handle these things though, the library works fine but as soon as you start trying to plug the library into some other technologies (Dagger, Retrofit) things start to work a bit funny. I get that it is equally difficult to handle these things on the library or in the client app, but maybe it would be useful introducing something like a BaseApplication class from which we could extend that guarantees that we always have an access token available. Thanks again!

0reactions
Keepertjecommented, Jun 24, 2020

Hello! 👋

I just recently updated to the 1.5.0 version and I started getting these errors from the MSAL library:

2020-06-17 14:36:18.826 20322-20322/com.example.example W/ExampleApp: com.microsoft.identity.client.exception.MsalClientException: The same command was already received and is being processed.
2020-06-17 14:36:18.826 20322-20322/com.example.example W/ExampleApp: duplicate_command
2020-06-17 14:36:18.827 20322-20322/com.example.example W/ExampleApp: The same command was already received and is being processed.
2020-06-17 14:36:18.829 20322-20322/com.example.example W/ExampleApp: com.microsoft.identity.client.exception.MsalClientException: The same command was already received and is being processed.
        at com.microsoft.identity.client.internal.controllers.MsalExceptionAdapter.msalExceptionFromBaseException(MsalExceptionAdapter.java:51)
        at com.microsoft.identity.client.SingleAccountPublicClientApplication$3.onError(SingleAccountPublicClientApplication.java:339)
        at com.microsoft.identity.client.SingleAccountPublicClientApplication$3.onError(SingleAccountPublicClientApplication.java:325)
        at com.microsoft.identity.common.internal.controllers.CommandDispatcher$1.run(CommandDispatcher.java:93)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:215)
        at android.app.ActivityThread.main(ActivityThread.java:6939)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:870)

I am getting the same error but only in combination with MS Authenticator. We are trying to make a plugin to get MSAL working with Ionic 5/ Capacitor.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Interceptors - OkHttp
A call to chain.proceed(request) is a critical part of each interceptor's implementation. This simple-looking method is where all the HTTP work happens, ...
Read more >
Using MSAL Interceptor to Call APIs with User Access Token
Github: https://github.com/tamani-coding/angular- msal - interceptor -exampleSee Part 1: https://youtu.be/TkCKqeYjpv0(00:00): Intro and ...
Read more >
Adding Interceptors in OkHTTP - Baeldung
A network interceptor hooks into the network level and is an ideal place to put retry logic.
Read more >
Band-aiding legacy networking libraries with Retrofit and OkHttp
For those unaware, OkHttp uses interceptors for not only letting clients hook into calls and modify their request/response bodies, but also for ...
Read more >
Working with Interceptors in OkHttp library - Stack Overflow
You don't need to create your own RequestBody class; that's only necessary when you transform the body. Move the contents of your writeTo ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found