agentleFS
reddygopalkrishnareddygopalkrishna/developer-agent-skills/skills/auth-implementation/SKILL.md

auth-implementation

Skill8 starsChanged 9 days agoMITView on GitHub

Implements authentication and authorization safely - OAuth 2.0 and OpenID Connect flows with PKCE, sessions vs tokens, token validation, refresh rotation, logout, RBAC/ABAC/ReBAC checks, service-to-service auth, MFA and passkeys. Use when adding login or SSO, integrating an identity provider, handling JWTs, access or refresh tokens, cookies or sessions, protecting APIs, adding roles and permissions, or reviewing auth code. Not for general security-audit reviews (use security-audit).

4 other projects use a version of this.

Adopted by

Most recently taken up by

The file

Show its rules and text

We split this skill into the rules it gives an agent

19 rules. 0 are common, 13 are distinctive, and 6 are very specific to this project.

  • 0

    Common

    Most of the 4 edited versions we read keep these.

  • 13

    Distinctive

    This author's own choices. Worth a look.

  • 6

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Don't build crypto or an IdP from scratch: Use a managed IdP or a maintained, widely used library for OAuth/OIDC, session handling, …

    line 21
  2. Very specific:

    Never put secrets where the browser can read them: no client secrets in SPAs or mobile apps, no tokens in localStorage, no tokens in URLs.

    line 24
  3. Very specific:

    Use PKCE with S256 for every client, including confidential ones.

    line 43
  4. Distinctive:

    Read references/oauth-oidc-checklist.md for the full step-by-step checklist when implementing or reviewing a flow.

    line 50
  5. Very specific:

    Verify the signature using keys from the IdP's JWKS (jwks_uri from the discovery document).

    line 56
  6. Very specific:

    Check iss equals the expected issuer, aud contains your client ID, exp is in the future (small clock skew allowance), and nonce matches.

    line 57
  7. Distinctive:

    Use the ID token to establish your session.

    line 58
  8. Very specific:

    If it's a JWT: verify signature, allowlist the algorithm (never accept none, never let the token choose between symmetric and asymmetric), …

    line 62
  9. Distinctive:

    If it's opaque: use the IdP's introspection endpoint (RFC 7662) and cache results briefly.

    line 63
  10. Distinctive:

    If the user should also leave the IdP session, redirect to the IdP's end-session endpoint (OIDC RP-initiated logout) with an allowlisted …

    line 88
  11. Distinctive:

    Follow NIST SP 800-63B: favor length over composition rules, check new passwords against breached-password lists, no forced periodic …

    line 94
  12. Distinctive:

    Check on every request and at the object level: "may this user perform this action on this record?", not just "is this user logged in?".

    line 109
  13. Distinctive:

    Write tests for denial: wrong user, wrong tenant, wrong role, expired token, no token.

    line 113
  14. Distinctive:

    Prefer workload identity (cloud-managed identities, Kubernetes service account federation, SPIFFE) over long-lived shared secrets.

    line 117
  15. Distinctive:

    When acting on behalf of a user, propagate the user's identity (token exchange, RFC 8693) instead of using a god-mode service account.

    line 120
  16. Distinctive:

    Never paste real user credentials, tokens or client secrets into code, logs, tests or chat.

    line 140
  17. Very specific:

    Every token is validated (signature, algorithm, iss, aud, exp, scopes) before use.

    line 146
  18. Distinctive:

    No tokens or secrets are readable by browser JavaScript unless the no-backend SPA case was chosen deliberately.

    line 147
  19. Distinctive:

    Every endpoint has a server-side authorization check at object level, with denial tests.

    line 148

“Common” is measured: kept by at least 6 in 10 of the 4 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Auth Implementation

Auth bugs are quiet: the feature works for every honest user and fails only for the attacker. Use a vetted identity provider or library, pick the flow from the table below, validate every token fully, and check authorization on the server for every request and every object.

When to use

  • Adding sign-in, SSO, social login, or integrating an identity provider (IdP).
  • Issuing, storing, validating or refreshing JWTs, access tokens or refresh tokens.
  • Choosing between cookie sessions and tokens, especially for single-page apps.
  • Adding roles, permissions, tenant isolation, or service-to-service authentication.
  • Reviewing existing auth code or debugging a login/logout problem.

Ground rules

  1. Don't build crypto or an IdP from scratch. Use a managed IdP or a maintained, widely used library for OAuth/OIDC, session handling, password hashing and token signing. Your code wires them together.
  2. Authentication says who; authorization says what. Both happen on the server. Hiding a button is not authorization.
  3. Deny by default. A route with no explicit policy should reject.
  4. Never put secrets where the browser can read them: no client secrets in SPAs or mobile apps, no tokens in localStorage, no tokens in URLs.
  5. Follow current guidance: the OAuth 2.0 Security Best Current Practice (RFC 9700) and OpenID Connect Core. The OAuth 2.1 draft consolidates the same rules. When unsure about a detail, read the spec or the IdP's docs rather than guessing.

Choose the flow

ClientRecommendedNotes
Server-rendered web appAuthorization code + PKCE, confidential clientStore tokens server-side; browser gets a session cookie
Single-page app with a backend you controlBackend-for-frontend (BFF): the backend runs the code flow and keeps tokens; browser gets an HttpOnly session cookieRemoves tokens from JavaScript entirely; preferred
Single-page app with no backendAuthorization code + PKCE as a public clientKeep access tokens in memory, short-lived; refresh token rotation or sender-constrained tokens
Native / mobile appAuthorization code + PKCE through the system browser (RFC 8252)Never an embedded web view for login; no client secret in the app
CLI, TV, IoT with limited inputDevice authorization grant (RFC 8628)Poll politely; show the verification URL and code
Machine to machineClient credentials, or workload identity / mTLSNo user involved; scope tokens narrowly
Your own username/password form, no IdPServer-side sessions + a strong password hashConsider an IdP anyway; you take on MFA, recovery, breach handling

Do not use the implicit grant or the resource owner password credentials grant. RFC 9700 says clients SHOULD NOT use the implicit grant and the password grant MUST NOT be used; the OAuth 2.1 draft removes both.

Authorization code flow checklist

  • Use PKCE with S256 for every client, including confidential ones.
  • Send a random, single-use state and verify it on callback (CSRF protection for the login itself).
  • For OIDC, send a nonce and verify it in the ID token.
  • Register exact redirect URIs. No wildcards, no open redirects after login (validate any returnTo against an allowlist of paths).
  • Exchange the code on the back channel; codes are single-use and short-lived.
  • Request the minimum scopes.

Read references/oauth-oidc-checklist.md for the full step-by-step checklist when implementing or reviewing a flow.

Validate tokens fully

ID token (proves the user signed in to your client):

  • Verify the signature using keys from the IdP's JWKS (jwks_uri from the discovery document). Cache keys; refetch on unknown kid.
  • Check iss equals the expected issuer, aud contains your client ID, exp is in the future (small clock skew allowance), and nonce matches.
  • Use the ID token to establish your session. Don't send it to APIs as an access token.

Access token (presented to your API):

  • If it's a JWT: verify signature, allowlist the algorithm (never accept none, never let the token choose between symmetric and asymmetric), check iss, aud equals this API, exp, and required scopes or roles.
  • If it's opaque: use the IdP's introspection endpoint (RFC 7662) and cache results briefly.
  • JWT payloads are only base64-encoded, not encrypted. Don't put sensitive data in them.

Sessions vs tokens

Server-side session + cookieTokens held by the client
RevocationImmediate (delete the session)Hard until expiry; needs short lifetimes or introspection
XSS exposureCookie is HttpOnly, not readable by scriptTokens in JS memory can be used by injected script
CSRF exposureNeeds SameSite + CSRF tokens for state-changing requestsNot cookie-based, so no CSRF; bearer tokens are the risk
Best forBrowsersAPIs called by mobile apps, services, third parties

Cookie settings for session cookies: HttpOnly; Secure; SameSite=Lax (or Strict if cross-site navigation doesn't need to stay logged in), Path=/, and the __Host- name prefix when possible (it requires Secure, Path=/ and no Domain). Regenerate the session ID at login and privilege change (prevents session fixation). Set idle and absolute timeouts.

Token lifetimes, refresh and revocation

  • Access tokens: short-lived (minutes, not days).
  • Refresh tokens: rotate on every use; if an old refresh token is reused, revoke the whole token family (it was stolen). Bind refresh tokens to the client; for public clients use rotation or sender-constrained tokens (DPoP, RFC 9449; or mTLS-bound tokens, RFC 8705).
  • Support revocation (RFC 7009) on logout and on password or MFA change.
  • Store refresh tokens encrypted at rest on the server side.

Logout

  1. Destroy the local session (server-side) and clear the cookie.
  2. Revoke refresh tokens you hold.
  3. If the user should also leave the IdP session, redirect to the IdP's end-session endpoint (OIDC RP-initiated logout) with an allowlisted post-logout redirect.
  4. For multiple apps sharing an IdP, consider OIDC back-channel logout so other apps end their sessions.

Passwords, MFA and passkeys (if you own credentials)

  • Hash with Argon2id (preferred), scrypt or bcrypt via a maintained library, with parameters from current OWASP guidance. PBKDF2 only where FIPS requires it.
  • Follow NIST SP 800-63B: favor length over composition rules, check new passwords against breached-password lists, no forced periodic rotation.
  • Rate-limit and add backoff on login, MFA and reset endpoints. Return the same response for "unknown user" and "wrong password".
  • Password reset tokens: random, single-use, short expiry, stored hashed, invalidated on use and on password change.
  • MFA: offer passkeys/WebAuthn (phishing-resistant) and TOTP. SMS is the weakest option. Require re-authentication for sensitive actions (changing email, password, MFA, payout details).

Authorization

ModelUse whenExample
RBAC (roles)Permissions follow job functionadmin, editor, viewer
ABAC (attributes)Rules depend on attributes of user, resource, context"Managers may approve expenses under their cost center below 5,000"
ReBAC (relationships)Access follows ownership/sharing graphsDocs shared with a user or group, nested folders (Zanzibar-style systems such as OpenFGA, SpiceDB)

Rules:

  • Check on every request and at the object level: "may this user perform this action on this record?", not just "is this user logged in?". Missing object checks cause IDOR, the most common auth bug.
  • Derive user and tenant from the authenticated session or token, never from request parameters.
  • Centralize policy in one module or a policy engine (for example OPA or Cedar) instead of scattered if checks.
  • Filter list endpoints by permission in the query, not after fetching.
  • Write tests for denial: wrong user, wrong tenant, wrong role, expired token, no token.

Service-to-service

  • Prefer workload identity (cloud-managed identities, Kubernetes service account federation, SPIFFE) over long-lived shared secrets.
  • Otherwise use the client credentials grant with narrowly scoped tokens, or mTLS.
  • Each service validates incoming tokens itself; don't trust "it came from inside the network".
  • When acting on behalf of a user, propagate the user's identity (token exchange, RFC 8693) instead of using a god-mode service account.

Common bugs

BugFix
JWT signature not verified, or alg: none acceptedUse a library; pin allowed algorithms; verify before reading claims
aud not checked, so tokens for another API are acceptedCheck aud equals this API
Authorization only in the frontendEnforce on the server for every endpoint
IDOR: /orders/123 returns any user's orderScope queries by the session's user/tenant
Tokens in localStorageBFF with HttpOnly cookie, or in-memory tokens
Missing state or exact redirect URI matchingAdd state; register exact URIs
Open redirect after loginAllowlist returnTo paths
Session ID not rotated at loginRegenerate on login and privilege change
Long-lived access tokens with no revocationShort lifetimes + refresh rotation
User enumeration via login or reset messagesUniform responses and timing
Secrets for confidential clients committed or shipped in appsSecret manager; public clients use PKCE without secrets

Safety

  • Never paste real user credentials, tokens or client secrets into code, logs, tests or chat. Use test tenants and test users.
  • Changing IdP configuration, rotating production signing keys or revoking sessions in production affects real users: describe the change and get explicit confirmation first.

Done when

  • The flow matches the table for the client type, with PKCE, state, nonce and exact redirect URIs.
  • Every token is validated (signature, algorithm, iss, aud, exp, scopes) before use.
  • No tokens or secrets are readable by browser JavaScript unless the no-backend SPA case was chosen deliberately.
  • Every endpoint has a server-side authorization check at object level, with denial tests.
  • Logout destroys the session and revokes refresh tokens.

References

  • references/oauth-oidc-checklist.md — read when implementing or reviewing an OAuth 2.0 / OIDC integration step by step.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.