Search by

mpge / laravel-openid-provider

mpge

Turn a Laravel application into a small, spec-compliant OpenID Connect provider built on Laravel Passport.

0.1.0 2026-11-19 16:48 UTC

This package is auto-updated.

Last update: 2026-11-19 19:54:09 UTC


README

tests License: MIT

Turn a Laravel application into an OpenID Connect provider, built on Laravel Passport.

The package does a deliberately small part of OpenID Connect and does it to the spec: the authorization code flow, ID tokens, UserInfo, discovery, JWKS with key rotation, and token revocation. It is checked against the OpenID Foundation conformance suite instead of asking you to trust it. See docs/conformance.md.

  • PHP 8.3+, Laravel 12 and 13, Passport 13, league/oauth2-server 9
  • No hand-written cryptography: signing and encryption go through league/oauth2-server, Passport and lcobucci/jwt
  • Fails closed: anything that cannot be validated is refused

What is supported

Feature Status
Authorization code flow (response_type=code) Supported
PKCE S256 only. Required for public clients, optional for confidential clients (configurable)
Implicit and hybrid flows Refused with unsupported_response_type
Grants at the token endpoint authorization_code, refresh_token, client_credentials
Client authentication client_secret_basic, client_secret_post
ID tokens RS256, with iss, sub, aud, exp, iat, auth_time, nonce
Refresh tokens Rotated on every use; reuse revokes the whole chain
Authorization code reuse Refused, and every token issued from the code is revoked
UserInfo (GET and POST) Claims of the granted scopes only, sub always present
Discovery and JWKS /.well-known/openid-configuration, /.well-known/jwks.json
Signing key rotation Two published keys at once, openid-provider:rotate-keys
prompt=none, prompt=login, prompt=consent, max_age Supported, with a re-authentication contract
Token revocation (RFC 7009) Supported
Token introspection (RFC 7662) Opt-in, OPENID_PROVIDER_INTROSPECTION=true
RP-initiated logout Opt-in, OPENID_PROVIDER_LOGOUT=true
Dynamic client registration, request objects, encrypted ID tokens, pairwise subjects, front and back-channel logout Not supported

Installation

composer require mpge/laravel-openid-provider
php artisan vendor:publish --tag=passport-migrations
php artisan migrate
php artisan passport:keys

Schedule passport:purge and openid-provider:prune to keep the token tables small (see installation).

The service provider is auto-discovered. It registers the package migrations (two small tables that track which tokens came from which authorization) and the routes below. Set the issuer, which must be the public https URL of the application:

OPENID_PROVIDER_ISSUER=https://id.example.com

Make the user model implement the subject contract. The default subject is the primary key as a string; it must never change for a user.

use Laravel\Passport\Contracts\OAuthenticatable;
use Laravel\Passport\HasApiTokens;
use Mpge\OpenIdProvider\Concerns\HasOidcSubject;
use Mpge\OpenIdProvider\Contracts\OidcSubject;

class User extends Authenticatable implements OAuthenticatable, OidcSubject
{
    use HasApiTokens, HasOidcSubject;
}

Full steps, including the api guard and the login route, are in docs/installation.md.

Endpoints

Path Route name
GET, POST /oauth/authorize passport.authorizations.authorize, passport.authorizations.approve
DELETE /oauth/authorize passport.authorizations.deny
POST /oauth/token passport.token
GET, POST /oauth/userinfo oidc.userinfo
POST /oauth/revoke oidc.revoke
POST /oauth/introspect (when enabled) oidc.introspect
GET, POST /oauth/logout, POST /oauth/logout/confirm (when enabled) oidc.logout, oidc.logout.confirm
GET /.well-known/openid-configuration oidc.discovery
GET /.well-known/jwks.json oidc.jwks

Configure a client

Clients are ordinary Passport clients. Create a confidential client for a server-side application:

php artisan passport:client --name="Moodle" --redirect_uri="https://moodle.example.com/admin/oauth2callback.php"

Give the client its ID, its secret and your issuer URL. Most clients only need the issuer: they read everything else from discovery. Redirect URIs are compared exactly, character for character. Public clients (no secret) must use PKCE with S256. See docs/clients-and-pkce.md, and docs/moodle.md for a complete Moodle setup.

Add a custom scope

Scopes map to claims in config/openid-provider.php. A claim's resolver is an attribute name, a class implementing ClaimResolver, or a [Class::class, 'method'] pair:

'scopes' => [
    // ...
    'acme_access' => [
        'description' => 'Your Acme roles.',
        'claims' => [
            'acme_roles' => App\OpenId\AcmeRoles::class,
            'acme_employee_id' => 'employee_number',
        ],
    ],
],
namespace App\OpenId;

use Illuminate\Contracts\Auth\Authenticatable;
use Mpge\OpenIdProvider\Contracts\ClaimResolver;

class AcmeRoles implements ClaimResolver
{
    public function resolve(Authenticatable $user, string $claim): array
    {
        return $user->roles()->pluck('name')->all();
    }
}

The scope is registered with Passport and listed in discovery automatically. UserInfo returns acme_roles only to clients that were granted acme_access. See docs/scopes-and-claims.md.

Rotate signing keys

php artisan openid-provider:rotate-keys --prepare   # publish the next key
# wait for clients to refresh their JWKS cache (keys.jwks_max_age)
php artisan openid-provider:rotate-keys --promote   # sign with it; the old key stays published
# wait for ID tokens signed by the old key to expire (id_token_ttl)
php artisan openid-provider:rotate-keys --retire    # stop publishing the old key

Run the command without options to see the current keys. See docs/key-rotation.md.

Documentation

Contributing and security

See CONTRIBUTING.md. Please report vulnerabilities privately as described in SECURITY.md, not in public issues.

License

MIT. See LICENSE.md.