Lesson 4 of 7
OAuth and OpenID Connect sign-in
Listen to an AI summary of OAuth and OpenID Connect sign-in
Signing in with another account
The sports shop has a button marked Sign in with Google. A customer who presses the button signs in at Google, and Google tells the shop who the customer is. The shop never sees the Google password.
The provider is the company that holds the account, here Google. OAuth is the set of rules for the messages between the shop and the provider. OpenID Connect adds a way for the provider to say who the person is.
The four steps
1.The shop
sends the browser to the provider
2.The provider
signs the person in
3.The provider
sends the browser back to the shop, with a code
4.The shop
swaps the code for an ID token and starts a session
A redirect is a response that sends the browser to another URL, and steps 1 and 3 are redirects. In step 1 the shop sends the provider two values, state and redirect_uri. redirect_uri is the URL at the shop that the provider sends the browser back to in step 3. The code is a value that works once. The shop sends the code to the provider, with a secret only the shop holds, and gets an ID token back. An ID token is a JSON Web Token (Signed tokens) that says who signed in.
The callback of the sports shop
The callback is the route at redirect_uri, which runs step 4. Look at lines 3 and 4.
The state value
The state value is a secure random value that the shop makes in step 1 and saves in the session. The provider sends the same value back in step 3. In step 4 the shop compares the state in the URL with the saved value. The first callback never reads state.
An attacker starts a sign-in with the attacker's own account and stops before step 4. The attacker sends Sam the link for step 4. Sam opens the link, and the shop signs Sam in to the attacker's account. A card that Sam then saves is saved in the attacker's account.
The redirect_uri
The provider sends the code only to a redirect_uri the shop registered in advance. A registered URL with a wildcard, such as https://shop.example/*, lets an attacker choose any page of the shop to receive the code. A page that passes its URL on, such as an open redirect (Open redirects), then gives the code to the attacker. Safer code writes one whole URL in the settings. A redirect_uri built from the request lets the sender choose where the code goes, where the provider does not compare the whole URL with the registered one.
The email address
Some providers let a person type any email address and never check the address. An attacker makes an account at such a provider with Sam's address. Line 4 of the first callback finds the shop account by email, so the shop signs the attacker in as Sam.
The ID token holds sub, the provider's own id for the person, and email_verified, which is true where the provider checked the address. Safer code finds the account by sub, and refuses an address that is not verified. sub is unique inside one provider only, so a shop with two providers finds the account by the provider and sub together.
The ID token
Line 3 of the first callback reads the ID token with jwt.decode, which checks nothing. The safer callback checks the signature with the key of the provider. The issuer (iss) names the provider that made the token. The audience (aud) names the website the token is for, and a token made for another website is refused. jwt.verify also refuses a token whose expiry time (exp) has passed.
app.get('/callback', async (req, res) => { const saved = req.session.oauthState delete req.session.oauthState if (!saved || req.query.state !== saved) { return res.status(400).send('Sign-in refused') } const tokens = await provider.swapCode(req.query.code) const person = jwt.verify(tokens.id_token, providerKey, { algorithms: ['RS256'], issuer: 'https://accounts.provider.example', audience: SHOP_CLIENT_ID, }) if (!person.email_verified) { return res.status(400).send('Sign-in refused') } req.session.userId = await findUserBySub(person.sub) res.redirect('/') })
Lines 2 to 6 check state. Line 3 deletes the saved value, so the value works once, and line 4 refuses a request when no value was saved. Lines 8 to 12 check the signature, the algorithm, the issuer, the audience and the expiry time of the ID token. Lines 13 to 15 refuse an address the provider did not check, and line 16 finds the account by sub. The safer callback leaves out one step that Sessions teaches, a new session id at sign-in. Two more protections are outside this lesson. PKCE ties the code to the browser that started the sign-in, and nonce ties the ID token to one sign-in.
The four checks
state- The value that comes back is the value the shop saved in the session.
redirect_uri- One whole URL, fixed in the settings and registered with the provider.
- The email address
- The account is found by
sub, and the address is verified. - The ID token
- The signature, the algorithm, the issuer, the audience and the expiry time are checked.
Examples of what to look for
- A callback route that never compares
statewith a value saved in the session, or a check that passes when no value was saved. - A
redirect_uribuilt from the request, or registered with a wildcard. - An account found by the email address from the provider, with no check of
email_verified. - An ID token read with
jwt.decode, or checked with noissuerand noaudience.
Check your understanding
State what the shop compares the
statevalue with.Show the answer
The shop compares the
statein the URL with the value the shop saved in the session at the start of the sign-in.Which line of the first callback reads the ID token, and what does the line check?
Show the answer
Line 3 reads the ID token with
jwt.decode, which checks nothing.Give one reason why the safer callback finds the account by
suband not by email.Show the answer
A provider may never have checked the email address, so an attacker can use Sam's address.
subis the provider's own id for the person.