# API Auth Engineer

> Implement API authentication: OAuth2 flows with Keycloak, token exchange, client registration, and end-to-end token verification. Use when working with oauth2 implementation or when the user mentions oauth2 implementation.

- Skill: `opknock/api-auth-engineer` (Agent Skill)
- Install (CLI): `npx get-skill add opknock/api-auth-engineer`
- Raw SKILL.md: https://api.skillmd.com/api/skills/opknock/api-auth-engineer/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Integrations & APIs
- License: MIT
- Author: OpKnock (https://skillmd.com/u/opknock)
- Updated: 2026-11-23
- Page: https://skillmd.com/skills/opknock/api-auth-engineer

---


Implement API authentication: OAuth2 flows with Keycloak, token exchange, client registration, and end-to-end token verification.

## Agentic Workflow: Read -> Reason -> Act (api-auth-engineer)

You are **api-auth-engineer** (security) — a sub-agent that **Reads, Reasons, and Acts** via `allowed-tools`.

### 1. Read — security context for `api-auth-engineer`
- Domain: Implement API authentication: OAuth2 flows with Keycloak, token exchange, client registration, and end-to-end token verification.
- **oauth2-implementation**: Set up and verify OAuth2 flows against Keycloak — `docker run -d -p 8080:8080 -e KEYCLOAK_ADMIN=admin -e KEYCLOAK_ADMIN_PASSWORD=ad`
- Check `knowledge` and `prerequisites: node.js, python, jsonwebtoken, passport`

### 2. Reason — think for `api-auth-engineer`
- For `oauth2-implementation`: Set up and verify OAuth2 flows against Keycloak — decide which checks to run
- Evaluate trust/policy: `kdesk trust` + `kdesk doctor` patterns for your inputs

### 3. Act — execute with `api-auth-engineer` tools
- Tools: `Glob`, `Grep`, `Read`, `Bash` (see frontmatter `tools`/`allowed-tools`)
- Use `safe_path` for any write; record evidence (paths, checksums)
- Fingerprint: `api-auth-engineer:16b30556`

# API Auth Engineer

## What this skill does
Implement and verify API authentication using OAuth2 with Keycloak as the identity provider. Covers realm setup, the password and client-credentials grants, and verifying tokens at the API.

## When to use
- Adding OAuth2 to an API
- Standing up a local identity provider
- Testing grant flows and token verification

## Real commands
```bash
# Start Keycloak (dev mode)
docker run -d -p 8080:8080 \
  -e KEYCLOAK_ADMIN=admin -e KEYCLOAK_ADMIN_PASSWORD=admin \
  quay.io/keycloak/keycloak:latest start-dev

# Discovery endpoints
curl -s 'http://localhost:8080/realms/demo/.well-known/openid-configuration' | jq '.token_endpoint, .authorization_endpoint'

# Password grant
curl -s -X POST 'http://localhost:8080/realms/demo/protocol/openid-connect/token' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d 'grant_type=password&client_id=api&username=alice&password=secret' | jq -r '.access_token'

# Client credentials grant
curl -s -X POST 'http://localhost:8080/realms/demo/protocol/openid-connect/token' \
  -d 'grant_type=client_credentials&client_id=orders-api&client_secret=abc123' | jq -r '.access_token'

# Refresh token
curl -s -X POST 'http://localhost:8080/realms/demo/protocol/openid-connect/token' \
  -d 'grant_type=refresh_token&refresh_token=$REFRESH&client_id=api&client_secret=abc123' | jq -r '.access_token'

# Revoke
curl -s -X POST 'http://localhost:8080/realms/demo/protocol/openid-connect/revoke' -d 'token=$TOKEN&client_id=api'

# Verify at the API
curl -s http://localhost:8080/api/verify -H 'Authorization: Bearer $TOKEN' | jq '.subject'
```

## Flow selection
- password: first-party apps with user creds (no longer recommended)
- client_credentials: server-to-server
- authorization_code + PKCE: SPAs and mobile
- refresh_token: long-lived sessions

## Best practices
- Never use the password grant for third parties
- Use PKCE for public clients
- Validate signatures, issuer, audience, and expiry
- Rotate client secrets and refresh tokens

## Testing
```bash
TOKEN=$(curl -s -X POST 'http://localhost:8080/realms/demo/protocol/openid-connect/token' -d 'grant_type=password&client_id=api&username=alice&password=secret' | jq -r '.access_token')
curl -s http://localhost:8080/api/verify -H "Authorization: Bearer $TOKEN" | jq
```

## Capabilities

### oauth2-implementation
Set up and verify OAuth2 flows against Keycloak

**Parameters:**
- `realm` (string): Keycloak realm name
- `client_id` (string): OAuth2 client ID
- `grant_type` (string): password, client_credentials, authorization_code, or refresh_token

**Commands:**
- `docker run -d -p 8080:8080 -e KEYCLOAK_ADMIN=admin -e KEYCLOAK_ADMIN_PASSWORD=admin quay.io/keycloak/keycloak:latest start-dev`
- `curl -s -X POST 'http://localhost:8080/realms/demo/protocol/openid-connect/token' -H 'Content-Type: application/x-www-form-urlencoded' -d 'grant_type=password&client_id=api&username=alice&password=secret' | jq -r '.access_token'`
- `curl -s 'http://localhost:8080/realms/demo/.well-known/openid-configuration' | jq '.token_endpoint, .authorization_endpoint'`
- `curl -s -X POST 'http://localhost:8080/admin/realms/demo/clients' -H 'Authorization: Bearer $ADMIN_TOKEN' -H 'Content-Type: application/json' -d '{"clientId":"orders-api","publicClient":false,"serviceAccountsEnabled":true}'`
- `curl -s http://localhost:8080/api/verify -H 'Authorization: Bearer $TOKEN' | jq '.subject'`

**Examples:**
- curl -s -X POST 'http://localhost:8080/realms/demo/protocol/openid-connect/token' -d 'grant_type=client_credentials&client_id=orders-api&client_secret=abc123' | jq -r '.access_token'
- curl -s -X POST 'http://localhost:8080/realms/demo/protocol/openid-connect/revoke' -d 'token=$REFRESH&client_id=api&client_secret=abc123'
- curl -s -H 'Authorization: Bearer $TOKEN' http://localhost:8080/api/protected | jq '.role'

## References
- [OAuth 2.0 Framework (RFC 6749)](https://datatracker.ietf.org/doc/html/rfc6749)
- [Keycloak Docs](https://www.keycloak.org/documentation)

