OAuth 2.0 is a standard that lets one application access your data in another service without ever seeing your password. You approve a limited set of permissions on the service's own login page, and the app receives an access token that works only for those permissions and only until it expires or you revoke it.
RFC 6749 defines it in one sentence: OAuth 2.0 "enables a third-party application to obtain limited access to an HTTP service". A typical example: a printing service wants the photos in your cloud storage. Without OAuth, it would ask for your cloud password. With OAuth, it gets a token that can read photos and nothing else.
Why OAuth exists
The specification's introduction describes the old approach: third-party apps had to "store the resource owner's credentials for future use", usually your password, often in clear text. That has three problems. The app gets full access to everything, not just photos. You can't revoke one app without changing your password for all of them. And every app that stores your password is another place it can leak from. OAuth replaces the shared password with a separate credential per app, the access token.
The four roles
| Role (RFC 6749 term) | In the photo example | What it does |
|---|---|---|
| Resource owner | You | Can grant access to the protected data. |
| Client | The printing service | The app that wants to make requests on your behalf. |
| Authorization server | The cloud provider's login and consent service | Authenticates you and issues access tokens to the client. |
| Resource server | The cloud provider's photo API | Hosts the data and accepts requests that carry a valid access token. |
The authorization server and resource server are often run by the same company, but they are separate jobs. The important part is that you type your password only into the authorization server, never into the client.
The authorization code flow, step by step
This is the flow behind almost every "Connect your account" button. Simplified from RFC 6749 section 4.1, with PKCE added as current practice requires:
- The client sends your browser to the authorization server. The URL carries the client's ID, the permissions it wants (
scope), where to send you back (redirect_uri), astatevalue and a PKCEcode_challenge. The RFC's example request:GET /authorize?response_type=code&client_id=s6BhdRkqt3&state=xyz &redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb - You log in and approve. The consent screen lists the requested scopes. This happens on the provider's domain, so check the address bar.
- The authorization server redirects back with a code. Your browser lands on the
redirect_uriwith?code=...&state=xyz. The code is not a token yet. RFC 6749 recommends a maximum lifetime of 10 minutes, and it can be used only once. - The client exchanges the code for tokens. Its server sends the code, its own credentials if it has them, and the PKCE
code_verifierstraight to the token endpoint, not through your browser. - The authorization server returns tokens. The RFC's example response:
{ "access_token": "2YotnFZFEjr1zCsicMWpAA", "token_type": "example", "expires_in": 3600, "refresh_token": "tGzv3JOkF0XG5Qx2TlKWIA" }expires_inis in seconds, so this token lasts one hour. - The client calls the API with the access token. Most APIs use bearer tokens, sent in a header as shown in RFC 6750:
Authorization: Bearer mF_9.B5f-4.1JqM
Why the detour through a code? The redirect in step 3 passes through your browser, its history and possibly logs. Only the short-lived, single-use code travels that way; the actual tokens go directly between the client's server and the authorization server.
Scopes: what the app is allowed to do
RFC 6749 defines scope as "a list of space-delimited, case-sensitive strings", with meanings set by each provider. Examples look like photos.read or repo. The authorization server can grant fewer scopes than the client asked for, and must then say which it actually issued. When an app asks for far more than its job needs, such as a printing service asking to delete files, decline. Requesting the minimum is the principle of least privilege applied to tokens.
Access tokens and refresh tokens
An access token is the credential the API checks. A refresh token lets the client get a new access token when the old one expires, without sending you through the consent screen again. RFC 6749 says refresh tokens go only to the authorization server, never to resource servers.
The weak point is the bearer token itself. RFC 6750 defines a bearer token as one where "any party in possession of the token" can use it, with no proof of a cryptographic key. A leaked token works for whoever holds it until it expires. That is why tokens are short-lived, why they must only travel over HTTPS, and why a leaked one should be revoked straight away (see what to do when an API key leaks; the same steps apply to tokens). Stealing a live token or session is the subject of session hijacking.
PKCE: protecting the code
RFC 7636 (September 2015) added Proof Key for Code Exchange, pronounced "pixy", because apps that can't keep a secret, such as mobile and single-page apps, were "susceptible to the authorization code interception attack". A malicious app that grabbed the code from the redirect could exchange it for tokens.
PKCE fixes this with a one-time secret. Before step 1, the client makes a random code_verifier (43 to 128 characters) and sends only its hash as the code_challenge:
code_challenge = BASE64URL-ENCODE(SHA256(ASCII(code_verifier)))
The RFC's test vector, which you can reproduce in Python:
python -c "import hashlib,base64;v=b'dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk';print(base64.urlsafe_b64encode(hashlib.sha256(v).digest()).rstrip(b'=').decode())"
E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM
In step 4 the client sends the original verifier. The authorization server hashes it and compares it with the challenge from step 1. An attacker who intercepted only the code doesn't have the verifier, so the exchange fails.
The four grant types, and which ones to use now
RFC 6749 (2012) defined four ways to get a token. The OAuth 2.0 Security Best Current Practice, RFC 9700, published in January 2025, narrowed that down:
| Grant type | Used for | Status in RFC 9700 |
|---|---|---|
| Authorization code | Apps acting for a user | The recommended flow. PKCE required for public clients and recommended for confidential ones. |
| Implicit | Old browser apps; token returned directly in the redirect | Clients should not use it unless leakage and injection are mitigated. |
| Resource owner password credentials | The app collects your password itself | Must not be used. |
| Client credentials | Server-to-server, no user involved | Still appropriate; the client authenticates as itself. |
RFC 9700 adds more rules for implementers: authorization servers must compare redirect URIs by exact string match (with an exception for port numbers on localhost for native apps), refresh tokens for public clients must be sender-constrained or rotated, and access tokens should be restricted to the resource server they are meant for. Clients must prevent cross-site request forgery on the redirect, either through PKCE or a one-time state value tied to the browser. CSRF explained covers why that matters.
OAuth is not a login protocol
An access token says "this app may read photos". It does not reliably tell the app who you are. Sign-in buttons use OpenID Connect, which its specification calls "a simple identity layer on top of the OAuth 2.0 protocol". A request becomes OpenID Connect by including the openid scope, and the response adds an ID token, a JSON Web Token carrying claims about the authentication event. If you are building login, use an OpenID Connect library rather than treating any valid access token as proof of identity.
Checking an OAuth consent screen
- Confirm the address bar shows the real provider's domain before entering your password.
- Read the scopes. "Read your email" and "Send email as you" are very different grants.
- Review connected apps in your account settings now and then, and remove ones you no longer use. Revoking there withdraws the app's access without touching your password.
Frequently asked questions
- Is OAuth used for login?
- OAuth 2.0 itself is an authorization framework: it issues access tokens that let an app call an API. 'Sign in with...' buttons normally use OpenID Connect, which the specification describes as a simple identity layer on top of OAuth 2.0. It adds an ID token, a JWT carrying claims about how and when the user authenticated.
- What is the difference between an access token and a refresh token?
- An access token is sent to the API with each request and usually expires quickly. A refresh token is sent only to the authorization server, to get a new access token without asking the user again. RFC 6749 says refresh tokens are never sent to resource servers.
- Is the implicit flow still allowed?
- The OAuth 2.0 Security Best Current Practice (RFC 9700) says clients should not use the implicit grant unless token leakage and injection are mitigated, and recommends the authorization code flow instead. The resource owner password credentials grant must not be used at all.
- How do I revoke an app's OAuth access?
- Open the security or connected-apps page of the account that granted access (for example your Google, Microsoft or GitHub account settings) and remove the app. That withdraws the access you granted at the authorization server, without changing your password.