Langsung ke konten

Mulai

SDK PHP

identvia/identvia-php 1.1.1 masukkan pengguna dengan OpenID Connect. PHP 8.1+ polos, tanpa framework, cocok shared hosting.

Salin prompt untuk agen AI Anda

Memakai agen coding? Prompt ini mengarahkannya ke dokumentasi ini dan mencantumkan aturan yang wajib diikutinya. Tinjau kode yang ditulisnya seperti kode lainnya.

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.

Persyaratan

  • PHP 8.1+ dengan openssl dan json (standar di mana-mana).
  • Opsional: curl (jika tidak, stream PHP) dan apcu (jika tidak, cache kunci memakai berkas).
  • Satu dependensi: firebase/php-jwt untuk pemeriksaan tanda tangan. SDK tidak mengimplementasikan kriptografi sendiri.

Pasang

Terminal
composer require identvia/identvia-php

Atau unggah zip dengan File Manager cPanel dan sertakan vendor/autoload.php.

Konfigurasi

PHP
$identvia = new Identvia\Sdk\Identvia([
    'issuer'        => 'https://id.identvia.com',          // wajib
    'client_id'     => 'oid_…',             // wajib
    'client_secret' => 'ivs_…',             // hanya app rahasia
    'redirect_uri'  => 'https://example.com/callback.php', // harus cocok persis
    'scopes'        => ['email', 'profile'], // openid selalu ditambahkan
    'cache_dir'     => __DIR__ . '/../storage/identvia', // opsional; bawaan: direktori temp sistem
    'leeway'        => 60,                  // penyimpangan jam yang diizinkan, detik
    'timeout'       => 10,                  // Timeout HTTP, detik
]);
OpsiCatatan
token_authclient_secret_basic (bawaan) atau client_secret_post
ca_bundleJalur ke berkas CA jika toko sertifikat host usang. Verifikasi TLS tidak bisa dimatikan.
cache, http, state_storeBawa cache, klien HTTP, atau penyimpanan state sendiri (antarmuka di src/).

Jauhkan rahasia dari repositori: variabel lingkungan, atau config.php di luar web root.

Masuk

PHP
// login.php: mengirim pengguna ke Identvia (state, nonce, dan PKCE ditangani untuk Anda)
$identvia->redirectToSignIn([
    'return_to' => '/account',   // opsional; hanya jalur same-site diterima
    // 'prompt'  => 'login',      // paksa masuk baru
    // 'max_age' => 300,          // wajibkan masuk dalam 5 menit terakhir
]);

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

Error

ExceptionKapanApa yang harus dilakukan
AuthorizationExceptionPengguna menekan Batal (userCancelled()) atau Identvia mengembalikan kesalahanTampilkan pesan ramah
InvalidStateExceptionCallback kedaluwarsa, dipakai ulang, atau palsu; issuer salahMulai masuk lagi
TokenExceptionPertukaran kode gagal (invalid_grant, invalid_client)Periksa rahasia Anda; mulai lagi
ValidationExceptionID token gagal pemeriksaanJangan masukkan pengguna; catat di log
HttpExceptionKegagalan jaringan atau TLSCoba lagi nanti

Semuanya memperluas Identvia\Sdk\Exception\IdentviaException.

Userinfo, refresh, revoke, logout

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

// Butuh scope offline_access. Identvia merotasi refresh token: selalu simpan yang baru.
$tokens = $identvia->refresh($tokens->refreshToken, $signIn->sub());

$identvia->revoke($tokens->refreshToken);   // saat keluar

// Opsional: juga akhiri sesi Identvia
header('Location: ' . $identvia->logoutUrl($tokens->idToken, 'https://example.com/'));

Apa yang diperiksa SDK

  • state: acak, sekali pakai, kedaluwarsa setelah 10 menit; beberapa tab bisa masuk bersamaan.
  • PKCE (S256) pada setiap permintaan, termasuk app rahasia.
  • iss pada callback (RFC 9207), menjaga dari kebingungan penyedia identitas.
  • Tanda tangan ID token dengan kunci publik Identvia (hanya RS256/ES256, bukan none atau HMAC); kunci di-cache sejam dan diambil lagi sekali saat rotasi.
  • iss, aud, azp, exp, iat, nonce, auth_time terhadap max_age, dan at_hash.
  • HTTPS dengan verifikasi sertifikat setiap panggilan; http:// hanya untuk localhost.

Sesi

SDK menyimpan masuk tertunda di sesi PHP dan memulai jika perlu. Cookie sesi harus terkirim saat Identvia mengalihkan kembali, jadi pakai SameSite=Lax (bukan Strict). Panggil session_regenerate_id(true) setelah masuk berhasil.

App contoh: Identvia Notes

App lengkap dengan CSP ketat, tombol, masuk/keluar, dan catatan per pengguna. Ada di zip di examples/notes-app.

Terminal
cd identvia-php/examples/notes-app
cp config.example.php config.php    # tempel client ID dan rahasia Anda
php -S localhost:9999 -t public     # lalu buka http://localhost:9999