Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is OAuth Thread Safe?

OAuth's access token/refresh token flow seems wildly UN-thread-safe to me. Help me understand it better.

Let's say I'm integrating with an API that leverages OAuth (like this one). I have my access token and I'm making API calls -- all is well in the world. But then my access token expires, and I need a new one. No problem, I use the refresh token that I was issued, and I get a new one.

Everything above sounds fine and dandy... But not in a multi-threaded world. Meaning, if the above actions all occur twice at the exact same instance on separate threads (e.g., two users request an API call simultaneously against the same object), and there can only ever be ONE access token alive at any given time, then won't one cancel out the other? And in a highly-transactional app wouldn't this happen a lot.

I have a strong feeling that this is a dumb question, but I can't wrap my brain around how this can be thread-safe.

like image 305
filmnut Avatar asked May 07 '16 05:05

filmnut


People also ask

Can OAuth be hacked?

Perhaps the most infamous OAuth-based vulnerability is when the configuration of the OAuth service itself enables attackers to steal authorization codes or access tokens associated with other users' accounts. By stealing a valid code or token, the attacker may be able to access the victim's data.

Is OAuth authentication secure?

OAuth represents an advanced step in the use of credentials for authentication of API service users. In fact, studies reveal that it is the only security method with close to 100% dependability. Its unmatched reliability is based on its ability to create unique authentication tokens for every user.

Is OAuth better than JWT?

OAuth2 is very flexible. JWT implementation is very easy and does not take long to implement. If your application needs this sort of flexibility, you should go with OAuth2. But if you don't need this use-case scenario, implementing OAuth2 is a waste of time.

Can OAuth tokens be stolen?

Incidents of stolen or found OAuth tokens commandeered by adversaries are not uncommon. Microsoft suffered an OAuth flaw in December 2021, where applications (Portfolios, O365 Secure Score, and Microsoft Trust Service) were vulnerable to authentication issues that enables attackers to takeover Azure accounts.


2 Answers

Oauth is a protocol. It depends on a particular implementation whether or not that implementation is "thread safe".

Oauth2 != Oauth: How is OAuth 2 different from OAuth 1?

And REST APIs (like the one you cited) are inherently stateless, so there's really no question of "thread safety".

Finally, here's a good discussion on how to share an OAuth2 credential (that is, once you've established the credential) between multithreaded applications:

Optimizing OAuth 2.0 Requests

In multithreaded applications, the credential should be shared between threads. Refreshing of the credential should be performed synchronously to avoid a race condition.

The client libraries make sharing a credential across threads straightforward. Each client library has a session (or user) object which is constructed with a credential that it reuses throughout its lifetime. To share the credential across threads, simply construct each session using the same credential. In all client libraries, the credential is a thread-safe object and refreshes itself synchronously when its access token expires.

For example, in the Java client library, you would create a Credential as a singleton and share it across all sessions.

like image 179
paulsm4 Avatar answered Jan 03 '23 23:01

paulsm4


I have some issues with oauth grant_type password flow.

When my app make a request to a protected resource it, using a ExchangeFilterFunction in a spring WebClient, make a request to obtain a access_token. If access_token is expired the app make a new request.

The problem is: in my implementation, if a thread detect a expired access_token it make a request to obtain a new token, in the meantime, other threads will do the same and N threads may at the same be trying to get a new access_token.

The fastest and most primitive way to solve this problem is blocking (e.g. syncrhonized keyword in java) the code snippet that gets a new token from the other threads, in this way only one request is made, but this will block all threads. When the first thread receives the new token, the other threads will be released, but now they will no longer need to make the request because they will detect a valid token.

As stated earlier, this is an implementation-specific tweak. I don't know if spring-security takes that care, but as far as I know, there's nothing in the oauth protocol specifying how to handle this.

like image 26
Willams S. de Sousa Avatar answered Jan 03 '23 22:01

Willams S. de Sousa