OAuth 2.0 Under the Hood: Authorization Flows, Tokens & Security
So before we understand OAuth and OpenID Connect, let's first look into what led to something like this and why we gradually moved away from relying solely on traditional email and password-based authentication towards OAuth and federated authentication systems.
Why did we need OAuth?
In the early era of the internet, users visited many sites, and a lot of them required an account. Users, of course, created accounts as per their needs. But with time, the internet grew larger and larger more websites, more social media platforms, more games and there came a point where users signed up for hundreds of websites and applications.
Of course, in order to remember their passwords, a lot of users ended up using the same password across multiple websites.
This led to a severe security vulnerability. If a password gets exposed in a data breach, someone could potentially gain access to all your accounts across different websites. This is also what makes attacks like credential stuffing possible.
Another issue was that people found it very hard to manage passwords for multiple websites, even if they were conscious about security. This eventually led to something known as password fatigue.
Another situation that led to the need for an OAuth-like system was the need to share resources across services.
For example, imagine your flight booking application needing access to your email account to automatically retrieve flight confirmation emails. What some developers did back then was ask users to share their passwords with third-party services, and that was disastrous, to say the least.
Sharing a password essentially meant giving full access to your account to another service. There was no proper way to scope the data that the third-party application could access.
Revoking access was also a huge pain. How are you going to revoke a third-party application's access to your email account apart from changing your password? And if you were using the same password across multiple websites, you might have to change those passwords as well.
So sharing passwords was off the table because of the above issues, but the problem still remained.
Around 2007, OAuth, short for Open Authorization, came into the picture, and at that time, it was a truly revolutionary idea.
OAuth 1.0 and the introduction of access tokens
The idea behind OAuth was to share tokens across services or websites instead of directly sharing passwords.
These tokens could grant access to specific resources owned by the user instead of granting access to their entire account.
For example, I could give you a token that only allows you to read my emails instead of giving you access to my entire Google account, as was previously the case.
There are four major components involved in the OAuth authorization model:
Resource Owner: The user who owns the data, i.e., you.
Client: The application or service requesting access to the data. For example, a flight booking application.
Resource Server: The server that hosts and provides the protected resources. In the case of emails, let's say Gmail.
Authorization Server: The server responsible for authorizing access and issuing tokens to the client.
These are the terms commonly used in OAuth 2.0, but the underlying participants also existed in OAuth 1.0.
How OAuth 1.0 works
The typical OAuth 1.0 flow between these components worked something like this:
The client requests a temporary request token from the service provider.
The client redirects the user to the authorization endpoint, where the user grants permission to access specific resources.
Once the user grants permission, they are redirected back to the client with an authorization verifier.
The client exchanges the authorized request token and verifier for an access token.
The client then uses the access token to make cryptographically signed requests to the resource server.
In our example, the flight booking application could now use its access token to retrieve emails from Gmail, provided that the user had granted the necessary permissions.
The important thing here is that at no point did the user have to share their actual Google account password with the flight booking application.
The evolution from OAuth 1.0 to OAuth 2.0
Around 2010, work on OAuth 2.0 began, and it was officially standardized in 2012.
The reason for this was that OAuth 1.0 had a few limitations. I mean, it was pretty revolutionary for its time, but one of the major issues was that it was relatively complex for developers to implement.
OAuth 1.0 required cryptographic signatures for requests, which made implementations more complicated and prone to errors.
OAuth 2.0 simplified this by introducing an authorization framework that commonly uses bearer tokens over HTTPS.
Unlike OAuth 1.0, where requests had to be cryptographically signed, a bearer token allows the client to access protected resources simply by presenting the token.
For example:
Authorization: Bearer <access_token>
This made OAuth 2.0 much simpler to implement. However, it also introduced a security concern: anyone who gets hold of a bearer token can potentially use it until it expires or is revoked.
OAuth 2.0 therefore relies heavily on HTTPS, secure token storage, limited token lifetimes, and other security mechanisms.
Another major improvement was that OAuth 2.0 allowed developers to choose different authorization flows depending on the type of application they were building.
A traditional server-side web application, a mobile application, and a backend service communicating with another backend service all have different authorization requirements.
The limitation of OAuth 2.0
OAuth 2.0 was pretty good at solving authorization, but it still had a limitation when it came to authentication.
Before we proceed, it's important to understand the difference between these two concepts:
Authentication: Verifying who the user is.
Authorization: Determining what resources or actions someone is allowed to access.
OAuth 2.0 was designed as an authorization framework, not an authentication protocol.
An OAuth 2.0 access token allows a client to access protected resources, but the protocol itself does not define a standardized way for the client to verify the identity of the user.
Developers could retrieve user information through provider-specific APIs, but there was no standardized identity layer across different OAuth providers.
To solve this limitation, something called OpenID Connect (OIDC) was introduced and officially finalized in 2014.
OpenID Connect (OIDC)
OpenID Connect was not an independent replacement for OAuth 2.0. Instead, it was built on top of OAuth 2.0 and extended its capabilities by introducing an identity layer.
One of the major additions introduced by OpenID Connect was something called an ID token, which was not part of the original OAuth 2.0 specification.
An ID token is a JSON Web Token (JWT) that contains claims about the authenticated user and the authentication event.
These claims can include information such as:
sub— A unique identifier for the user within the issuer.iss— The issuer of the token.aud— The intended audience of the token.exp— The expiration time of the token.iat— The time at which the token was issued.email— The user's email address, when requested and provided.nonce— A value used to associate the authentication request with the ID token, when applicable.
The client can validate the ID token and use the information present inside it to establish the identity of the authenticated user.
Because of OpenID Connect, you can now go to a website and sign up using something like "Sign in with Google" without having to create and manage a separate password.
However, not every social login implementation uses OpenID Connect. Some providers use OAuth 2.0 along with their own user identity APIs.
How OpenID Connect works with OAuth 2.0
So, the typical flow for OpenID Connect using the Authorization Code Flow with PKCE works something like this:
The user clicks on "Sign in with Google" on the client application.
The client generates the necessary security parameters, including a
state, a PKCE code verifier and code challenge, and optionally anonce. It then redirects the user to Google's authorization endpoint with parameters such asclient_id,redirect_uri,response_type=code, andscope=openid email profile.Google authenticates the user and asks for consent where required.
Once authentication is successful, Google redirects the user back to the client's registered callback URL with an authorization code and the original
state.The client's backend validates the
stateand sends a request to Google's token endpoint to exchange the authorization code for tokens. It includes the PKCE code verifier and client authentication where required.The authorization server validates the request and returns an access token, an ID token, and, depending on the configuration, a refresh token.
The client's backend validates the ID token by verifying its cryptographic signature, issuer, audience, expiration time, and nonce if one was included in the original request.
Once the ID token is validated, the backend uses the user's unique identity to find or create a corresponding user in its own database and establishes an application session.
The access token can then be used to access authorized resources from the resource server, while the ID token is used to establish the identity of the authenticated user.
The refresh token, if issued, can be used to obtain a new access token without requiring the user to go through the entire authorization flow again.
This is essentially how OAuth 2.0 and OpenID Connect work together to provide both delegated authorization and standardized authentication without requiring users to share their passwords with third-party applications.