OAuth and Token Handling for Extensions
Implement secure auth — OAuth flow, token storage, refresh, rotation.
What You Will Learn
- Implement OAuth flow.
- Store tokens securely.
- Refresh tokens.
- Rotate credentials.
- Audit token usage.
Why This Matters
OAuth is the highest-risk part of Extension development. Token leakage = account compromise. This lesson builds secure auth.
Concept Explained
OAuth flow: user authorizes → Extension gets code → exchanges for access token → uses token for API calls. Store tokens encrypted. Refresh before expiry. Rotate client secrets.
How It Works
Implement standard OAuth 2.0 flow. Store tokens in encrypted secrets manager. Refresh before expiry. Rotate client secrets quarterly. Audit token usage.
Step-by-Step Tutorial
1. Implement OAuth flow
Authorize URL → user grants → callback with code → exchange for token.2. Store tokens encrypted
Secrets manager (Vault, AWS Secrets, etc.). Never plaintext.3. Refresh before expiry
Check expiry; refresh proactively.4. Rotate client secrets
Quarterly. Update Extension config.5. Audit token usage
Log token use; alert on anomalies.Real-World Example
A team stored OAuth tokens in plaintext DB. DB backup leaked. Tokens compromised. Lesson: always encrypt tokens; use secrets manager.
Example Prompts / Commands / Code
"""const express = require('express');
const crypto = require('crypto');
const app = express();
const CLIENT_ID = process.env.CLIENT_ID;
const CLIENT_SECRET = process.env.CLIENT_SECRET;
const REDIRECT_URI = 'https://my-extension.example.com/callback';
// Step 1: Redirect user to authorize
app.get('/authorize', (req, res) => {
const state = crypto.randomBytes(16).toString('hex');
// Store state in session
const url = `https://github.com/login/oauth/authorize?client_id=${CLIENT_ID}&redirect_uri=${REDIRECT_URI}&scope=read:catalog&state=${state}`;
res.redirect(url);
});
// Step 2: Handle callback
app.get('/callback', async (req, res) => {
const { code, state } = req.query;
// Verify state matches session
// Exchange code for token
const tokenResponse = await fetch('https://github.com/login/oauth/access_token', {
method: 'POST',
headers: { 'Accept': 'application/json' },
body: new URLSearchParams({
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
code,
redirect_uri: REDIRECT_URI,
})
});
const tokens = await tokenResponse.json();
// Store tokens ENCRYPTED in secrets manager
await secretsManager.storeEncrypted(req.user.id, tokens);
res.send('Authorized!');
});
"""
Common Mistakes
- Storing tokens in plaintext — leaks are catastrophic.
- Not verifying state parameter — CSRF attacks.
- Not refreshing before expiry — Extension breaks.
- Never rotating client secrets — accumulated risk.
Best Practices
- Implement standard OAuth 2.0 flow.
- Verify state parameter (CSRF protection).
- Store tokens encrypted in secrets manager.
- Refresh before expiry.
- Rotate client secrets quarterly.
Troubleshooting
| Problem | How to Fix |
|---|---|
| Token expired | Refresh. If refresh token expired, re-authorize. |
| Token compromised | Revoke immediately. Rotate client secret. Notify affected users. |
Practical Exercise
Your Turn
Implement OAuth flow for your Extension. Store tokens in a secrets manager. Test refresh and rotation.
Professional Challenge
Build a token rotation pipeline: quarterly client secret rotation, automatic token refresh, alerts on anomalies.
Key Takeaways
- OAuth flow: authorize → code → token.
- Store tokens encrypted in secrets manager.
- Verify state parameter (CSRF).
- Refresh before expiry.
- Rotate client secrets quarterly.
Frequently Asked Questions
Should I use OAuth or GitHub App?
What about JWT?
How long do tokens last?
Further Reading
Official References
SEO Metadata
SEO title: OAuth and Token Handling for Extensions
Meta description: Implement secure auth — OAuth flow, token storage, refresh, rotation.
Primary keyword: oauth and token handling for extensions
Secondary keywords: oauth and token handling for extensions
Search intent: Informational
URL slug: /copilot-extension-oauth-token-handling
Categories: AI Tools, GitHub Copilot
Tags: GitHub Copilot, Professional, Extensions, OAuth, Tokens, Security, IMCSEIAN, Tutorial, IMCSEIAN
Featured image concept: IMCSEIAN lesson card for OAuth and Token Handling for Extensions
Comments
Comments
Post a Comment