How does OAuth 2.0 work?

In short
OAuth 2.0 is an authorization framework that lets a third-party app get limited access to a user's data on an HTTP service without learning the user's password. The user approves the app, and the app receives an access token it can present to the service instead.
The problem OAuth 2.0 solves
Imagine an app that wants to read your photos stored on another website. The old approach was to ask you for your username and password on that website and let the app log in as you. That is risky. The app keeps your password, and it gets full access to everything in your account.
OAuth 2.0 is an authorization framework defined in RFC 6749, published by the IETF in 2012. It gives a third-party application limited access to an HTTP service. The application never needs your password. Instead, you approve a specific request, and the service hands the app a token that works only within the limits you agreed to.
The main roles
OAuth names four roles. Knowing them makes the rest of the flow easier to follow.
- Resource owner: the person (or other entity) who owns the data, such as you.
- Client: the third-party application that wants access.
- Authorization server: the server that checks your identity and issues tokens.
- Resource server: the server that hosts the protected data and accepts access tokens.
In many real products the authorization server and the resource server belong to the same company, but the specification keeps them as separate roles.
How it works
The basic flow runs in a few steps. The client first sends the user to the authorization server with a request that names what access it needs. The user logs in there and approves or denies the request. If the user approves, the client receives a grant, which it exchanges for an access token. The client then calls the resource server and includes the access token in its request.
The access token is a string that represents the approval. It usually expires after a set time. When it does, the client can use a refresh token, if one was issued, to get a new access token without asking the user to log in again.
The specification describes several grant types, which are the ways a client can obtain authorization. The most common are the authorization code grant, which is used by web server applications and, with extra protection, by mobile and single-page apps; the client credentials grant, used when an app acts on its own behalf and no user is involved; and the resource owner password credentials grant, which the specification treats as a last resort because the client handles the user's password.
A simple example
Here is a simplified view of an authorization code request. The client sends the user's browser to the authorization endpoint with parameters like these:
GET /authorize?response_type=code
&client_id=s6BhdRkqt3
&redirect_uri=https%3A%2F%2Fclient.example.com%2Fcallback
&scope=photos.read
&state=xyz123
The server shows the user a consent screen. If the user agrees, it redirects the browser back to the redirect URI with a short-lived code and the same state value. The client then sends that code, along with its own credentials, directly to the token endpoint over a secure connection. The token endpoint returns the access token. The state value lets the client check that the response belongs to the request it started, which helps against cross-site request forgery.
The exact parameter names and rules are in the specification, so check section 4.1 when you implement this.
Common mistakes
Developers new to OAuth often make a few predictable errors. The first is skipping the state parameter. Without it, an attacker can trick a user's browser into completing a flow the user did not start. The second is sending tokens over plain HTTP. The specification requires Transport Layer Security (TLS) so that tokens cannot be read on the network.
The third mistake is confusing authentication with authorization. OAuth 2.0 is about granting access to resources. It does not, by itself, tell the client who the user is. For login, many teams add OpenID Connect on top of OAuth 2.0. The fourth mistake is storing tokens carelessly, or requesting far more scope than the feature needs. Ask for the smallest set of permissions that works.
Alternatives and related ideas
OAuth 1.0 is the older protocol. RFC 6749 replaces it, so new projects should use OAuth 2.0. For identity and login, OpenID Connect builds on OAuth 2.0 and adds a standard way to receive user information in an ID token. For server-to-server calls with no user involved, the client credentials grant is usually simpler than building a full user flow.
When you are ready to read the specification itself, start with the protocol flow in section 1.2 and the authorization code grant in section 4.1. The security considerations in section 10 are worth reading before you ship anything.
FAQ
Is OAuth 2.0 the same as logging in?
Not exactly. OAuth 2.0 is about granting an app limited access to a resource. It does not define how to identify the user. Login is usually added with OpenID Connect, which is built on top of OAuth 2.0.
Why does the app not get my password?
The design keeps your password with the authorization server. The app only receives a token that represents the permissions you approved, so it never has to store or handle your credentials.
What is the difference between an access token and a refresh token?
An access token is used to call the resource server and usually expires after a set time. A refresh token, when issued, lets the client request a new access token without making the user log in again.
Which OAuth 2.0 grant type should I use?
For a web application acting for a user, the authorization code grant is the usual choice. For machine-to-machine calls where no user is involved, the client credentials grant fits better.
Sources
This page is based on the official documentation below. Read it for full detail.
OAuth 2.0 — official documentation ↗