openid / AppAuth-Android

Android client SDK for communicating with OAuth 2.0 and OpenID Connect providers.
https://openid.github.io/AppAuth-Android
Apache License 2.0
2.89k stars 891 forks source link

Run AuthService outside of activity context #333

Open Smedzlatko opened 6 years ago

Smedzlatko commented 6 years ago

I suddently ran into a weird issue when running AppAuth in my android app in an emulator. Everything works fine on phone, as far as I am concerned but the following exception is thrown inside emulator:

04-25 11:29:52.582 5103-5103/com.myfuturity.futurity W/System.err: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

I tracked the issue down and we are not injecting activity context into the auth state manager and it is apparently needed. We are using KOIN for dependency injection (including context) and all our AppAuth services are injected via Koin.

Is there some workaround that would enable us running AppAuth with application context (that's the one being injected)? Setting the FLAG_NEW_TASK to the auth intent does not work, unfortunately.

Thank you for the clarification.

chenthorne commented 6 years ago

We had this same issue and resolved it by using an activity context for a one off instance of AuthorizationService just in our login activity and cleaning it up in onStop. We inject the one based on application context everywhere else.

iainmcgin commented 6 years ago

I should probably expose a variant of getAuthorizationRequestIntent that allows you to specify the completion and cancelation PendingIntents, so that you can call startActivity yourself with the correct context. Alternatively, I could allow the context to be overridden with a parameter passed to performAuthorizationRequest.

Manukam commented 4 years ago

Hi, What is the recommended way of doing this? I am stuck on this error and can't seem to find a way to get around this.

halysongoncalves commented 4 years ago

Any news?

Moes81 commented 3 years ago

Someone already figured out a workaround? We also encapsulated the entire authorization process into a separate module, while using KOIN to inject the androidContext.

rasmusohrstig commented 3 years ago

The workaround is already mentioned by @clenthorne: https://github.com/openid/AppAuth-Android/issues/333#issuecomment-384731640

You need to maintain two different flavours of AuthorizationService: One that takes an Activity and is recreated for each new Activity instance. This one is used to launch Custom tabs. One that takes application context and is created once for the app. This one is used for making authorized network requests.

If you are using a DI framework you can solve this by using two different Qualifier annotation classes for the different flavours.

Omara8 commented 3 years ago

We had this same issue and resolved it by using an activity context for a one off instance of AuthorizationService just in our login activity and cleaning it up in onStop. We inject the one based on application context everywhere else.

this approach is not working for me, i can't bypass the crash at all

raffaelgyr commented 1 month ago

We had this same issue and resolved it by using an activity context for a one off instance of AuthorizationService just in our login activity and cleaning it up in onStop. We inject the one based on application context everywhere else.

If that is the intended solution, I think it would be great if AuthorizationService had a super class that is intended to be initialized by an application Context.