Skip to content

OidcClient

Wraps league/oauth2-client’s generic provider (via SpaceDelimitedScopeProvider, which fixes the library’s comma-delimited scope parameter) for the OIDC Authorization Code flow.

PKCE (S256) is hardcoded, not an app-configurable option, since OAuth 2.1 mandates it for the Authorization Code grant. The nonce authorization-request parameter and its later ID-token verification are entirely our own responsibility — league/oauth2-client is OAuth2-only and has no OIDC/nonce concept.

final class OidcClient

Since1.0.0
SourceOidcClient.php

public function __construct(string $clientId, string $clientSecret, string $redirectUri, string $authorizationEndpoint, string $tokenEndpoint, array<int, string> $scopes = ['openid'], ?ClientInterface $httpClient = null, RandomnessInterface $randomness = new SystemRandomness(…)): mixed

The source of entropy for the OIDC nonce.

ParameterTypeDescription
$clientIdstringThe OAuth client id.
$clientSecretstringThe OAuth client secret.
$redirectUristringThis app’s callback URL, registered with the authorization server.
$authorizationEndpointstringThe authorization server’s /authorize endpoint.
$tokenEndpointstringThe authorization server’s /token endpoint.
$scopesarray``<``int``, ``string``>The scopes to request.
$httpClient?``ClientInterfaceA Guzzle HTTP client override (e.g. for testing); defaults to a real Guzzle client.
$randomnessRandomnessInterfaceThe source of entropy for the OIDC nonce.

Returns mixed

MethodDescription
buildAuthorizationRequest(): OidcAuthorizationRequestGenerates state/PKCE-verifier/nonce and builds the authorization redirect URL.
exchangeCode(string $code, string $pkceVerifier): AccessTokenInterfaceExchanges an authorization code for tokens, using the PKCE verifier persisted from the matching OidcClient::buildAuthorizationRequest() call.
fromDiscovery(OidcDiscoveryDocument $document, string $clientId, string $clientSecret, string $redirectUri, array<int, string> $scopes = ['openid'], ?ClientInterface $httpClient = null, RandomnessInterface $randomness = new SystemRandomness(…)): selfBuilds a client from a provider’s discovery document (see OidcDiscoveryClient) instead of hand-copied endpoint URLs.

public function buildAuthorizationRequest(): OidcAuthorizationRequest

Generates state/PKCE-verifier/nonce and builds the authorization redirect URL.

The caller persists the returned state (e.g. via OidcStateStorage::store()) before redirecting the browser.

Returns OidcAuthorizationRequest — The redirect URL plus the state to persist.

public function exchangeCode(string $code, string $pkceVerifier): AccessTokenInterface

Exchanges an authorization code for tokens, using the PKCE verifier persisted from the matching OidcClient::buildAuthorizationRequest() call.

The PKCE code_verifier from the matching OidcAuthorizationState.

ParameterTypeDescription
$codestringThe authorization code received on the callback.
$pkceVerifierstringThe PKCE code_verifier from the matching OidcAuthorizationState.

Returns AccessTokenInterface — The token response, including the ID token (see getValues()['id_token']).

ThrowsWhen
AuthenticationExceptionIf the token endpoint rejects the exchange.

public static function fromDiscovery(OidcDiscoveryDocument $document, string $clientId, string $clientSecret, string $redirectUri, array<int, string> $scopes = ['openid'], ?ClientInterface $httpClient = null, RandomnessInterface $randomness = new SystemRandomness(…)): self

Builds a client from a provider’s discovery document (see OidcDiscoveryClient) instead of hand-copied endpoint URLs.

The source of entropy for the OIDC nonce.

ParameterTypeDescription
$documentOidcDiscoveryDocumentThe provider’s metadata.
$clientIdstringThe OAuth client id.
$clientSecretstringThe OAuth client secret.
$redirectUristringThis app’s callback URL, registered with the authorization server.
$scopesarray``<``int``, ``string``>The scopes to request.
$httpClient?``ClientInterfaceA Guzzle HTTP client override (e.g. for testing); defaults to a real Guzzle client.
$randomnessRandomnessInterfaceThe source of entropy for the OIDC nonce.

Returns self — A client wired to the discovered authorization and token endpoints.

ThrowsWhen
AuthenticationExceptionIf the document lacks an authorization or token endpoint, or rules out PKCE S256.