Skip to content

OAuth2Client

Viames Marino edited this page Nov 6, 2026 · 3 revisions

Pair framework: OAuth2Client

Pair\Models\OAuth2Client manages OAuth2 client credentials and token issuance.

Main methods

  • createAccessToken(): string — fresh token, database-clock timestamps, no renewal or deletion of other tokens.
  • accessTokenResponse(?string $scope = null): JsonResponse — explicit RFC 6749 response.
  • issueAccessToken(): string — historical reuse/refresh policy, retained for compatibility.
  • sendAccessTokenResponse(): void
  • generateSecret(int $bytes = 32): string

Lifecycle/config:

  • _init() defines fields and validation rules

Implementation example

use Pair\Models\OAuth2Client;

$client = OAuth2Client::find($clientId);

if (!$client or !$client->enabled) {
	throw new RuntimeException('Invalid client');
}

$token = $client->createAccessToken();

Notes

  • Designed for server-to-server OAuth flows.
  • The client identifier is the id primary key; availability is stored in enabled.
  • This example assumes the caller has already authenticated the client secret. Finding an enabled client by ID alone does not authenticate it.
  • issueAccessToken() reuses and refreshes a valid token or creates one, then removes other tokens for that client.
  • sendAccessTokenResponse() delegates to the new explicit response and terminates. It omits absent scope and emits mandatory no-store/no-cache headers.
  • sendLegacyAccessTokenResponse() preserves the old payload and token reuse behavior only for frozen integrations; it is not the standards-compliant emitter.
  • Validate the client secret and authorize the requested scope before issuing. Scope persistence and authorization remain application responsibilities.
  • accessTokenResponse() validates scope syntax before persisting any token.

See also: OAuth2Token, ApiController.

Clone this wiki locally