İçeriğe geç

Başlarken

PHP SDK

identvia/identvia-php 1.1.1 kullanıcıları OpenID Connect ile oturum açtırır. Saf PHP 8.1+, çerçeve yok, paylaşımlı hostingde çalışır.

Yapay zekâ ajanınız için istemi kopyalayın

Kodlama ajanı mı kullanıyorsunuz? Bu istem onu bu belgelere yönlendirir ve uyması gereken kuralları listeler. Yazdıklarını diğer kodlar gibi inceleyin.

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.

Gereksinimler

  • PHP 8.1+ with openssl and json (her yerde standart).
  • İsteğe bağlı: curl (yoksa PHP akışları) ve apcu (yoksa anahtar önbelleği dosya kullanır).
  • Tek bağımlılık: imza denetimi için firebase/php-jwt. SDK kriptografiyi kendisi uygulamaz.

Kur

Terminal
composer require identvia/identvia-php

Veya zip’i cPanel Dosya Yöneticisi ile yükleyip vendor/autoload.php ekleyin.

Yapılandırma

PHP
$identvia = new Identvia\Sdk\Identvia([
    'issuer'        => 'https://id.identvia.com',          // zorunlu
    'client_id'     => 'oid_…',             // zorunlu
    'client_secret' => 'ivs_…',             // yalnızca gizli uygulamalar
    'redirect_uri'  => 'https://example.com/callback.php', // tam eşleşmeli
    'scopes'        => ['email', 'profile'], // openid her zaman eklenir
    'cache_dir'     => __DIR__ . '/../storage/identvia', // isteğe bağlı; varsayılan: sistem geçici dizini
    'leeway'        => 60,                  // izin verilen saat sapması, saniye
    'timeout'       => 10,                  // HTTP zaman aşımı, saniye
]);
SeçenekNotlar
token_authclient_secret_basic (varsayılan) veya client_secret_post
ca_bundleBarındırıcının sertifika deposu eskiyse bir CA dosyası yolu. TLS doğrulaması kapatılamaz.
cache, http, state_storeKendi önbelleğinizi, HTTP istemcinizi veya durum deponuzu getirin (src/ içindeki arayüzler).

Sırrı depodan uzak tutun: ortam değişkenleri veya web kökü dışındaki config.php.

Oturum aç

PHP
// login.php: kullanıcıyı Identvia’ya gönderir (state, nonce ve PKCE sizin için)
$identvia->redirectToSignIn([
    'return_to' => '/account',   // isteğe bağlı; yalnızca same-site yollar kabul edilir
    // 'prompt'  => 'login',      // taze oturum açmayı zorla
    // 'max_age' => 300,          // son 5 dakikada oturum açmayı gerektir
]);

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

Hatalar

İstisnaNe zamanNe yapılmalı
AuthorizationExceptionKullanıcı İptal’e bastı (userCancelled()) veya Identvia hata döndüDostça bir mesaj göster
InvalidStateExceptionSüresi dolmuş, yeniden kullanılmış veya sahte callback; yanlış issuerOturum açmayı yeniden başlat
TokenExceptionKod değişimi başarısız (invalid_grant, invalid_client)Sırrınızı kontrol edin; yeniden başlayın
ValidationExceptionID token bir denetimi geçemediKullanıcıyı asla oturum açtırmayın; günlüğe yazın
HttpExceptionAğ veya TLS hatasıSonra yeniden dene

Hepsi Identvia\Sdk\Exception\IdentviaException sınıfını genişletir.

Userinfo, refresh, revoke, logout

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

// offline_access kapsamı gerekir. Identvia yenileme jetonlarını döndürür: her zaman yenisini saklayın.
$tokens = $identvia->refresh($tokens->refreshToken, $signIn->sub());

$identvia->revoke($tokens->refreshToken);   // oturum kapatırken

// İsteğe bağlı: Identvia oturumunu da sonlandır
header('Location: ' . $identvia->logoutUrl($tokens->idToken, 'https://example.com/'));

SDK’nın denetledikleri

  • state: rastgele, tek kullanımlık, 10 dakika sonra biter; birden çok sekme aynı anda oturum açabilir.
  • PKCE (S256) her istekte, gizli uygulamalar dahil.
  • Callback’teki iss (RFC 9207), kimlik sağlayıcı karışmasına karşı korur.
  • Identvia’nın yayımladığı anahtarlarla ID token imzası (yalnızca RS256/ES256, asla none veya HMAC); anahtarlar bir saat önbellekte, döndürmede bir kez yeniden alınır.
  • iss, aud, azp, exp, iat, nonce, auth_time ile max_age, ve at_hash.
  • Her çağrıda sertifika doğrulamalı HTTPS; http:// yalnızca localhost için.

Oturumlar

SDK bekleyen oturum açmaları PHP oturumunda tutar ve gerekirse başlatır. Identvia geri yönlendirirken oturum çerezi gönderilmeli — SameSite=Lax ( Strict değil). Başarılı oturumdan sonra session_regenerate_id(true) çağırın.

Örnek uygulama: Identvia Notes

Sıkı CSP, düğme, oturum açma/kapama ve kullanıcı başına notlar içeren tam bir uygulama. Zip’te examples/notes-app.

Terminal
cd identvia-php/examples/notes-app
cp config.example.php config.php    # client ID ve sırrınızı yapıştırın
php -S localhost:9999 -t public     # sonra http://localhost:9999 açın