Oauth

From G1Joshi/Agent-Skills · View interactive version

Oauth is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert OAuth 2.0 / 2.1 protocol assistance covering Authorization Code Flow with PKCE, Client Credentials, and token exchange. Use when implementing third-party logins, securing API endpoints, or configuring OAuth servers.

How to use this skill

Skill file

OAuth 2.1

OAuth 2.1 is the consolidation of OAuth 2.0 and its best practices into a single standard. It allows third-party applications to grant limited access to an HTTP service through an authorization server.

When to Use

Quick Start

// Client (Frontend) - redirect to Auth Server
const authUrl = `https://auth.example.com/authorize?
  response_type=code&
  client_id=${CLIENT_ID}&
  redirect_uri=${REDIRECT_URI}&
  scope=read:profile&
  code_challenge=${pkceChallenge}&
  code_challenge_method=S256`;

window.location.href = authUrl;

// Callback (Handling the redirect)
const code = new URLSearchParams(window.location.search).get("code");
const tokenResponse = await fetch("https://auth.example.com/token", {
  method: "POST",
  body: JSON.stringify({
    grant_type: "authorization_code",
    code,
    client_id: CLIENT_ID,
    redirect_uri: REDIRECT_URI,
    code_verifier: pkceVerifier, // Proof Key
  }),
});

Core Concepts

The 4 OAuth 2.0 / 2.1 Grant Types

| Grant Type | Client Type | Use Case |

| :------------------------------- | :----------------------------------------------- | :---------------------------------------------- |

| Authorization Code with PKCE | Public (SPA, Mobile) & Confidential (Web Server) | Standard user login and authorization |

| Client Credentials | Confidential (Backend Daemons) | Machine-to-machine background communication |

| Refresh Token | Confidential & Public | Renewing expired access tokens without re-login |

| Device Authorization | Input-constrained (Smart TV, CLI) | Authenticating via secondary browser screen |

Proof Key for Code Exchange (PKCE) Protocol Flow

Protects authorization codes from interception on public clients:

[ Client ] ──1. Generate Verifier & Challenge (SHA256)──→ Local
[ Client ] ──2. GET /authorize?code_challenge=xyz───────→ [ Auth Server ]
[ Client ] ←─3. Receive Authorization Code─────────────── [ Auth Server ]
[ Client ] ──4. POST /token?code=123&code_verifier=abc──→ [ Auth Server ]
[ Client ] ←─5. Receive Access Token & Refresh Token───── [ Auth Server ]

Scopes & Consent Management

Defines granular operational permissions approved by the resource owner:

POST /oauth/token HTTP/1.1
Host: auth.example.com
Content-Type: application/x-www-form-urlencoded

grant_type=client_credentials
&client_id=service_analytics
&client_secret=supersecret
&scope=read:reports%20export:csv

Common Patterns

Authorization Code Flow with PKCE (Proof Key for Code Exchange)

Problem: Public clients (SPAs, mobile apps) cannot safely store client secrets, making authorization codes vulnerable to interception.

Solution:

Generate cryptographic code verifier and code challenge on the client before initiating authorization:

// Generate Code Verifier and S256 Challenge
const verifier = generateRandomString(64);
const challenge = base64UrlEncode(sha256(verifier));

// Redirect user to authorization endpoint
const authUrl =
  `https://auth.example.com/oauth/authorize?` +
  `response_type=code&client_id=spa-client&` +
  `redirect_uri=${encodeURIComponent(redirectUri)}&` +
  `code_challenge=${challenge}&code_challenge_method=S256`;

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :---------------------- | :--------------------------- | :-------------------------------- |

| invalid_grant | Code expired or reused. | Get a new authorization code. |

| redirect_uri_mismatch | URI doesn't match allowlist. | Check dashboard settings exactly. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.