跳到正文

快速开始

快速入门

大约十分钟即可为 PHP 网站添加“使用 Identvia 登录”。你需要带 OpenSSL 的 PHP 8.1+,所有 cPanel 主机都具备。

复制给你的 AI 代理用的提示词

在用编程代理?这段提示词会把它指向这些文档,并列出它必须遵守的规则。像审查其他代码一样审查它写的代码。

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.

1. 注册你的应用

打开 开发者控制台,使用您的 Identvia 账户登录并按 新应用。选择 Web 应用 用于服务器端站点(保留客户端密钥),并将您的回调 URL 添加为重定向 URI,例如 http://localhost:8000/callback.php。对于浏览器 SPA 选择 单页面应用;对于 iOS、Android 或桌面选择 本地 & 桌面应用(私有使用方案、应用链接、回环)。

你会看到 client ID 和 client secret。请立即复制密钥:它只显示一次,Identvia 只保存它的哈希值。

你的第一个环境是 Development,允许 http://localhost。正式上线时,添加一个 Production 环境并使用真实的 https:// 回调。它有自己的 client ID 和密钥。

2. 安装 SDK

使用 Composer:

Terminal
composer require identvia/identvia-php

其他平台(JS、移动端、.NET、WordPress):见 SDK 与库。

没有 Composer 或 SSH?下载 zip 包(已包含 vendor/),在 cPanel 文件管理器中解压到网站旁边,然后 require 'identvia-php/vendor/autoload.php'。

3. 进行配置

identvia.phpPHP
<?php // identvia.php:在登录页和回调页中引入此文件
require __DIR__ . '/vendor/autoload.php';

session_start();

$identvia = new Identvia\Sdk\Identvia([
    'issuer'        => 'https://id.identvia.com',
    'client_id'     => getenv('IDENTVIA_CLIENT_ID'),
    'client_secret' => getenv('IDENTVIA_CLIENT_SECRET'),
    'redirect_uri'  => 'http://localhost:8000/callback.php',
    'scopes'        => ['email', 'profile'],
]);

4. 添加按钮

index.htmlHTML
<a class="identvia-btn" href="/login.php">Sign in with Identvia</a>
<script src="https://id.identvia.com/sdk/v1/button.js" async></script>
使用 Identvia 登录← 效果就是这样

5. 发起登录

login.phpPHP
<?php // login.php
require __DIR__ . '/identvia.php';

$identvia->redirectToSignIn(['return_to' => '/dashboard.php']);

6. 处理回调

callback.phpPHP
<?php // callback.php
require __DIR__ . '/identvia.php';

try {
    $signIn = $identvia->handleCallback($_GET);
} catch (Identvia\Sdk\Exception\AuthorizationException $e) {
    exit($e->userCancelled() ? 'You canceled sign-in.' : 'Sign-in failed.');
} catch (Identvia\Sdk\Exception\IdentviaException $e) {
    error_log($e->getMessage());
    exit('Sign-in failed. Please try again.');
}

session_regenerate_id(true);
$_SESSION['user_id'] = $signIn->sub();   // 稳定,你的应用独有
$_SESSION['email']   = $signIn->email(); // 仅当用户分享时

header('Location: ' . ($signIn->returnTo ?? '/'));

就这样。handleCallback() 会检查 state,使用 PKCE 交换授权码,用 Identvia 的密钥验证 ID 令牌签名,并检查签发方、受众、过期时间和 nonce。只要有任何不对,它就会抛出异常,不会让任何人登录。

使用 sub() 作为用户 ID,而不是邮箱地址。用户可以更改邮箱或选择不分享,而且 sub 在每个应用中都不同,这是有意为之。

后续步骤