r/redditdev • u/securimancer • Jan 12 '21
Reddit API OAuth2 API Changes Upcoming
As part of modernizing our OAuth2 infrastructure, we’re implementing some potentially breaking changes to our OAuth2 flow as outlined below on February 15, 2021.
Refresh Token Changes
When executing our refresh token flow, we currently only send back an access token in the response. Responses to /api/v1/access_token with grant_type=refresh_token looked like:
{
"access_token": "your access token",
"token_type": "bearer",
"expires_in": 3600,
"scope": "your scopes"
}
This meant that the refresh token you get during the authorization code flow can be reused indefinitely. Going forward, our response will also include a brand new refresh token (as allowed by the RFC spec).
{
"access_token": "your access token",
"token_type": "bearer",
"expires_in": 3600,
"refresh_token": "your new refresh token",
"scope": "your scopes"
}
Since some OAuth2 client implementations might not handle this scenario (whereas PRAW does, for example), we’re not immediately enforcing revocation of the consumed refresh token. We’re looking to enforce this starting in Q2 2021, given there aren't significant numbers of OAuth2 clients misbehaving after the change.
Also note that all refresh tokens previously had no expiration. We're going to start enforcing a 1 year expiration on refresh tokens to help curb Reddit's storage for refresh tokens (we've got a lot of them).
Authorization Code Reuse
When executing our authorization code flow, we consume the auth code in exchange for an access token. If, within an auth code's 10 minute TTL, that same auth code is attempted to be used again, we will revoke any tokens issued with said auth code, per RFC spec . This should be unnoticeable to well-behaved clients; however, instead of harmlessly failing, we will now be revoking any access or refresh tokens issued with that auth code.
Redirect URI Fix Fragments
The last, but likely least impactful, change we're implementing is adding a "fix fragment" #_ to the end of the redirect URI in the Location header in response to a POST request to /api/v1/authorize. This should be transparent as browsers and url parsers should drop the fragment when redirecting.
Edit 1: clarified Reddit's storage of refresh tokens.
Edit 2: Adding a note about potential network connectivity / cosmic rays breaking the refresh token flow. As it stands now, we're including a 2 retries leeway to account for any miscommunication in this process starting Q2 2021. E.g.,. you can send the same refresh token 3 times before it is irrevocably revoked.
Edit 2021-02-18: This hasn't been deployed yet, but goal is today / next week. Appreciate the patience as there's a lot going on in the world currently. The enforcement of refresh tokens is also still under discussion, might be Q2 or Q3 even. Also trying to get an Github-y API key flavor of long-lived access token in the mix too to address the concerns about longevity of OAuth2 tokens and how crappy the password grant is.
5
u/SirensToGo Jan 13 '21
…are we not supposed to store refresh tokens?
I have an installed application and I store both the access token and the refresh token in the device’s keystore. While I probably won’t run into the 1 year issue since it’d mean a user would need to not launch the app for an entire year (assuming I rotate refresh tokens correctly), but I don’t entirely understand the point of it given that the authorization was specifically indefinite.
5
u/Watchful1 RemindMeBot & UpdateMeBot Jan 13 '21
u/securimancer has reddit considered changing the "password" auth flow for script type apps? Other popular API's with this type of application, like discord or twitch bots, can use only the app id and app secret, both available when you create an app in the UI, to login. But reddit requires both of those, as well as your password for this flow.
Previously you could manually retrieve a refresh token and use that in place of the password, but it was still three pieces of information. Now you'll have to either keep a constantly changing secret somewhere or store your password, which is considerably more sensitive.
16
u/not_an_aardvark snoowrap author Jan 13 '21 edited Jan 13 '21
If I'm understanding correctly, does this mean that every refresh token would be effectively revoked and replaced as soon as it's used to generate an access token?
If so, this would break almost every bot and integration using OAuth2 (including PRAW-based, snoowrap-based, and otherwise). Although PRAW updates the refresh token that it uses at runtime (as shown in the linked code snippet), it doesn't update a refresh token in persistent storage, as discussed in the other comment thread. The result is that a bot would break as soon as it was rebooted, due to using a stale refresh token.
It's not really realistic for API wrappers to be updated to automatically write refresh tokens in storage, either. There are a large number of ways in which tokens can be stored (in a config file, in a database with a different token for each user, etc). Effectively, it seems like this requires bots to store their credentials in an online config that gets repeatedly updated at runtime. This is pretty different from how long-term credential storage usually works.
Requiring users to update their stored refresh tokens at runtime would also create some major sychronization issues. For example, if a bot sends a request with a refresh token to get an access token, but then loses network connection before receiving reddit's response, the bot would effectively be locked out because the old refresh token would be revoked and the bot wouldn't have received the new refresh token. As a result, the app owner would need to make the end user go through the OAuth authentication flow again (or for personal scripts, the app owner would need to manually fix their bot). It's not clear how one would avoid this error, and having a design that can randomly break itself and require manual intervention due to network errors doesn't seem like a good architecture to push on app/bot developers.
If this is implemented, I would likely start recommending that people use the
passwordgrant type for personal use scripts rather thanrefresh_token, since it would allow for more robust long-term storage of credentials despite the potential issues with storing passwords. The inevitable synchronization lockouts and credential management complexity would make it difficult to recommend "installed" and "web"-type apps at all.It's not clear what the benefit of this behavior is to justify making it impossible to do reliable credential management. Is there any chance you could reconsider it?
Have you considered enforcing the expiration at 1 year after last use, rather than at one year after being issued? This seems like it would help solve the storage issue without requiring yearly manual credential-cycling. (This is only relevant if you decide not to do the revocation strategy described above.)
edit: Clarified why using
passwordgrants instead doesn't solve the problem4
u/Pyprohly RedditWarp Author Jan 18 '21
This change nerfs the refresh token grant and I don’t like it. The IETF are trying to get rid of the password flow but the new change gives the refresh token grant an unfortunate caveat compared to the password grant. Could we at least please get the “expiration at 1 year after last use” idea /u/not_an_aardvark has suggested? Why is it so difficult to store refresh tokens anyway?
Also, on a side note, while changes are being made to the refresh token flow, could scope limiting be implemented on the refresh token grant :P I.e., add support for that optional
scopeparameter mentioned in section 6 of RFC 6749.4
u/KrisCraig Reddit.NET Author Feb 11 '21
Regarding refresh token expiration: Is it 1 year from the date that the refresh token was created or from the date that the refresh token was last used? Please tell me it's the latter.
1
u/bboe PRAW Author Feb 04 '21
Hi u/securimancer,
I'm working on implementing support for changing
refresh_tokens, but I've noticed that the responses fromhttps://www.reddit.com/api/v1/access_tokenforgrant_type=refresh_tokenaren't actually returning a new refresh token as described. Was this initial rollout put on hold?Thanks!
3
u/bboe PRAW Author Feb 23 '21 edited Feb 24 '21
For those following along, here is how PRAW is likely to handle it: https://github.com/praw-dev/praw/pull/1687
The following temporary documentation shows how to leverage these changes: https://praw.readthedocs.io/en/callbacks/tutorials/refresh_token.html
We are likely to release PRAW 7.2 within the next day containing these changes.
Edit: PRAW 7.2 has been released.
3
u/SpyTec13 Snoowrap Developer Jan 12 '21
Good change! Though it gives me more work, bad! 👀
2
u/Watchful1 RemindMeBot & UpdateMeBot Jan 12 '21
This looks like a good change. I've always wondered why refresh tokens never expired.
u/bboe, do you know if that code line securimancer linked to results in updating the praw.ini file with the new refresh token? I guess this will be a major incentive to using praw.ini files instead of running your own configs.
1
u/Investigator06 Apr 12 '21
I could not understand reason I received this as some of the postings were unusual in addition I received from Reddit when I opened it it sent to Twitter with all these crazy writing and pictures. I never go to Twitter. I do not know if there was a problem today as after I lost my original account and opened my new account it is like a fake account was created with a history of people and sites. It was weird. I am looking at wall street bets and what or who I saw on Twitter was roaring kitty.
1
u/[deleted] Jan 23 '21
[removed] — view removed comment