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

feat: Implement JWT refresh token for authorization #2264

Open
wants to merge 1 commit into
base: development
Choose a base branch
from

Conversation

liveHarshit
Copy link
Member

Fixes #2230

@auto-label auto-label bot added the feature label Aug 9, 2019
@iamareebjamal
Copy link
Member

Will test and see

@@ -66,7 +65,7 @@ class AuthService(

fun logout(): Completable {
return Completable.fromAction {
authHolder.token = null
authHolder.accessToken = null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove refresh token as well

}
set(value) {
if (value != null && JWTUtils.isExpired(value))
throw IllegalStateException("Cannot set expired refreshToken")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean this will crash the app or what? User should be logged out in this case

if (headerType == JWT_REFRESH_TOKEN)
authHolder.getRefreshAuthorization()
else
authHolder.getAccessAuthorization()
Copy link
Member

@iamareebjamal iamareebjamal Aug 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, no, no. How does it refresh the token when it is expired?

See the answers here https://stackoverflow.com/questions/22450036/refreshing-oauth-token-using-retrofit-without-modifying-all-calls

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good approach. 👍

@liveHarshit
Copy link
Member Author

While injecting auth service in the authorization, the app is crashing with module definition. Any idea why we can inject auth holder but not auth service?

@liveHarshit
Copy link
Member Author

Problem is how to do API call from the authenticator to refresh token, @iamareebjamal can I use view model in authenticator?

@iamareebjamal
Copy link
Member

See the comment in the same answer. There is a circular dependency

@liveHarshit
Copy link
Member Author

liveHarshit commented Aug 21, 2019

See the comment in the same answer. There is a circular dependency

Are you talking about this: https://stackoverflow.com/a/51360214
Currently, I'm getting infinite calls.

Stack Overflow
We are using Retrofit in our Android app, to communicate with an OAuth2 secured server. Everything works great, we use the RequestInterceptor to include the access token with each call. However there

@iamareebjamal
Copy link
Member

I meant don't use dependency injection there.

Manually create a separate service

@liveHarshit
Copy link
Member Author

Manually create a separate service

I have a doubt, it needs to create a different HTTP client or using the same dependency injection module.

@iamareebjamal
Copy link
Member

Different HTTP client

@iamareebjamal
Copy link
Member

Status?

@aggarwalpulkit596
Copy link
Contributor

If anyone isn't working i can I've already implemented the same in my project 😅

@iamareebjamal
Copy link
Member

Go ahead

@aggarwalpulkit596
Copy link
Contributor

@iamareebjamal i guess the flow will be like if i get 401 in response then i need to use the refresh token to get another jwt token and replace that token from the existing right?

@iamareebjamal
Copy link
Member

iamareebjamal commented Sep 17, 2019

https://stackoverflow.com/questions/22450036/refreshing-oauth-token-using-retrofit-without-modifying-all-calls

Stack Overflow
We are using Retrofit in our Android app, to communicate with an OAuth2 secured server. Everything works great, we use the RequestInterceptor to include the access token with each call. However there

@aggarwalpulkit596
Copy link
Contributor

thanks

@aggarwalpulkit596
Copy link
Contributor

the current mechanism uses an interceptor for attaching the jwt token should I make pr for that first to move it to authenticator ?

@iamareebjamal
Copy link
Member

iamareebjamal commented Sep 18, 2019

Do it in one PR

And, we have to use interceptor, or else, there'll be 2 requests for every unauthenticated resource. It was happening like this before, I changed it to Interceptor. We can have both authenticator and interceptor, but it may be more complicated for you and delay the PR. So, let's keep interceptor for now.

Authenticator only triggers on API failure, meaning for any unauthenticated resource, it'll try things without token and obviously fail, and then trigger the authenticator, which'll attach the token and do the request. This'll make 2 API calls for each resource. Ideal case is using Interceptor + Authenticator, but your choice between Interceptor or A+I

@aggarwalpulkit596
Copy link
Contributor

@iamareebjamal yeah I got figured it out that we have to use interceptor by default and authenticator for refreshing the token

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement refresh tokens authentication
5 participants