Sign inSign up

lucasglmt/oauth_mock

By lucasglmt

•Updated 9 months ago

OAUTH MOCK is a small application that allows you to simulate a connection to an OAuth2 service.

Image
Integration & delivery
API management
Developer tools
0

572

lucasglmt/oauth_mock repository overview

⁠OAUTH MOCK

OAUTH MOCK is a small application written in Go that allows you to simulate an OAuth2/OpenID Connect service, mainly for testing and development purposes.

⁠Disclaimer

This project is intended for development and testing purposes only.

It is NOT designed to be used in production and should not be deployed in a production environment.

⁠Features

  • OAuth2 Authorization Code Flow
  • PKCE support (S256 and plain)
  • OpenID Connect (OIDC) support
  • ID Token (JWT signed with RS256)
  • Refresh Token support
  • OIDC Discovery endpoint
  • JWKS endpoint for token validation
  • Userinfo endpoint
  • Configurable clients via YAML

⁠Prerequisites

  • Go 1.23+
  • (optional) Docker

⁠Getting Started

⁠Build from source
go run cmd/api/main.go

The API will start on the port defined in the .env file (default: 8080).

⁠Using Docker
docker build -t oauth_mock .
docker run -p 8080:8080 oauth_mock

⁠Configuration

⁠Environment Variables (.env)
VariableDescriptionDefault
PORTServer port8080
APP_ENVEnvironment (development/production)development
ISSUEROIDC Issuer URLhttp://localhost:{PORT}
⁠Clients (clients.yaml)
clients:
  - id: my-client-id
    name: My App
    redirect_uris:
      - http://localhost:3000/callback
    allowed_scopes:
      - openid
      - profile
      - email
    users:
      - email: [email protected]
        password: secret
⁠OAuth Behavior (config.yaml)
authorize:
  mandatory_state: true  # Require state parameter

⁠API Endpoints

⁠OIDC Discovery
MethodEndpointDescription
GET/.well-known/openid-configurationOIDC Discovery document
GET/.well-known/jwks.jsonJSON Web Key Set
⁠OAuth2 Flow
MethodEndpointDescription
GET/api/v1/authorizeStart authorization (login page)
POST/api/v1/authorize/loginSubmit credentials
POST/api/v1/authorize/consentGrant consent
POST/api/v1/tokenExchange code/refresh token for tokens
GET/api/v1/userinfoGet user information
⁠Other
MethodEndpointDescription
GET/api/v1/pingHealth check
GET/swagger/index.htmlSwagger UI

⁠Usage Example

⁠1. Start Authorization Flow
GET http://localhost:8080/api/v1/authorize
    ?response_type=code
    &client_id=1
    &redirect_uri=http://localhost:3000/callback
    &scope=openid%20email%20profile
    &state=random-state
⁠2. Exchange Code for Tokens
curl -X POST http://localhost:8080/api/v1/token \
  -d "grant_type=authorization_code" \
  -d "code=mock-20260204..." \
  -d "client_id=1" \
  -d "redirect_uri=http://localhost:3000/callback"

Response:

{
  "access_token": "access-20260204...",
  "token_type": "Bearer",
  "expires_in": 3600,
  "refresh_token": "refresh-20260204...",
  "id_token": "eyJhbGciOiJSUzI1NiIs...",
  "scope": "openid email profile"
}
⁠3. Refresh Token
curl -X POST http://localhost:8080/api/v1/token \
  -d "grant_type=refresh_token" \
  -d "refresh_token=refresh-20260204..." \
  -d "client_id=1"
⁠4. Get User Info
curl -H "Authorization: Bearer access-20260204..." \
  http://localhost:8080/api/v1/userinfo

Response:

{
  "sub": "[email protected]",
  "email": "[email protected]",
  "name": "user"
}

⁠ID Token

When the openid scope is requested, the token response includes a signed JWT (id_token) with the following claims:

ClaimDescription
issIssuer URL
subSubject (user email)
audAudience (client ID)
expExpiration time
iatIssued at
emailUser email
nameUser name

The JWT is signed with RS256. Public keys are available at /.well-known/jwks.json.

⁠PKCE (Proof Key for Code Exchange)

PKCE is supported for public clients (SPAs, mobile apps). It prevents authorization code interception attacks.

⁠Methods Supported
MethodDescription
S256SHA-256 hash (recommended)
plainPlain text (not recommended)
⁠Usage with PKCE
  1. Generate a code_verifier (43-128 characters, URL-safe)
  2. Create code_challenge = BASE64URL(SHA256(code_verifier))
  3. Include in authorization request:
GET /api/v1/authorize
    ?response_type=code
    &client_id=1
    &redirect_uri=http://localhost:3000/callback
    &scope=openid
    &code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM
    &code_challenge_method=S256
  1. Include code_verifier in token request:
curl -X POST http://localhost:8080/api/v1/token \
  -d "grant_type=authorization_code" \
  -d "code=mock-..." \
  -d "client_id=1" \
  -d "redirect_uri=http://localhost:3000/callback" \
  -d "code_verifier=dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"

⁠Development

⁠Run Tests
go test ./...
⁠Regenerate Swagger Docs
swag init -g cmd/api/main.go -o docs

⁠License

MIT

Tag summary

Content type

Image

Digest

sha256:979b908b4…

Size

8.9 MB

Last updated

9 months ago

docker pull lucasglmt/oauth_mock