đ§ Introduction â OAuth2, Demystified
You've probably clicked "Login with Google" or "Sign in with GitHub" more times than you can count. But have you ever stopped and wondered:
What actually happens under the hood when I do that?
That slick one-click login experience is powered by OAuth2, a protocol designed to let applications access user data without asking for passwords. Itâs one of the most critical yet misunderstood pieces of modern web security.
Whether youâre building a web app, a mobile app, or an API-driven service, there's a good chance you'll need to integrate OAuth2 at some point. But hereâs the catch:
Most OAuth2 documentation reads like a textbook written by robots for robots.
In this article, Iâll walk you through OAuth2 in plain Englishâwhat it is, why it exists, and how it worksâstep by step.
By the end, you'll:
- Understand the core flow of OAuth2 (without memorizing jargon)
- Know what each piece of the protocol does and why
đ What is OAuth2 (and Why Should You Care?)
OAuth2 is a protocol that lets apps access user data on another serviceâsecurely and with permissionâwithout ever seeing the userâs password.
Think of it like this:
OAuth2 is like handing a valet a special key that only starts your carâbut doesnât open your glove box, trunk, or access your phone via Bluetooth.
The valet (a third-party app) can drive your car (perform limited tasks) without ever having full control over your belongings (like your personal data).
đ€ Why Not Just Share the Password?
Before OAuth2, some apps literally asked users for their credentials to access third-party services (like email or cloud storage). Sketchy, right?
OAuth2 solves this problem by letting users authorize access through a secure process handled by the service they trust (like Google or GitHub). The user logs in there, not in your app, and your app receives a special token that allows access to certain parts of their data.
đ§© Core Use Cases of OAuth2
Youâve probably already interacted with OAuth2 if youâve done any of these:
- Signed into a website using Google, GitHub, Facebook, or Twitter
- Connected your GitHub repo to a CI/CD service like Netlify or CircleCI
- Granted a mobile app access to your Google Calendar or Contacts
- Authorized Postman to use your Spotify account data for testing
Behind the scenes, OAuth2 makes all of that possibleâwithout ever giving away your actual login credentials.
đšâđ» Why It Matters to Developers
If youâre building an app that needs to:
- Let users log in with a third-party account
- Access APIs like GitHub, Google Calendar, Dropbox, Spotify, etc.
- Allow integrations with services on behalf of the user
...then OAuth2 is your new best friend.
And while it can be complex, understanding the fundamentals will help you:
- Avoid confusing errors (like that dreaded
CORSorinvalid_redirect_uristuff) - Secure your app against token misuse and CSRF attacks
- Build user trust with a seamless and safe login experience
Next, letâs break down the roles involved in an OAuth2 flowâlike whoâs handing the valet key and whoâs driving the car. đ§âđ§
đ§± The Key Players in OAuth2 (Who's Who in the Zoo)
OAuth2 can feel like a lot, but once you understand whoâs playing what role, it all starts to click.
Letâs break down the four main characters in this security dramaâusing simple language and real-world metaphors.
đ€ 1. Resource Owner â The User
This is the person who owns the data and grants permission.
Think of them as the car owner who decides whether or not to give someone the valet key.
Theyâre the ones who log into Google or GitHub and approve your app's request.
đ± 2. Client â Your Application
This is your app that wants access to the userâs data.
Your app is the valet asking for permission to drive the carâbut it needs to go through the proper process to prove it's trustworthy.
The client never sees the userâs credentials (like their password). Instead, it gets a token after the user grants permission.
đ 3. Authorization Server â The Gatekeeper
This is the service that authenticates the user and issues tokens.
Think of it like the front desk at a hotel that verifies the userâs identity and gives out digital keycards with limited access.
Examples:
- Googleâs OAuth2 server
- GitHubâs OAuth app system
- Facebookâs Login service
This is the place users are redirected to when they click âLogin with GitHubâ.
đ 4. Resource Server â The Data Vault
This is the server that holds the userâs protected dataâlike email, profile info, repositories, playlists, etc.
Itâs the garage where the car is parked, and only lets people in who have the right kind of valet key (a valid access token).
In many cases, the Authorization Server and Resource Server are the same (e.g., in Googleâs system), but they donât have to be.
đŻ Real Example: Logging in With GitHub
| Role | Who It Is |
|---|---|
| Resource Owner | You (the user) |
| Client | Your web app |
| Authorization Server | GitHubâs OAuth server |
| Resource Server | GitHubâs API (e.g., /user, /repos) |
đ The OAuth2 Flow â Step by Step (Authorization Code Grant)
Now that we know the key players, letâs walk through how they actually interact in the most commonly used OAuth2 flow: the Authorization Code Grant.
This is the flow typically used for web apps where you want to authenticate users securely.
TL;DR: Your app asks for permission, the user approves it, and your app gets a temporary "hall pass" (access token) to call APIs on their behalf.
Letâs break it down step by step đ
đ§š Step 1: The User Clicks âLogin with GitHubâ
Your app presents a button:
<a href="https://github.com/login/oauth/authorize?client_id=YOUR_CLIENT_ID&redirect_uri=http://localhost:8080/callback&scope=user">
Login with GitHub
</a>
When the user clicks it, theyâre redirected to the Authorization Server (in this case, GitHub).
đ Step 2: Redirect to Authorization Server
The browser navigates to a URL like this:
https://github.com/login/oauth/authorize
?client_id=abc123
&redirect_uri=http://localhost:8080/callback
&scope=user
&state=randomstring
Hereâs what each query parameter means:
-
client_id: Your appâs public identifier on GitHub -
redirect_uri: Where GitHub should send the user afterward -
scope: What your app is requesting (e.g.,user,repo) -
state: A random string to protect against CSRF attacks
đ€ Step 3: User Logs In and Approves Access
GitHub shows a prompt:
âDo you want to allow
Your Appto access your profile?â
Once the user logs in and approves, GitHub redirects them back to your app via the redirect_uri.
đ§Ÿ Step 4: Authorization Code is Sent Back
Your app receives a GET request at the callback endpoint:
GET /callback?code=123456&state=randomstring
-
code: A temporary authorization code youâll now exchange for a token. -
state: You verify this matches what you originally sent (CSRF protection).
đ Step 5: App Exchanges Code for Access Token
Your backend makes a POST request to GitHubâs token endpoint:
POST https://github.com/login/oauth/access_token
Content-Type: application/json
{
"client_id": "abc123",
"client_secret": "your-client-secret",
"code": "123456",
"redirect_uri": "http://localhost:8080/callback"
}
đ§© Where do you get your
client_idandclient_secret?
To get these, you need to register a new OAuth application on GitHub.
Once registered, GitHub will give you:
- A Client ID (public identifier for your app)
- A Client Secret (keep this safeâused for secure token exchanges)
If everything checks out, GitHub responds:
{
"access_token": "gho_abcdef123456",
"scope": "user",
"token_type": "bearer"
}
You now have the tokenâbut not user info yet.
đ Step 6: App Uses Access Token to Access User Data
You can now call GitHubâs API on behalf of the user:
GET https://api.github.com/user
Authorization: Bearer gho_abcdef123456
GitHub responds with the userâs profile info:
{
"login": "yourusername",
"name": "Denil",
"avatar_url": "https://...",
...
}
Mission accomplished! đŻ Youâve successfully authenticated a user using OAuth2 and can now personalize the app based on their profile.
đ§ Coming Up in Part 2:
In the next part of this series, weâll roll up our sleeves and implement the entire OAuth2 flow in Go, using thegolang.org/x/oauth2package with GitHub as our OAuth provider.
Youâll learn how to:
- Register an OAuth app on GitHub to get your
client_idandclient_secret- Set up a simple Go web server to handle login, callback, and token exchange
- Use the access token to fetch real user data from the GitHub API
Stay tunedâPart 2 will turn everything weâve learned into real, working code. đ»đ„
Top comments (0)