-
Notifications
You must be signed in to change notification settings - Fork 381
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
add functionality to exchange one token to another #150
Conversation
Exchange a token with type 'refresh' for a token with type 'token' | ||
with the same claims but a shorter expiry time. | ||
|
||
The token with typ 'refresh' will be revoked after the exhange |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the normal proceedure to revoke the refresh token? I thought these stayed valid and could be used multiple times.
This looks pretty good, just the q about revoking a refresh token. I could be wrong but my understanding is that they should stick around. Also can you please add some words to the readme and changelog? Thanks :D |
I guess we could make it up to the user to revoke it, or create two different methods exchangeAndRevoke and exchange. What do you think? |
I just realized that you can use the refresh token as a normal token. Is this the case or did I miss something? |
|> Guardian.Claims.ttl | ||
|> Map.delete("ttl") | ||
defp set_ttl(claims, type) do | ||
if type === "refresh" do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to support :refresh
? We use atoms elsewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, but this ensures that we can set a default living time for the refresh tokens
Edit:
I read your comment wrong, the reason I used "refresh" is because guardian currently use "token" as default
@Hanspagh correct you could use it. Once you start using different types of tokens EnsureAuthenticated should check the typ field. I use |
As an example, here's my plug for one of my apps pipeline :api_auth do
plug Guardian.Plug.VerifyHeader, realm: "bearer"
plug Guardian.Plug.EnsureAuthenticated, typ: "access", handler: MyAwesomeApp.Api.V1.ApiAuthErrorHandler
plug Guardian.Plug.LoadResource
end |
Makes sense, my only concern is that if someone isn't aware of this, you could authenticate your self with a refresh token, which defeats the whole purpose of this. So we might consider having EnsureAuthenticated check for the typ "token" per default. What do you think? |
I'm not sure if that would be good or not. I'm thinking about pipelines and setting the token type there. It might be ok. Let me percolate on it a bit. Also I think we should change the default type to 'access' rather than 'token'. Token doesn't really mean anything. |
I think you are right 'access' makes much more sense then token. If we don't make it a default in EnsureAuthenticated as well, I think we should at least inform the user about the implications. |
Any progress on this? |
Hey @Hanspagh thanks for pinging on this one. I think we definitely need an I think what would be great is:
Thoughts? |
I think the overall idea sounds really solid. |
Hmm true facts. I guess there's a couple of ways I can think of that we could deal with that.
I think 2 seems more natural to me. That way the calling code would be responsible: Guardian.exchange(jwt, "refresh", "access") Or in the case where it's many: Guardian.exchange(jwt, ["refresh", "rememberMe"], "access") Thoughts? |
Seems good, I will start working on it |
Here is first shot at the proposed solutions. Still missing docs and being able to modify the token created
|
defp do_exchange(from_typ, to_typ, claims) do | ||
if correct_typ?(claims, from_typ) do | ||
{:ok, resource} = Guardian.serializer.from_token(claims["sub"]) | ||
case encode_and_sign(resource, to_typ, %{}) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we want to drop some of the claims here and use the remaining claims. If we just use an empty map we'll lose any custom claims that were embedded in the 'refresh' token.
I will have a look. I have a very busy work week coming up, but I will be back in action the 27. |
I am sorry for the huge delay on this, but I have fixed the two issues you mentioned namely; to handle atom types and not drop all claims when doing the exchange |
If you don't have anymore feedback, I will update the docs and we can finish this. |
When this pull request will be merged? |
I will update the docs and rebase it this weekend |
Thank you @Hanspagh |
Update README.md & changelog.md
9af62c4
to
5fd4edf
Compare
Okay, I did the rebase, and added some docs. |
I am sorry for reviving this again, but I am pretty sure that we could just merge this? |
Sorry for the delays @Hanspagh, I've not been following this thread. Let me review the changes and see about merging. |
Makes it possible to exchange a token with type 'refresh' to a token with type 'token', like we discussed in #111, it will play nicely with guardianDB once ueberauth/guardian_db#15