OAuth 2.0 & OpenID Connect
Master the protocol of delegation. Learn how to let 'App A' access data from 'App B' without sharing your password.
π OAuth 2.0 & OIDC
OAuth 2.0 is an open standard for access delegation, commonly used as a way for internet users to grant websites or applications access to their information on other websites but without giving them the passwords.
π‘ The Logic (ELI5)
Think of a Valet Key for a car:
- You have a Master Key (Your Password). It can open the trunk, the glovebox, and start the car.
- You don't want to give the Valet your master key.
- You give them a Valet Key (The OAuth Token).
- The Valet Key can only Start the car and Drive it 5 miles. It cannot open the trunk.
- The Valet (Application) gets the job done without ever seeing your master key.
π The Deep Dive
OAuth vs OpenID Connect (OIDC)
- OAuth 2.0: For Authorization. "Let this app post to my Twitter."
- OIDC: For Authentication. "Sign in with Google." (It's a layer on top of OAuth).
The "Four Actors" in OAuth
- Resource Owner: You (The user).
- Client: The app you are using (e.g., a photo editor).
- Authorization Server: The service that asks "Do you allow this?" (e.g., Google).
- Resource Server: The place where your data lives (e.g., Google Photos).
The Flow (Grant Types)
- Authorization Code Flow: Most secure. Used for web apps. Involves a temporary code that is swapped for a token.
- Implicit Flow: Obsolete. Used for JavaScript apps in the old days.
- Client Credentials: For service-to-service communication (no user involved).
π― Interview Pulse
Use Case: "Sign in with Google"
Why is this safe? Answer: Because the application never sees your Google password. It only receives a "Token" from Google saying "Yes, this is John, and I've verified him."
Scopes
The "Scopes" are the specific permissions requested (e.g., read:email, write:profile). Always mention that tokens should have the Narrowest Scope possible.
Security: state parameter
In the OAuth redirect, we always send a random string called state.
Why? To prevent CSRF (Cross-Site Request Forgery) attacks where an attacker might try to "Inject" their own authorization code into your session.
π