Skip to content

Rotating refresh tokens #186

Closed
Closed

Description

@kasperwelbers

Hi, I was wondering whether I understand correctly that httr2 (currently) does not support refresh token rotation.

The documentation for req_oauth_refresh says: "When requesting an access token, the server may also return a new refresh token. If this happens, oauth_flow_refresh() will error [...]".

In an application we're working on we use refresh token rotation to mitigate damage from possible token theft. This is mainly for browser clients, but we figured it might make sense for R clients as well. Might this be possible at some point, or do you think client side storage of refresh tokens in R is sufficiently secure that rotation is pointless?

And btw, really cool package. Had the flow running in about a minute!

Activity

  1. hadley commented on Dec 12, 2022

    @hadley
    Member

    Unfortunately I can't remember the details — it might be something that I just didn't implement because I didn't see an immediate need, or it might something that's harder to implement to get the new token stored in the right place. Either way, I'll take a look next time I'm working on httr2 as it would be nice to support it.

  2. jennybc commented on Dec 12, 2022

    @jennybc
    Member

    @kasperwelbers do you know of an API that does this:

    When requesting an access token, the server may also return a new refresh token ...

    My experience is mostly with Google and it specifically does not do this (at least, not in my experience and documentation reading). It's always helpful to have a live specimen to work against.

  3. kasperwelbers commented on Dec 13, 2022

    @kasperwelbers
    Author

    @hadley I think interest in rotating refresh tokens is only more recently on the rise (it is only briefly mentioned in rfc6749 10.4). While it would be easy to just disable the error and return the refresh_token (oauth_flow_refresh returns an httr2_token that has a slot for it) I get that it's tricky to implement in a way that users will safely store the new token. Perhaps a callback function with the refresh token as argument that is called if the refresh token changed?

    @jennybc I actually hadn't run into APIs that use it, but encountered it when setting up an authentication broker for our own APIs. Popular auth brokers like Auth0 are supporting and recommending it, so any API that uses them could use (or start using) it. If you need a test case you could use our auth server. This is in active dev, but I could fire-up a stable mock version.

    For context, I think the rise in interest is due to third-party cookies being phased out, which hits many browser applications that rely on (third-party) auth brokers like auth0. This requires some browser applications to store tokens in localstorage/indexeddb to persist tokens across sessions. Refresh token rotation with automatic reuse detection then becomes more relevant because it offers some level of damage control for leaked tokens.

    I think there are two reasons it would be relevant to implement for httr2. One is simply that some APIs might start enabling it by default, especially applications with public clients that use something like Auth0. The other reason is that if APIs offer it as an option, it might actually make sense to enable it for R. While R can be quite confidential, I do worry about how securely users store tokens.

  4. jonthegeek commented on Apr 22, 2023

    @jonthegeek
    Contributor

    do you know of an API that does this:

    When requesting an access token, the server may also return a new refresh token ...

    Zoom does this.

  5. hadley commented on May 9, 2023

    @hadley
    Member

    @jonthegeek I don't think that applies since you'd use either the oauth_flow_auth_code() or `oauth_flow_device()

    @kasperwelbers can you give me any more details about why you're using oauth_flow_refresh() directly? That's only really needed for the special case where you just have an access token, and you're skipping the usual oauth flow mechanisms. Given that oauth_flow_refresh() expects the refresh token to be stored in an environment variable (which is presumably set in .Renviron or similar), I don't see how we could possible update it.

    Or is this just a documentation issue? I believe that all the more usual ouath flows do support refresh token rotation:

    httr2/R/oauth.R

    Lines 44 to 52 in f9e73a3

    if (token_has_expired(token)) {
    cache$clear()
    if (is.null(token$refresh_token)) {
    token <- exec(flow, !!!flow_params)
    } else {
    token <- token_refresh(flow_params$client, token$refresh_token)
    }
    }
    }

  6. jonthegeek commented on May 10, 2023

    @jonthegeek
    Contributor

    Or is this just a documentation issue? I believe that all the more usual ouath flows do support refresh token rotation:

    I think this was mostly a "getting to know httr2" issue. The flow that I wanted to use it for (a GHA to automate processing of some R4DS zoom stuff, which I would supply with a refresh token to kick off the process) won't work anyway because of the rotation, so I need to dig deeper to make it work. Which I think will put me into the more-normal flows.

    I'll work through it to see if there's a real use-case left here.

  7. kasperwelbers commented on May 12, 2023

    @kasperwelbers
    Author

    @hadley Thanks for picking this up! For us the problem would actually be solved if we could just disable the error, as also suggested in #197. I saw that you already replied to this issue that it shouldn't be necessary to use oauth_flow_refresh. But let me argue why we think this makes sense.

    Our use case is that we store the (long-lived) refresh token client side, which is a common way to persist the connection across sessions (given proper care to keep the refresh token secret). At the start of a new session, we thus need to perform the refresh token grant. I understood from the documentation this is what oauth_flow_refresh() implements.

    This function generates an access token from a refresh token, following the process described in rfc6749, Section 6. Errors if the refresh returns a new refresh token, see req_oauth_refresh() for details.

    I'm not sure why this errors on a new refresh token, but this is a valid response, as also described in section 6: "The authorization server MAY issue a new refresh token, in which case the client MUST discard the old refresh token and replace it with the new refresh token".

    I understand that httr would not want to get involved in the process of replacing the refresh token, because storing the token is the user's responsibility. But the error makes it impossible for users to replace the refresh token themselves.

    Adding a parameter to disable the error would be a simple, non-breaking change, and it would make sense if the function follows rfc6749

  8. hadley commented on May 12, 2023

    @hadley
    Member

    @kasperwelbers ok got it — if you want to manually manage refresh tokens, oauth_flow_refresh doesn't help at all. I think it's still important to know that your refresh token has change (and you need to update your stored token), so I'll make this a warning rather than a error.

  9. added a commit that references this issue on May 12, 2023
    f822dbb
  10. kasperwelbers commented on May 12, 2023

    @kasperwelbers
    Author

    @hadley great, thanks! Sorry about the confusion about managing the refresh token with oauth_flow_refresh. My real issue was being able to use it as a one-liner for doing the token refresh grant with rotation (our temp solution was to sinfully use the unexported httr2:::token_refresh).

  11. added a commit that references this issue on May 12, 2023
    c061466
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions