Authentication

Integrate OAuth 2.0 and OpenID Connect in PHP

Use an established authorization library to connect PHP applications to identity providers without implementing protocol details yourself.

OAuth 2.0 and OpenID Connect are different

OAuth 2.0 is an authorization framework for delegated access. OpenID Connect (OIDC) adds an identity layer and standard ID token. If the goal is user sign-in, use OIDC rather than treating an arbitrary OAuth access token as proof of identity.

Use Authorization Code with PKCE

For browser-based sign-in, use the Authorization Code flow with PKCE where supported. Generate and validate a one-time state value to protect the authorization response against request forgery. OIDC also uses a nonce value to bind the ID token to the login request.

Validate the ID token

Use a maintained library and the provider's discovery metadata and signing keys. Validate signature, issuer, audience, expiry and nonce. Do not merely decode the JWT payload and trust its claims. Handle key rotation using the provider's published JWKS.

Protect client credentials

Keep confidential client secrets outside the public document root and out of source control. Use HTTPS for redirect URIs. Register exact redirect URIs rather than broad wildcard patterns. Do not put access tokens in URLs or logs.

Map identities carefully

Use the provider's stable subject identifier together with the issuer as the external identity key. Email addresses can change and may not be unique across providers. Decide how account linking works and require a verified, authenticated process before linking identities.

Checklist

  • Use OIDC for authentication.
  • Prefer Authorization Code with PKCE.
  • Validate state, nonce and every required ID-token claim.
  • Use a maintained protocol library.
  • Keep secrets private and redirect URIs exact.
  • Key external identities by issuer and subject.
  • Define logout, token revocation and account-linking behavior.

References

OpenID Connect Core · OAuth 2.0 Security Best Current Practice

Share this page Help other developers discover this resource.