Zum Inhalt springen

Erste Schritte

PHP-SDK

identvia/identvia-php 1.1.1 meldet Nutzer mit OpenID Connect an. Reines PHP 8.1+, kein Framework, läuft auf Shared Hosting.

Prompt für Ihren KI-Agenten kopieren

Sie nutzen einen Coding-Agenten? Dieser Prompt verweist ihn auf diese Doku und listet die Regeln auf, die er befolgen muss. Prüfen Sie, was er schreibt, wie jeden anderen 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.

Anforderungen

  • PHP 8.1 oder neuer mit openssl und json (überall Standard).
  • Optional: curl (sonst PHP-Streams) und apcu (sonst Datei-Cache für Schlüssel).
  • Eine Abhängigkeit: firebase/php-jwt für Signaturprüfungen. Das SDK implementiert keine Kryptografie selbst.

Installieren

Terminal
composer require identvia/identvia-php

Oder Zip per cPanel-Dateimanager hochladen und vendor/autoload.php einbinden.

Konfiguration

PHP
$identvia = new Identvia\Sdk\Identvia([
    'issuer'        => 'https://id.identvia.com',          // erforderlich
    'client_id'     => 'oid_…',             // erforderlich
    'client_secret' => 'ivs_…',             // nur vertrauliche Apps
    'redirect_uri'  => 'https://example.com/callback.php', // muss exakt übereinstimmen
    'scopes'        => ['email', 'profile'], // openid wird immer hinzugefügt
    'cache_dir'     => __DIR__ . '/../storage/identvia', // optional; Standard: System-Temp-Verzeichnis
    'leeway'        => 60,                  // erlaubte Uhrabweichung, Sekunden
    'timeout'       => 10,                  // HTTP-Timeout, Sekunden
]);
OptionHinweise
token_authclient_secret_basic (Standard) oder client_secret_post
ca_bundlePfad zu einer CA-Datei, wenn der Zertifikatsspeicher des Hosts veraltet ist. TLS-Prüfung lässt sich nicht abschalten.
cache, http, state_storeEigener Cache, HTTP-Client oder State-Speicher (Interfaces in src/).

Geheimnis außerhalb des Repos halten: Umgebungsvariablen oder config.php außerhalb des Webroots.

Anmelden

PHP
// login.php: sendet den Nutzer zu Identvia (state, nonce und PKCE werden für Sie erledigt)
$identvia->redirectToSignIn([
    'return_to' => '/account',   // optional; nur same-site-Pfade akzeptiert
    // 'prompt'  => 'login',      // frische Anmeldung erzwingen
    // 'max_age' => 300,          // Anmeldung in den letzten 5 Minuten verlangen
]);

// callback.php: schließt ab
$signIn = $identvia->handleCallback($_GET);
$signIn->sub();           // the user's ID for your app
$signIn->email();         // null wenn nicht geteilt
$signIn->emailVerified();
$signIn->name();
$signIn->amr();           // e.g. ['hwk', 'user'] for a passkey
$signIn->tokens;          // TokenSet: accessToken, refreshToken, idToken, expiresAt
$signIn->returnTo;

Fehler

ExceptionWannWas tun
AuthorizationExceptionNutzer hat Abbrechen gedrückt (userCancelled()) oder Identvia lieferte einen FehlerFreundliche Meldung zeigen
InvalidStateExceptionAbgelaufener, wiederverwendeter oder gefälschter Callback; falscher IssuerAnmeldung neu starten
TokenExceptionCode-Austausch fehlgeschlagen (invalid_grant, invalid_client)Geheimnis prüfen; neu starten
ValidationExceptionID-Token hat eine Prüfung nicht bestandenNutzer nie anmelden; protokollieren
HttpExceptionNetz- oder TLS-FehlerSpäter erneut versuchen

Alle erweitern Identvia\Sdk\Exception\IdentviaException.

Userinfo, refresh, revoke, logout

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

// Braucht den Scope offline_access. Identvia rotiert Refresh-Tokens: immer das neue speichern.
$tokens = $identvia->refresh($tokens->refreshToken, $signIn->sub());

$identvia->revoke($tokens->refreshToken);   // beim Abmelden

// Optional: auch Identvia-Sitzung beenden
header('Location: ' . $identvia->logoutUrl($tokens->idToken, 'https://example.com/'));

Was das SDK prüft

  • state: zufällig, einmalig, läuft nach 10 Minuten ab; mehrere Tabs können gleichzeitig anmelden.
  • PKCE (S256) bei jeder Anfrage, auch für vertrauliche Apps.
  • iss im Callback (RFC 9207), schützt vor Identity-Provider-Mix-up.
  • ID-Token-Signatur mit Identvias veröffentlichten Schlüsseln (nur RS256/ES256, nie none oder HMAC); Schlüssel eine Stunde gecacht und einmal neu geholt bei Rotation.
  • iss, aud, azp, exp, iat, nonce, auth_time gegen max_age, und at_hash.
  • HTTPS mit Zertifikatsprüfung bei jedem Aufruf; http:// nur für localhost.

Sitzungen

Das SDK hält laufende Anmeldungen in der PHP-Session und startet eine bei Bedarf. Das Session-Cookie muss beim Zurückleiten von Identvia gesendet werden, also SameSite=Lax (nicht Strict). Nach erfolgreicher Anmeldung session_regenerate_id(true) aufrufen.

Beispiel-App: Identvia Notes

Eine komplette App mit strikter CSP, Button, An-/Abmeldung und Notizen pro Nutzer. Im Zip unter examples/notes-app.

Terminal
cd identvia-php/examples/notes-app
cp config.example.php config.php    # Client-ID und Geheimnis einfügen
php -S localhost:9999 -t public     # dann http://localhost:9999 öffnen