Skip to content

Get started

PHP SDK

identvia/identvia-php 1.1.1 signs users in with OpenID Connect. Plain PHP 8.1+, no framework, works on shared hosting.

Copy prompt for your AI agent

Using a coding agent? This prompt points it at these docs and lists the rules it must follow. Review what it writes like any other code.

You are helping me add "Sign in with Identvia" to my app (PHP). Identvia is an OpenID Connect provider.

Read these first (they are plain Markdown and are the source of truth; prefer them over anything you remember):
- https://id.identvia.com/developers/docs/quickstart.md
- https://id.identvia.com/developers/docs/oidc.md
- https://id.identvia.com/developers/docs/security.md
- Discovery: https://id.identvia.com/.well-known/openid-configuration

Stack: plain PHP 8.1+ (no framework). Use the official SDK identvia/identvia-php (composer require identvia/identvia-php, or the zip with vendor/ from https://id.identvia.com/developers/docs/php for hosts without Composer). Create identvia.php (config), login.php (redirectToSignIn), callback.php (handleCallback, then session_regenerate_id(true) and store sub) and logout.php. Read secrets from environment variables or a config file outside the web root.

Requirements:
1. Authorization code flow with PKCE (S256 only), a random state and a nonce per sign-in. No implicit flow.
2. Validate the ID token: RS256 signature from the JWKS, iss = https://id.identvia.com, aud contains my client_id, exp/iat, and the nonce.
3. Identify users by sub (it is pairwise: different for every app). Treat email as optional, because the person may choose not to share it.
4. Redirect URIs must match exactly. Keep the client secret and refresh tokens server-side; never log tokens.
5. Ask only for the scopes I need (openid, plus email/profile if used). Handle the user canceling (error=access_denied) gracefully.

Optional, only if I ask for AI-agent access later:
- AgentPass (agents acting for a user with single-use, holder-bound passes): https://id.identvia.com/developers/docs/agentpass.md
- MCP server for agent clients: https://id.identvia.com/developers/docs/mcp.md
- Token exchange, rich authorization requests and CIBA: https://id.identvia.com/developers/docs/delegation.md

Before writing code, tell me which files you will add or change. Don't invent endpoints or options that aren't in the docs; if something is unclear, ask me.

Requirements

  • PHP 8.1 or newer with openssl and json (standard everywhere).
  • Optional: curl (otherwise PHP streams are used) and apcu (otherwise the key cache uses files).
  • One dependency: firebase/php-jwt for signature checks. The SDK never implements cryptography itself.

Install

Terminal
composer require identvia/identvia-php

Or upload the zip with cPanel’s File Manager and include vendor/autoload.php.

Configuration

PHP
$identvia = new Identvia\Sdk\Identvia([
    'issuer'        => 'https://id.identvia.com',          // required
    'client_id'     => 'oid_…',             // required
    'client_secret' => 'ivs_…',             // confidential apps only
    'redirect_uri'  => 'https://example.com/callback.php', // must match exactly
    'scopes'        => ['email', 'profile'], // openid is always added
    'cache_dir'     => __DIR__ . '/../storage/identvia', // optional; default: system temp dir
    'leeway'        => 60,                  // clock skew allowed, seconds
    'timeout'       => 10,                  // HTTP timeout, seconds
]);
OptionNotes
token_authclient_secret_basic (default) or client_secret_post
ca_bundlePath to a CA file if your host’s certificate store is outdated. TLS verification can’t be turned off.
cache, http, state_storeBring your own cache, HTTP client or state storage (interfaces in src/).

Keep the secret out of your code repository: use environment variables, or a config.php outside the web root.

Sign in

PHP
// login.php: sends the user to Identvia (state, nonce and PKCE are handled for you)
$identvia->redirectToSignIn([
    'return_to' => '/account',   // optional; only same-site paths are accepted
    // 'prompt'  => 'login',      // force a fresh sign-in
    // 'max_age' => 300,          // require a sign-in in the last 5 minutes
]);

// callback.php: completes it
$signIn = $identvia->handleCallback($_GET);
$signIn->sub();           // the user's ID for your app
$signIn->email();         // null if not shared
$signIn->emailVerified();
$signIn->name();
$signIn->amr();           // e.g. ['hwk', 'user'] for a passkey
$signIn->tokens;          // TokenSet: accessToken, refreshToken, idToken, expiresAt
$signIn->returnTo;

Errors

ExceptionWhenWhat to do
AuthorizationExceptionThe user pressed Cancel (userCancelled()) or Identvia returned an errorShow a friendly message
InvalidStateExceptionExpired, reused or forged callback; wrong issuerStart sign-in again
TokenExceptionCode exchange failed (invalid_grant, invalid_client)Check your secret; start again
ValidationExceptionThe ID token failed a checkNever sign the user in; log it
HttpExceptionNetwork or TLS failureRetry later

All of them extend Identvia\Sdk\Exception\IdentviaException.

Userinfo, refresh, revoke, logout

PHP
$claims = $identvia->userinfo($tokens->accessToken, $signIn->sub());

// Needs the offline_access scope. Identvia rotates refresh tokens: always store the new one.
$tokens = $identvia->refresh($tokens->refreshToken, $signIn->sub());

$identvia->revoke($tokens->refreshToken);   // on sign-out

// Optional: also end the Identvia session
header('Location: ' . $identvia->logoutUrl($tokens->idToken, 'https://example.com/'));

What the SDK checks

  • state: random, single use, expires after 10 minutes, several tabs can sign in at once.
  • PKCE (S256) on every request, also for confidential apps.
  • iss on the callback (RFC 9207), which defends against identity-provider mix-up.
  • ID token signature with Identvia’s published keys (RS256/ES256 only, never none or HMAC); keys are cached for an hour and re-fetched once when Identvia rotates them.
  • iss, aud, azp, exp, iat, nonce, auth_time against max_age, and at_hash.
  • HTTPS with certificate verification for every call; http:// is accepted only for localhost.

Sessions

The SDK keeps pending sign-ins in the PHP session and starts one if needed. The session cookie must be sent when Identvia redirects back, so use SameSite=Lax (not Strict). Call session_regenerate_id(true) after a successful sign-in.

Example app: Identvia Notes

A complete app with a strict CSP, the button, sign-in, sign-out and per-user notes. It’s in the zip under examples/notes-app.

Terminal
cd identvia-php/examples/notes-app
cp config.example.php config.php    # paste your client ID and secret
php -S localhost:9999 -t public     # then open http://localhost:9999