# OAuth 2.0 and OpenID Connect

> Delegate access and add 'Sign in with Google/GitHub' correctly using standard flows. Free notes, key concepts, resources and a mini project.

Canonical: https://www.internhack.in/roadmaps/tracks/backend/oauth2-oidc
Last updated: 2026-11-28

Delegate access and add 'Sign in with Google/GitHub' correctly using standard flows.

Intermediate · about 7 hours · difficulty 4/5

Learn first: [Token-Based Auth and JWT](https://www.internhack.in/roadmaps/tracks/backend/token-auth-jwt)

**OAuth 2.0** is a framework for **delegated authorization**: letting an app access a user's resources on another service (read their GitHub repos, their Google Calendar) without ever seeing their password. **OpenID Connect (OIDC)** is a thin identity layer on top of OAuth 2.0 that adds an **ID token** (a JWT describing who the user is), which is what powers "Sign in with Google".

### Roles

The **resource owner** (the user), the **client** (your app), the **authorization server** (Google, Auth0, Keycloak) and the **resource server** (the API holding the data).

### The flow to know: Authorization Code with PKCE

1. Your app redirects the user to the authorization server with `client_id`, requested `scope`, `redirect_uri`, a random `state` and a PKCE code challenge.
2. The user logs in and consents.
3. The server redirects back with a short-lived **code**.
4. Your backend exchanges the code (plus the PKCE verifier) for tokens.

Other grants: **client credentials** for machine-to-machine calls. The implicit and password grants are deprecated.

### Build or buy

Identity is easy to get subtly wrong. Many teams use a provider (Auth0, Clerk, Cognito, Firebase Auth) or self-hosted **Keycloak** instead of writing their own authorization server. You should still understand the flow to integrate and debug it.

### Pitfalls

- Skipping `state` validation, enabling CSRF on the login flow.
- Accepting ID tokens without checking `iss`, `aud` and signature.

You are ready to move on when you have implemented "Sign in with GitHub or Google" end to end and can explain each redirect and parameter.

## Key concepts

- **OAuth roles and scopes**
- **Authorization code flow with PKCE**
- **Client credentials grant**
- **OpenID Connect and ID tokens**
- **Social login providers**
- **Keycloak, Auth0, Cognito**
- **Token introspection**

## Mini project

Add Sign in with GitHub to your app using the authorization code flow with PKCE, linking the external identity to a local user record.

## Check yourself

What is the difference between an OAuth access token and an OIDC ID token, and which one should your API accept?

## Free resources

- [OAuth 2.0](https://oauth.net/2/) (oauth.net)
- [OpenID Connect Core specification](https://openid.net/specs/openid-connect-core-1_0.html) (OpenID Foundation)
- [OAuth 2.0 Simplified](https://www.oauth.com/) (Aaron Parecki)

Previous: [Token-Based Auth and JWT](https://www.internhack.in/roadmaps/tracks/backend/token-auth-jwt) · Next: [SAML and Enterprise Single Sign-On](https://www.internhack.in/roadmaps/tracks/backend/saml-enterprise-sso)

## More in Who Is Calling and What Can They Do

- [Sessions and Cookies](https://www.internhack.in/roadmaps/tracks/backend/sessions-cookies)
- [Token-Based Auth and JWT](https://www.internhack.in/roadmaps/tracks/backend/token-auth-jwt)
- [OAuth 2.0 and OpenID Connect](https://www.internhack.in/roadmaps/tracks/backend/oauth2-oidc)
- [SAML and Enterprise Single Sign-On](https://www.internhack.in/roadmaps/tracks/backend/saml-enterprise-sso)
- [Authorization: Roles and Permissions](https://www.internhack.in/roadmaps/tracks/backend/authorization-models)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Backend Developer Roadmap](https://www.internhack.in/roadmaps/tracks/backend)
