Skip to content
Join free
mcollina

Oauth skill

Implements OAuth 2.0/2.1 authorization flows in Fastify applications — configures authorization code with PKCE, client credentials, device flow, refresh token rotation, JWT validation, and token introspection/revocation endpoints.

$npx alyn-cli add oauth
View this skill on GitHubmcollina/skills/skills/oauth/SKILL.md
Pack stars
1,981
Pack installs
131k
Alyn Score
66/99
Updated
9 days ago
License
MIT

mcollina

Oauth

What is the Oauth skill?

Oauth is a backend skill by mcollina for Claude Code, Cursor, Codex and other AI coding agents. Implements OAuth 2.0/2.1 authorization flows in Fastify applications — configures authorization code with PKCE, client credentials, device flow, refresh token rotation, JWT validation, and token introspection/revocation endpoints.

Use this skill when you need to: - Implement or debug an OAuth 2.0/2.1 flow in a Fastify application - Validate tokens, configure PKCE, or set up refresh token rotation - Secure Fastify routes and plugins with access-control middleware - Resolve RFC compliance questions or identify security anti-patterns

When to use it

Use this skill when you need to: - Implement or debug an OAuth 2.0/2.1 flow in a Fastify application - Validate tokens, configure PKCE, or set up refresh token rotation - Secure Fastify routes and plugins with access-control middleware - Resolve RFC compliance questions or identify security anti-patterns export default fp(async function (fastify: FastifyInstance) { fastify.register(oauth2, { name: 'oauth2', scope: ['openid', 'profile', 'email'], credentials: { client: { id: process.env.CLIENTID!, secret: process.env.CLIENTSECRET!, }, auth: { authorizeHost: process.env.AUTHSERVER!, authorizePath: '/authorize', tokenHost: process.env.AUTHSERVER!, tokenPath: '/token', }, }, startRedirectPath: '/login', callbackUri: process.env.CALLBACKURI!, pkce: 'S256', // RFC 7636 — always use for public clients generateStateFunction: (req) = req.session.state = crypto.randomUUID(), checkStateFunction: (req, callback) = req.query.state === req.session.state ?

What the SKILL.md covers

  • When to use
  • Step-by-step: Authorization Code + PKCE in Fastify
  • 1. Install dependencies
  • 2. Register the OAuth plugin
  • 3. Handle the callback and exchange the code
  • 4. JWT validation middleware (token introspection hook)
  • 5. Protecting routes
  • 6. Refresh token rotation
  • Security checklist
  • Common anti-patterns
Author
mcollina
Pack
mcollina/skills (11 skills)
Category
Backend, #159 of 317
Alyn Score
66 / 99
Files
2
License
MIT
Last updated
9 days ago
Works with
Claude Code, Claude, Cursor, Codex, Gemini CLI, Copilot

Oauth compared to similar skills

SkillAlyn ScoreGitHub starsUpdated
Oauth · mcollina662k9 days ago
Security Audit · laolaoshiren469272 days ago
Backend Security Coder · rmyndharis662.1k18 days ago
Competition Oauth Oidc Chain · zhaoxuya5208345k11 days ago

Install Oauth

Pick your agent. Every command is ready to paste.

$npx alyn-cli add oauth

Detects your agents (Claude Code, Cursor, Codex and 70+ more) and installs to the right folder. Add -g to install globally.

$npx skills add mcollina/skills --skill oauth

Or use the open skills CLI directly.

01

Install it

Run the command above, or upload the folder in Claude.

02

Ask naturally

Describe the task. Your agent loads oauth when the request matches its description.

03

Name it if needed

If it does not trigger, say “use the oauth skill”. In Codex, type $oauth.

The skill itself

SKILL.md

FolderGitHub

When to use

Use this skill when you need to:

  • Implement or debug an OAuth 2.0/2.1 flow in a Fastify application
  • Validate tokens, configure PKCE, or set up refresh token rotation
  • Secure Fastify routes and plugins with access-control middleware
  • Resolve RFC compliance questions or identify security anti-patterns

Step-by-step: Authorization Code + PKCE in Fastify

1. Install dependencies

npm install @fastify/oauth2 @fastify/cookie @fastify/session fastify-plugin

2. Register the OAuth plugin

// plugins/oauth.ts
import fp from 'fastify-plugin'
import oauth2, { OAuth2Namespace } from '@fastify/oauth2'
import { FastifyInstance } from 'fastify'

export default fp(async function (fastify: FastifyInstance) {
  fastify.register(oauth2, {
    name: 'oauth2',
    scope: ['openid', 'profile', 'email'],
    credentials: {
      client: {
        id: process.env.CLIENT_ID!,
        secret: process.env.CLIENT_SECRET!,
      },
      auth: {
        authorizeHost: process.env.AUTH_SERVER!,
        authorizePath: '/authorize',
        tokenHost: process.env.AUTH_SERVER!,
        tokenPath: '/token',
      },
    },
    startRedirectPath: '/login',
    callbackUri: process.env.CALLBACK_URI!,
    pkce: 'S256',               // RFC 7636 — always use for public clients
    generateStateFunction: (req) => req.session.state = crypto.randomUUID(),
    checkStateFunction: (req, callback) =>
      req.query.state === req.session.state ? callback() : callback(new Error('State mismatch')),
  })
})

Validation checkpoint: Confirm callbackUri exactly matches a registered redirect URI at the authorization server before proceeding (RFC 6749 §3.1.2).

3. Handle the callback and exchange the code

// routes/auth.ts
import { FastifyInstance } from 'fastify'

export default async function authRoutes(fastify: FastifyInstance) {
  fastify.get('/login/callback', async (request, reply) => {
    // @fastify/oauth2 verifies state and exchanges code automatically
    const tokenResponse = await fastify.oauth2.getAccessTokenFromAuthorizationCodeFlow(request)

    // Store only what you need; never log the raw token
    request.session.set('accessToken', tokenResponse.token.access_token)
    request.session.set('refreshToken', tokenResponse.token.refresh_token)

    return reply.redirect('/')
  })

  fastify.get('/logout', async (request, reply) => {
    await request.session.destroy()
    return reply.redirect('/')
  })
}

4. JWT validation middleware (token introspection hook)

// hooks/verifyToken.ts
import { FastifyRequest, FastifyReply } from 'fastify'
import jwt from '@fastify/jwt'

export async function verifyToken(request: FastifyRequest, reply: FastifyReply) {
  try {
    await request.jwtVerify()
    // Validate required claims (RFC 7519)
    const payload = request.user as Record<string, unknown>
    const now = Math.floor(Date.now() / 1000)

    if (typeof payload.exp === 'number' && payload.exp < now)
      return reply.code(401).send({ error: 'token_expired' })

    if (payload.iss !== process.env.EXPECTED_ISSUER)
      return reply.code(401).send({ error: 'invalid_issuer' })

    if (payload.aud !== process.env.EXPECTED_AUDIENCE)
      return reply.code(401).send({ error: 'invalid_audience' })

  } catch (err) {
    return reply.code(401).send({ error: 'invalid_token', error_description: (err as Error).message })
  }
}

Validation checkpoints:

  • Verify exp, iss, aud, and sub on every request — never skip (RFC 7519 §4)
  • Use fastify.jwt.verify (asymmetric RS256/ES256) rather than HS256 for tokens issued by a third-party server

5. Protecting routes

// routes/api.ts
import { FastifyInstance } from 'fastify'
import { verifyToken } from '../hooks/verifyToken'

export default async function apiRoutes(fastify: FastifyInstance) {
  fastify.addHook('onRequest', verifyToken)   // applies to all routes in this scope

  fastify.get('/me', {
    schema: {
      response: { 200: { type: 'object', properties: { sub: { type: 'string' } } } },
    },
  }, async (request) => {
    const user = request.user as { sub: string }
    return { sub: user.sub }
  })
}

6. Refresh token rotation

async function refreshAccessToken(fastify: FastifyInstance, refreshToken: string) {
  const newToken = await fastify.oauth2.getNewAccessTokenUsingRefreshTokenFlow({ refresh_token: refreshToken })

  // Always replace the stored refresh token if rotation is in use (RFC 6749 §10.4)
  return {
    accessToken: newToken.token.access_token,
    refreshToken: newToken.token.refresh_token ?? refreshToken,
  }
}

Security checklist

Requirement RFC reference
Validate redirect URI against allowlist RFC 6749 §3.1.2
PKCE (S256) for all public clients RFC 7636 §4.2
Validate state to prevent CSRF RFC 6749 §10.12
Validate iss, aud, exp on every JWT RFC 7519 §4
Rotate refresh tokens on every use RFC 6749 §10.4
Use HTTPS everywhere; reject HTTP redirect URIs RFC 6749 §3.1.2.1
Rate-limit token endpoints OAuth 2.1 §7

Common anti-patterns

  • Storing tokens in localStorage — use HttpOnly, Secure, SameSite=Strict cookies instead
  • Skipping audience validation — allows token reuse across services
  • Using implicit flow — deprecated in OAuth 2.1; use authorization code + PKCE
  • Accepting response_type=token in browser apps — tokens in URL fragments leak in logs/referrers
  • Symmetric signing (HS256) for third-party tokens — use RS256/ES256 with JWKS endpoint

Further implementation references

  • See DEVICE_FLOW.md for device authorization flow (RFC 8628) implementation
  • See TOKEN_VALIDATION.md for JWKS rotation, caching strategies, and opaque token introspection
  • See CLIENT_CREDENTIALS.md for machine-to-machine service authentication patterns
  • See MOBILE_OAUTH.md for native/mobile app flows (RFC 8252) and custom URI schemes

Tips for Oauth

  • • Start a fresh session after installing so your agent picks up the new skill.
  • • It is part of a pack with 11 skills. Install only the ones you need with --skill to keep triggers precise.
  • • If two skills overlap, mention the one you want by name.

Questions about Oauth

What does the Oauth skill do?

Implements OAuth 2.0/2.1 authorization flows in Fastify applications — configures authorization code with PKCE, client credentials, device flow, refresh token rotation, JWT validation, and token introspection/revocation endpoints.

How do I install Oauth?

Run "npx alyn-cli add oauth" in your project. The Alyn CLI installs it for Claude Code, Cursor, Codex and 70+ other agents. In Claude on the web, upload the skill folder as a ZIP under Customize > Skills.

Does Oauth work in Cursor and Codex?

Yes. Oauth uses the open SKILL.md format, so it works in Claude Code, Claude, Cursor, OpenAI Codex, Gemini CLI, GitHub Copilot and other agents that support agent skills.

When should I use the Oauth skill?

Use this skill when you need to: - Implement or debug an OAuth 2.0/2.1 flow in a Fastify application - Validate tokens, configure PKCE, or set up refresh token rotation - Secure Fastify routes and plugins with access-control middleware - Resolve RFC compliance questions or identify security anti-patterns export default fp(async function (fastify: FastifyInstance) { fastify.register(oauth2, { name: 'oauth2', scope: ['openid', 'profile', 'email'], credentials: { client: { id: process.env.CLIENTID!, secret: process.env.CLIENTSECRET!, }, auth: { authorizeHost: process.env.AUTHSERVER!, authorizePath: '/authorize', tokenHost: process.env.AUTHSERVER!, tokenPath: '/token', }, }, startRedirectPath: '/login', callbackUri: process.env.CALLBACKURI!, pkce: 'S256', // RFC 7636 — always use for public clients generateStateFunction: (req) = req.session.state = crypto.randomUUID(), checkStateFunction: (req, callback) = req.query.state === req.session.state ?

What is inside the Oauth skill?

The SKILL.md covers When to use, Step-by-step: Authorization Code + PKCE in Fastify, 1. Install dependencies, 2. Register the OAuth plugin, 3. Handle the callback and exchange the code, 4. JWT validation middleware (token introspection hook), and the skill folder has 2 files.

What is a good alternative to Oauth?

Security Audit by laolaoshiren is the highest-ranked similar skill from another creator, with an Alyn Score of 46 compared to 66 for Oauth.

Is Oauth free?

Oauth is open source on GitHub under the MIT license. It is free to install; check the license before commercial use.

Who made Oauth?

Oauth is published by mcollina in the mcollina/skills repository, together with 10 other skills.

Similar skills. From other creators.