Bắt đầu
PHP SDK
identvia/identvia-php 1.1.1 đăng nhập người dùng bằng OpenID Connect. PHP 8.1+ thuần, không framework, chạy trên hosting chia sẻ.
Sao chép prompt cho tác nhân AI của bạn
Đang dùng tác nhân lập trình? Prompt này hướng nó tới tài liệu này và liệt kê các quy tắc nó phải tuân theo. Hãy xem lại những gì nó viết như mọi đoạn code khác.
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.
You are helping me add "Sign in with Identvia" to my app (Laravel). 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: Laravel. Use the official SDK identvia/identvia-php (framework-agnostic) from a small service class bound in a service provider, not a hand-written OAuth client. Add GET /auth/identvia/redirect and GET /auth/identvia/callback routes in the web middleware group. After the callback, find-or-create the user by the Identvia sub (not by email), call Auth::login() and $request->session()->regenerate(). Put the client secret in .env and config/services.php.
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.
You are helping me add "Sign in with Identvia" to my app (Symfony). 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: Symfony. Use the official SDK identvia/identvia-php wrapped in a service, plus a custom authenticator (AbstractAuthenticator) on the callback route. Map users by the Identvia sub, not email. Keep the client secret in Symfony secrets or env vars, never in committed config.
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.
You are helping me add "Sign in with Identvia" to my app (Node.js). 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: Node.js. There is no Identvia JavaScript SDK; use a certified OpenID Connect relying-party library (for example openid-client) with discovery from the issuer. Use the authorization code flow with PKCE (S256), state and nonce, keep tokens server-side in the session store, and regenerate the session id after sign-in.
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.
You are helping me add "Sign in with Identvia" to my app (Python). 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: Python. There is no Identvia Python SDK; use a maintained OpenID Connect client library (for example Authlib) with discovery from the issuer. Use the authorization code flow with PKCE (S256), state and nonce, and keep tokens server-side.
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.
Yêu cầu
- PHP 8.1+ với openssl và json (chuẩn mọi nơi).
- Tùy chọn: curl (không thì PHP streams) và apcu (không thì đệm khóa dùng tệp).
- Một phụ thuộc: firebase/php-jwt để kiểm chữ ký. SDK không tự triển khai mật mã.
Cài đặt
composer require identvia/identvia-phpHoặc tải zip bằng Trình quản lý tệp cPanel và include vendor/autoload.php.
Cấu hình
$identvia = new Identvia\Sdk\Identvia([
'issuer' => 'https://id.identvia.com', // bắt buộc
'client_id' => 'oid_…', // bắt buộc
'client_secret' => 'ivs_…', // chỉ ứng dụng bí mật
'redirect_uri' => 'https://example.com/callback.php', // phải khớp chính xác
'scopes' => ['email', 'profile'], // openid luôn được thêm
'cache_dir' => __DIR__ . '/../storage/identvia', // tùy chọn; mặc định: thư mục tạm hệ thống
'leeway' => 60, // độ lệch đồng hồ cho phép, giây
'timeout' => 10, // Timeout HTTP, giây
]);| Tùy chọn | Ghi chú |
|---|---|
| token_auth | client_secret_basic (mặc định) hoặc client_secret_post |
| ca_bundle | Đường dẫn tệp CA nếu kho chứng chỉ máy chủ cũ. Không tắt được xác minh TLS. |
| cache, http, state_store | Tự mang bộ nhớ đệm, HTTP client hoặc kho state (giao diện trong src/). |
Giữ bí mật ngoài kho mã: biến môi trường, hoặc config.php ngoài web root.
Đăng nhập
// login.php: gửi người dùng tới Identvia (state, nonce và PKCE được xử lý giúp bạn)
$identvia->redirectToSignIn([
'return_to' => '/account', // tùy chọn; chỉ chấp nhận đường dẫn same-site
// 'prompt' => 'login', // bắt buộc đăng nhập mới
// 'max_age' => 300, // yêu cầu đăng nhập trong 5 phút gần nhất
]);
// callback.php: hoàn tất
$signIn = $identvia->handleCallback($_GET);
$signIn->sub(); // the user's ID for your app
$signIn->email(); // null nếu không chia sẻ
$signIn->emailVerified();
$signIn->name();
$signIn->amr(); // e.g. ['hwk', 'user'] for a passkey
$signIn->tokens; // TokenSet: accessToken, refreshToken, idToken, expiresAt
$signIn->returnTo;Lỗi
| Ngoại lệ | Thời điểm | Cần làm gì |
|---|---|---|
| AuthorizationException | Người dùng bấm Hủy (userCancelled()) hoặc Identvia trả lỗi | Hiện thông báo thân thiện |
| InvalidStateException | Callback hết hạn, tái sử dụng hoặc giả mạo; issuer sai | Bắt đầu đăng nhập lại |
| TokenException | Đổi mã thất bại (invalid_grant, invalid_client) | Kiểm tra bí mật; bắt đầu lại |
| ValidationException | ID token không qua kiểm tra | Không bao giờ đăng nhập người dùng; ghi nhật ký |
| HttpException | Lỗi mạng hoặc TLS | Thử lại sau |
Tất cả đều mở rộng Identvia\Sdk\Exception\IdentviaException.
Userinfo, refresh, revoke, logout
$claims = $identvia->userinfo($tokens->accessToken, $signIn->sub());
// Cần phạm vi offline_access. Identvia xoay refresh token: luôn lưu cái mới.
$tokens = $identvia->refresh($tokens->refreshToken, $signIn->sub());
$identvia->revoke($tokens->refreshToken); // khi đăng xuất
// Tùy chọn: cũng kết thúc phiên Identvia
header('Location: ' . $identvia->logoutUrl($tokens->idToken, 'https://example.com/'));SDK kiểm tra gì
- state: ngẫu nhiên, một lần, hết hạn sau 10 phút; nhiều tab có thể đăng nhập cùng lúc.
- PKCE (S256) trên mọi yêu cầu, kể cả ứng dụng bí mật.
- iss trên callback (RFC 9207), chống nhầm nhà cung cấp danh tính.
- Chữ ký ID token bằng khóa công bố của Identvia (chỉ RS256/ES256, không none hay HMAC); khóa đệm một giờ và lấy lại một lần khi xoay.
- iss, aud, azp, exp, iat, nonce, auth_time so với max_age, và at_hash.
- HTTPS với xác minh chứng chỉ mỗi lần gọi; http:// chỉ cho localhost.
Phiên
SDK giữ đăng nhập đang chờ trong phiên PHP và khởi động nếu cần. Cookie phiên phải gửi khi Identvia chuyển hướng về — dùng SameSite=Lax (không Strict). Sau đăng nhập thành công gọi session_regenerate_id(true).
Ứng dụng mẫu: Identvia Notes
Ứng dụng đầy đủ với CSP nghiêm, nút, đăng nhập/xuất và ghi chú theo người dùng. Trong zip tại examples/notes-app.
cd identvia-php/examples/notes-app
cp config.example.php config.php # dán client ID và bí mật
php -S localhost:9999 -t public # sau đó mở http://localhost:9999