OAuth 2.0 & OpenID Connect, including Google, Apple and social linking
OAuth vs OIDC, the authorization-code flow with PKCE, ID tokens vs access tokens, verifying Google and Apple ID tokens, account linking, and storing third-party tokens safely.
OAuth 2.0 vs OIDC
- OAuth 2.0 is delegated authorization: “let app X post to my YouTube on my behalf”. It produces an access token for an API.
- OpenID Connect (OIDC) is an identity layer on top of OAuth: “who is this user?”. It adds an ID token, a JWT signed by the provider containing
sub,email,email_verified,audandnonce.
“Sign in with Google” is OIDC. “Connect your TikTok account so we can read view counts” is OAuth.
Authorization code flow with PKCE
1. Your app -> browser redirect:
https://accounts.google.com/o/oauth2/v2/auth?client_id=...&redirect_uri=.../callback
&response_type=code&scope=openid email profile
&state=RANDOM (CSRF protection) &nonce=RANDOM (replay protection)
&code_challenge=SHA256(verifier)&code_challenge_method=S256 <- PKCE
2. User logs in at Google and consents
3. Google -> redirect_uri?code=AUTH_CODE&state=RANDOM (check that state matches!)
4. Your backend -> POST token endpoint: code + code_verifier (+ client_secret)
5. Google -> { access_token, id_token, refresh_token? }
6. Verify id_token: signature (Google JWKS), iss, aud == your client_id, exp, nonce
7. Find or create the local user by (provider, sub), then issue YOUR OWN session/JWT
PKCE (Proof Key for Code Exchange): even if an attacker intercepts the code, they can’t exchange it without the code_verifier that only your client holds. It is mandatory for mobile and SPA clients, and recommended for everyone in OAuth 2.1.
Mobile and SPA shortcut: ID-token sign-in
The native SDK (Google Sign-In, Sign in with Apple) gives the app an ID token directly. The app sends it to your backend, which verifies it and then issues your own tokens:
// POST /api/v1/auth/oauth/google { "idToken": "eyJ..." }
GoogleIdToken token = verifier.verify(idToken); // signature + aud + exp + iss
if (token == null || !token.getPayload().getEmailVerified()) throw unauthorized();
User user = users.findOrCreateByGoogleSub(token.getPayload().getSubject(), email);
return issueSession(user);
In your projects
HttpCookieOAuth2AuthorizationRequestRepository · OAuth2 success/failure handlersServer-side Spring Security OAuth2 login with Google. Because the app is stateless, the OAuth state and authorization request are stored in a cookie instead of the HTTP session. On success, the handler issues RM’s own JWT cookies.
GoogleIdTokenVerifier · GoogleLoginService · POST /api/v1/auth/oauth/googleID-token sign-in for the SPA. The backend verifies Google’s token and starts a SperoTek session with its own MFA and policy rules.
OidcLoginDecoderConfig · AppleIdentityTokenVerifier · AppleSignInService · AppleTokenClient · socialaccount/infrastructure/oauth/* · PkceSupport · OAuthTokenCipherThe most complete example:
- Google OIDC, with a per-issuer
JwtDecodercache. - Sign in with Apple: verify Apple’s identity token and revoke Apple tokens when the account is deleted, which App Store rules require.
- Social account linking for TikTok, Facebook, Instagram, YouTube, X, Discord, Telegram and Snapchat through
*OAuthLinkProviders with PKCE. - Third-party access and refresh tokens are encrypted at rest with
OAuthTokenCipher(an AES-GCM key ring).
Alternatives
| Option | Notes |
|---|---|
| Build it with Spring Security / provider SDKs (yours) | Full control; you own the security |
| Auth0, Clerk, Cognito, Firebase Auth, Supabase Auth | Hosted identity: faster, costs money, vendor lock-in |
| Keycloak / Authentik / Spring Authorization Server | Self-hosted identity provider for many apps |
| Authsignal (in IR3D’s pom) | Adds risk-based MFA on top of your own auth |
Related concepts you must know
- Scopes. Request the minimum (
openid email). statevsnonce.stateprotects the redirect from CSRF;noncebinds the ID token to this login.- Redirect URI validation. Use an exact match. Open redirects leak codes.
- Client credentials flow. Machine-to-machine OAuth, the standard alternative to SperoTek’s company API tokens.
- Implicit flow is deprecated. Use code + PKCE.