# OAuth 2.0 and OIDC

API7 Gateway supports modern, token-based authentication through OAuth 2.0 and OpenID Connect (OIDC). By using the `openid-connect` plugin, you can delegate user authentication to a centralized Identity Provider (IdP) and enforce fine-grained access control on your APIs.

## Capabilities[​](#capabilities "Direct link to Capabilities")

* **Seamless Integration**: Connect with any OIDC-compliant IdP (Okta, Keycloak, Auth0, Azure AD).
* **Token Validation**: Automatically validate JWT signatures, expiration, and issuer.
* **Refresh Token Handling**: Securely handle token refresh flows at the gateway level.
* **Scoped Access Control**: Restrict API access based on OAuth 2.0 scopes or OIDC claims.

## Configure OIDC Authentication[​](#configure-oidc-authentication "Direct link to Configure OIDC Authentication")

To secure a route with OIDC, you need to configure the `openid-connect` plugin with your IdP's details.

Before you begin, prepare a [token from the Dashboard](https://docs.api7.ai/api7-gateway/reference/obtain-dashboard-token.md).

* Admin API
* ADC

```
# Create the service for the upstream you want to protect

curl -k "https://localhost:7443/apisix/admin/services/oidc-protected-service?gateway_group_id={gateway_group_id}" -X PUT \

  -H "X-API-KEY: ${API_KEY}" \

  -H "Content-Type: application/json" \

  -d '{

    "name": "oidc-protected-service",

    "upstream": {

      "type": "roundrobin",

      "scheme": "https",

      "nodes": [

        { "host": "your-upstream.example.com", "port": 443, "weight": 100 }

      ]

    }

  }'



# Create the route under that service with the openid-connect plugin

curl -k "https://localhost:7443/apisix/admin/routes/oidc-protected-route?gateway_group_id={gateway_group_id}" -X PUT \

  -H "X-API-KEY: ${API_KEY}" \

  -H "Content-Type: application/json" \

  -d '{

    "name": "oidc-protected-route",

    "service_id": "oidc-protected-service",

    "paths": ["/secure-api/*"],

    "plugins": {

      "openid-connect": {

        "client_id": "YOUR_CLIENT_ID",

        "client_secret": "YOUR_CLIENT_SECRET",

        "discovery": "https://{IDP_DOMAIN}/.well-known/openid-configuration",

        "scope": "openid profile email",

        "bearer_only": true

      }

    }

  }'
```

If you are not running locally, replace `localhost` with your Admin API host. The `-k` flag is required if the Admin API uses a self-signed TLS certificate. Replace `{gateway_group_id}` with the gateway group ID from the **Gateway Groups** page in the Dashboard (use `default` for the gateway group created by the quickstart).

adc.yaml

```
services:

  - name: oidc-protected-service

    upstream:

      scheme: https

      nodes:

        - host: your-upstream.example.com

          port: 443

          weight: 100

    routes:

      - name: oidc-protected-route

        uris:

          - /secure-api/*

        plugins:

          openid-connect:

            client_id: YOUR_CLIENT_ID

            client_secret: YOUR_CLIENT_SECRET

            discovery: "https://{IDP_DOMAIN}/.well-known/openid-configuration"

            scope: "openid profile email"

            bearer_only: true

    labels:

      docs-example: oidc-protected-service
```

Preview the changes owned by this example and confirm that the diff contains no unintended updates or deletions:

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=oidc-protected-service
```

Synchronize the reviewed configuration:

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=oidc-protected-service
```

## Token Validation and Claims[​](#token-validation-and-claims "Direct link to Token Validation and Claims")

Once authenticated, the gateway can:

1. Validate the token's signature using the IdP's public keys (automatically fetched via the discovery endpoint).
2. Check for token expiration (`exp`) and issuer (`iss`).
3. Inject user info or claims into request headers for upstream consumption.

## Next Steps[​](#next-steps "Direct link to Next Steps")

* [Configure SSO for the Dashboard](https://docs.api7.ai/api7-gateway/security-and-compliance/sso-dashboard.md) to centralize internal user access.
* [Implement RBAC](https://docs.api7.ai/api7-gateway/security-and-compliance/role-based-access-control.md) based on OIDC claims.
