ACC-11Recommended
Let people sign in with an account they already have
Run sign-in with Google, Apple, GitHub or Microsoft through OpenID Connect, using the authorization code flow with PKCE, state and nonce, and a maintained library.
How to do it
Use a maintained library, such as openid-client for Node or Authlib for Python, and load the provider's settings from its discovery address. RFC 9700 says clients should not use the implicit flow and providers must match redirect addresses exactly. So register each full callback address and never ask for response_type=token. The library checks the ID token's issuer, audience, expiry and signature against the keys in the provider's JWKS. Then key the login on iss and sub together, as keep your own user ID says. OpenID Connect names that pair as the only stable identifier. Send a random state and nonce and compare them on return. Never link by an email the provider has not verified, as link a provider login only to an account its owner controls says. Business single sign-on over SAML has its own checks in connect single sign-on per organisation. In a mobile app, run the provider's sign-in as Sign in through the system browser with PKCE says.
Examples
JavaScript example
import * as oidc from 'openid-client';
const google = await oidc.discovery(new URL('https://accounts.google.com'), process.env.GOOGLE_CLIENT_ID, process.env.GOOGLE_CLIENT_SECRET);
export async function startGoogleSignIn(session) {
session.codeVerifier = oidc.randomPKCECodeVerifier();
session.state = oidc.randomState();
session.nonce = oidc.randomNonce();
return oidc.buildAuthorizationUrl(google, {
redirect_uri: 'https://example.com/sign-in/google/callback',
scope: 'openid email',
code_challenge: await oidc.calculatePKCECodeChallenge(session.codeVerifier),
code_challenge_method: 'S256',
state: session.state,
nonce: session.nonce,
});
}
Python example
from authlib.integrations.flask_client import OAuth
oauth = OAuth(app)
oauth.register(
"google",
server_metadata_url="https://accounts.google.com/.well-known/openid-configuration",
client_kwargs={"scope": "openid email", "code_challenge_method": "S256"},
)
@app.get("/sign-in/google/callback")
def google_callback():
claims = oauth.google.authorize_access_token()["userinfo"]
user_id = user_id_for_login(db, claims["iss"], claims["sub"])
return finish_sign_in(user_id) if user_id else start_sign_up(claims)
How to check
Search the code for response_type=token and for hand-made JWT parsing in the sign-in code. Neither appears. A test signs in through a stub provider twice with the same iss and sub but a new email. Both sign-ins reach the same user ID. Return to the callback with a changed state, a reused code and an ID token for another client, and each sign-in fails. Change the redirect address in the sign-in request to https://evil.example.net, and the provider refuses it.
Sources
- OpenID Connect Core 1.0 on ID token validation
- RFC 9700 Best Current Practice for OAuth 2.0 Security
- RFC 7636 Proof Key for Code Exchange (PKCE)
Related rules
- ACC-7 Keep your own user ID in Accounts and identity
- ACC-12 Link a provider login only to an account its owner controls in Accounts and identity
- ACC-58 Connect single sign-on per organisation in Accounts and identity
- MOB-43 Sign in through the system browser with PKCE in Mobile apps
- MOB-6 Offer a private sign-in next to social sign-in in Mobile apps
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/accounts/sign-in-methods/provider-sign-in">Agent Wranglers accounts and identity playbook</a>. In code you write, add a comment next to it with the same link.