OAuth is how one app gets limited access to your account on another service without ever seeing your password. The 'Connect with Google' and 'Sign in with GitHub' buttons run on it.
The problem it solves
Say a photo printing site wants your holiday pictures from Google Photos. The old way was to ask for your Google password. That gave the site everything, down to the power to change the password, and the only way to take access back was to change it yourself.
OAuth replaces the password with an access token, a string limited to what you agreed to. It stops working when it expires or when you revoke it.
The four parties
- Resource owner: you, the person whose data it is.
- Client: the app asking for access, here the printing site.
- Authorisation server: the service that logs you in and hands out tokens, here Google.
- Resource server: the API that holds your data and accepts tokens.
How the authorisation code flow works
Most web apps use this flow. Here it is for the printing site:
The OAuth authorisation code flow
Step 1 of 10: You click Connect Google Photos on the printing site.
- You click 'Connect Google Photos'. The site redirects your browser to Google with its client ID, the scope it wants (read-only access to photos) and the address to send you back to.
- You log in on Google's own page and approve the request. The printing site never sees your password.
- Google redirects you back to the site with a short-lived authorisation code.
- The site's server swaps that code for an access token, in a direct call to Google that proves which app is asking.
- The site calls the Photos API with the token and gets only what the scope allows. For existing Google Photos pictures, the user must also select them through the Picker API; OAuth consent alone no longer grants access to the whole library.
The code passes through your browser and is short-lived. A confidential client authenticates when redeeming it; PKCE adds a verifier so an intercepted code cannot be redeemed without that verifier. The token itself goes straight from Google to the site's server.
Scopes, expiry and revoking
A scope names one permission, such as reading photos but not deleting them. Access tokens are short-lived, and many providers also issue a refresh token so the app can get a new one without asking you again. You can revoke an app's access from your account settings without touching your password.
OAuth and signing in
OAuth answers 'what may this app do?', which is authorisation. It doesn't tell the app who you are. 'Sign in with Google' adds OpenID Connect, a layer on top of OAuth 2.0 that issues an ID token describing the user. If the difference is fuzzy, authentication vs authorisation covers it.
Common mistakes
- Asking for more scopes than the app needs. A leaked token can then do more damage.
- Shipping a client secret inside a mobile or single-page app, where anyone can read it. Those apps must use the authorisation code flow with PKCE instead. PKCE is also recommended for confidential web clients.
- Treating an access token as proof of who the user is. That is the ID token's job.
Key takeaways
- OAuth gives an app limited access to your account without your password.
- You log in on the provider's page; the app only ever gets a token.
- Scopes limit what a token can do, and you can revoke it at any time.
- OAuth handles authorisation; OpenID Connect adds sign-in on top.
Wondering how this was made? How we make these