AppAuth-Android
AppAuth-Android copied to clipboard
Set tokens manually!
Hi, I am using android webview app to load angular application. To make sessions longer I have used AppAuth library to login from mobile to get and store tokens. After getting token I pass tokens to local storage so that angular application can process token and assume that user is logged in and proceed further. After angular app takes tokens, it changes values of token and android is unaware of it. So when app is restarted, android app uses old tokens and hence sign in fails.
How can I set token in appAuth state manually?
Thanks in advance!
You can call update
on AuthState
with the latest token response.
I don't know how your app's structured but I'd recommend against mixing the two contexts (native and the SPA) this way. I'd either manage the login session exclusively in the SPA or natively.
Thank You @agologan Alexandru, I can use, update method, which require tokenResponse object with other values. Shd i use a dummy token Response? As my angular keycloak config already processed the request and generated new idTokens, refresh token and access token. I want my android app to be aware of the tokens which are being modified in angular application inside webview.
You've probably figured this out by now, but the only dummy part of the AuthorizationResponse
is the associated request
which in your case is done in angular, otherwise the rest of the options are just the same as the response you get in the js context.
I've bumped into a similar need. We have the capability of signing in to a platform either on an external OAuth site, or through AppAuth in the app, and if we do the prior we would like to plug the tokens we get back into AppAuth for future token refreshes.
In short I'm pondering if it's even reasonable to try and manually instantiate an AuthState and try to fill it with already existing tokens (as well as known URLs for auth/tokens, client ID, etc), as things like doing a refresh seemingly requires a stored AuthorizationResponse object as well.
I'm leaning towards just rolling my own token refresh service for this specialized use-case, but it would be nice to use AppAuth for this as well.
Edit: And after posting I of course get around to figure it out too, minimal testing, but so far so good.
The steps:
- Instantiate
AuthorizationServiceConfiguration
with your params. - Instantiate
AuthState
with the above as param. - Instantiate
AuthorizationRequest
with your params. - Instantiate
AuthorizationResponse.Builder
with the above as param. - Set a garbage auth code with
.setAuthorizationCode()
on the builder. - Build the auth response
.build()
and update the auth state with it.update()
. - Get a token request from your auth response with
.createTokenExchangeRequest()
. - Instantiate
TokenResponse.Builder
with the above as param. - Set the access and refresh tokens on the builder with
.setAccessToken()
and.setRefreshToken()
. - Build the token response
.build()
and update the auth state with it.update()
. - Store your auth state, done!
It was fairly straight forward in the end, even if there's a number of things to do.