Demystifying OAuth 2.0 with a Clubbing Analogy
Tags: oauth, security, google
Published: 2024-03-26 12:07:57

Understanding OAuth 2.0 authentication can be complex. To simplify, we compare it to a club night out, using Google OAuth 2.0 as our example. This analogy will help demystify how applications authenticate users and manage access. Let's break down this process into more relatable terms.


### Key Technical Terms and Their Club Night Out Analogies

| **Technical Term** | **Analogy** |
|--------------------|-------------|
| **Client ID** | Akin to an event flyer with a QR code that gets you started on your journey to the club. |
| **Client Secret** | Similar to a secret passphrase given to the club's entrance (the backend) by the club's partner (Google), ensuring that the entrance is authorized to request VIP passes on behalf of guests. |
| **Google OAuth** | Comparable to the special pass system managed by the club's partner, verifying your identity and granting access. |

#### Clarifications

- **Where does the Client Secret come from?** The Client Secret is provided by Google when the application is registered with Google's OAuth system. It's like when the club's entrance (the backend of the application) gets a secret passphrase directly from the club's partner (Google) as proof of their partnership and authorization to request VIP passes.
- **Why does the backend have a Client Secret at all?** The backend holds the Client Secret to prove its identity and authorization to Google. It's necessary because it ensures that not just anyone can request access tokens on behalf of the application. Think of it as a way to prevent unauthorized parties from acting as the club's entrance and requesting VIP passes without proper authorization.

### Authentication Flow and Club Night Out Analogy Explanation

| **Authentication Flow Steps** | **Club Night Out Analogy** |
|-------------------------------|----------------------------|
| **1. Redirect to Google OAuth Page:** Frontend (F) directs the user to Google's OAuth 2.0 authentication page. | **1. Heading to Google Club Partner's Booth:** You follow the instructions (like a QR code on a flyer) to go to the Google Club Partner's booth to start the access process. |
| **2. User Grants Access:** User agrees to grant the application access to specified data on the consent screen. | **2. Showing Your ID at the Booth:** You show your ID (Google credentials) at the booth, allowing Google to verify who you are, similar to giving consent for a VIP status check. |
| **3. Auth Code via /auth/google/redirect:** Google redirects the user back to the application with an authorization code. | **3. Receiving a Special Code:** Google verifies your ID and gives you a special code, like a temporary VIP pass, to take to the club's entrance. |
| **4. Exchange Code with Token:** The backend receives the code and sends it back to Google, alongside the clientID and clientSecret for authentication. | **4. Exchanging the Code at the Club Entrance:** You present the special code at the club's entrance. The entrance then uses the secret passphrase (clientSecret) to prove to Google that it's authorized to request the actual VIP pass. |
| **5. Token Response:** Google validates the authorization code, clientID, and clientSecret, then sends an access token back to the application. | **5. Getting the Pass:** Google recognizes the passphrase, validating the club's entrance request, and issues the actual VIP pass (access token). |
| **6. Create JWT Token:** The backend generates a JWT for the authenticated user using the access token. | **6. Creating Your VIP Badge:** With the VIP pass received, the club's entrance creates a VIP badge for you, symbolizing your verified access. |
| **7. Set JWT Token in Cookie and Redirect:** The backend places the JWT in an HTTP-only cookie and redirects the user to the dashboard on the Frontend. | **7. Entering the Club:** With the VIP badge (the JWT in a cookie), you're granted entry into the club, ready to enjoy its exclusive amenities. |

Now lets have a look at the technical sequence diagram of the OAuth Authentication flow and see if we can understand it better:


![Google OAuth Sequence Diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zeluttnbmplwvcerbwfy.png)

Hopefully it is now more clear :)

For a deeper dive into Google OAuth 2.0, check out this [Google Developers post](https://developers.google.com/identity/protocols/oauth2).

Thanks for reading!

As always, I appreciate any feedback. 🍺