taproot / indieauth
PHP PSR-7兼容的IndieAuth服务器和客户端实现。
Requires
- php: >= 7.3.0
- barnabywalters/mf-cleaner: ^0.1.4
- dflydev/fig-cookies: ^3.0
- guzzlehttp/psr7: ^1.8||^2.0
- indieauth/client: ^1.1
- mf2/mf2: ^0.4.6||^0.5
- psr/http-message: ^1.0
- psr/http-server-middleware: ^1.0
- psr/log: ^1.1||^2.0||^3.0
Requires (Dev)
- guzzlehttp/guzzle: ^7.3
- phpunit/phpunit: ^9.5
- vimeo/psalm: ^4.7
README
taproot/indieauth是一个PSR-7兼容的IndieAuth服务器库。它允许您快速轻松地将现有网站转换为IndieAuth身份提供者,使您可以使用自己的域名登录网站,并向外部应用程序(例如,允许外部应用程序通过micropub在您的网站上发布)授予细粒度的网站访问权限。它提供了合理的默认值,但可以进行广泛的定制。
快速链接
- IndieAuth生活标准
- API文档
- 代码覆盖率
- 支持聊天室(ping
barnaby
或向那里的其他友好人士提问)
安装
taproot/indieauth目前测试过并与PHP 7.3、7.4、8.0和8.1兼容。
使用 composer 安装taproot/indieauth
composer.phar require taproot/indieauth
composer.phar install (or composer.phar update)
版本发布已使用GPG签名,因此您可以验证代码未被篡改。
gpg --recv-keys 1C00430B19C6B426922FE534BEF8CE58118AD524
cd vendor/taproot/indieauth
git tag -v v0.3.1 # Replace with the version you have installed
用法
典型的最小用法如下
// Somewhere in your app set-up code: $server = new Taproot\IndieAuth\Server([ // Your server’s issuer ID URL (see __construct() docs for more details) 'issuer' => 'https://example.com/', // A secret key, >= 64 characters long. 'secret' => YOUR_APP_INDIEAUTH_SECRET, // A path to store token data, or an object implementing TokenStorageInterface. 'tokenStorage' => '/../data/auth_tokens/', // An authentication callback function, which either returns data about the current user, // or redirects to/implements an authentication flow. 'authenticationHandler' => function (ServerRequestInterface $request, string $authenticationRedirect, ?string $normalizedMeUrl) { // If the request is authenticated, return an array with a `me` key containing the // canonical URL of the currently logged-in user. if ($userUrl = getLoggedInUserUrl($request)) { return ['me' => $userUrl]; } // Otherwise, redirect the user to a login page, ensuring that they will be redirected // back to the IndieAuth flow with query parameters intact once logged in. return new Response('302', ['Location' => 'https://example.com/login?next=' . urlencode($authenticationRedirect)]); } ]); // In your authorization endpoint route, which must not be CSRF-protected: return $server->handleAuthorizationEndpointRequest($request); // In your token endpoint route, which must not be CSRF-protected: return $server->handleTokenEndpointRequest($request); // In another route (e.g. a micropub route), to authenticate the request: // (assuming $bearerToken is a token parsed from an “Authorization: Bearer XXXXXX” header // or access_token property from a request body) if ($accessToken = $server->getAccessToken($bearerToken)) { // Request is authenticated as $accessToken['me'], and is allowed to // act according to the scopes listed in $accessToken['scope']. $scopes = explode(' ', $accessToken['scope']); }
IndieAuth客户端需要一些发现元数据来发现相关的URL和配置细节。目前,提供此发现超出了taproot/indieauth的范围(我们可能会考虑在将来部分自动化生成indieauth-metadata端点),因此请参阅规范中的发现部分以获取更多信息。
请参阅__construct
文档以获取更多配置选项,以及文档以获取更多关于它们的处理方法的信息,特别是
- Taproot\IndieAuth\Server::__construct()以获取有关如何配置您的
Server
实例的详细信息。 - Taproot\IndieAuth\Server::handleAuthorizationEndpointRequest()以了解授权请求期间确切发生的情况(这是该库的主要用途)
- Taproot\IndieAuth\Callback\DefaultAuthorizationForm(及其相关模板)以获取有关自定义默认同意屏幕表单的详细信息。
- Taproot\IndieAuth\Callback\SingleUserPasswordAuthenticationCallback以获取实现身份验证回调的示例,以及有关自定义模板的相关模板信息。
- Taproot\IndieAuth\Storage\TokenStorageInterface以获取有关实现自己的令牌存储的详细信息。
- Taproot\IndieAuth\Callback\AuthorizationFormInterface以获取有关实现自己的授权表单的信息。
示例应用程序
请参阅taproot/micropub示例应用程序,了解如何使用taproot/indieauth的工作示例。
贡献
如果您对使用这个库有任何疑问,请加入 indieweb 开发聊天室,并联系 barnaby
或询问那里的其他友好人士。
如果您发现库中存在错误或问题,或者想要建议一个新功能,请 创建一个问题。
如果讨论导致您想要提交一个 pull request,虽然这不是必需的,但按照此流程操作将增加其快速被接受的机会
- 将此存储库 fork 到您的 github 账户,并将其克隆到您的开发计算机上。
- 运行
./run_coverage.sh
并确保所有测试通过——您需要 XDebug 以获取代码覆盖率数据。 - 如果适用,编写失败的回归测试,例如修复错误时的测试。
- 进行您的更改。
- 再次运行
./run_coverage.sh
和open docs/coverage/index.html
。确保您所做的更改都由测试覆盖。从 0.1.0 版本开始,taproot/indieauth 几乎有 100% 的测试覆盖率,这个数字不应该下降! - 运行
./vendor/bin/psalm
并修复它带来的任何警告。 - 安装并运行
./phpDocumentor.phar
以重新生成文档(如果适用)。 - 推送您的更改并提交 PR。
变更日志
v0.3.1
2022-10-23
- 修正了 Cache-Control 头部,向用户响应添加了 CSP 和 X-Frame-Options 头部 (#21)
- 移除了对 nyholm/psr7 和 webmozart/path-util 的硬依赖 (#20)
- 允许与 mf2/mf2 ^0.5 一起安装,并添加了处理照片 img+alt 解析的代码
v0.3.0
2022-10-21
破坏性变更
- 类中的一些公共成员现在是受保护的,并且只能在实例化时进行配置
issuer
键现在是半必需的,在服务器配置数组中省略它将导致警告
其他更改
- 以前接受自定义模板路径的所有地方现在还支持以下签名的可调用函数 (#18)
function (array $context): string
- 客户端 ID 网页现在除了 h-app 外,还搜索匹配的 h-x-app 微格式 (#17)
- 如果客户端 ID h-(x-)app 上存在有效的作者属性,DefaultAuthorizationForm 和相应的模板将使其可用并显示它 (#16)
- 改进了文档,包括内部链接,更好的格式化
- 允许 DoubleSubmitCookieCsrfMiddleware 的 cookie 路径设置为任意值(对于内部 IndieAuth 使用没有用,但在其他地方重用该代码时很有用)
- DoubleSubmitCookieCsrfMiddleware 为 $request 添加了一个预渲染的 CSRF 表单元素属性,以便方便使用
v0.2.2
2022-10-03
- 除了 v1.1 外,现在还允许使用 psr/log v2 和 v3 进行安装
v0.2.1
2022-09-24
添加了一个迁移脚本,用于将 FilesystemJsonStorage 令牌从 v0.1 格式更新到 v0.2 格式。使用以下命令运行它
php vendor/taproot/indieauth/bin/migrate.php ../path/to/your/json/token/storage/
在验证和获取之前对客户端_id 和 redirect_uri 进行标准化,但为了比较目的,存储并使用原始字符串(修复了 #12)
v0.2.0
2022-09-06
- 允许支持带有 response_type=id 的旧客户端 (#3)
- 将 FilesystemJsonStorage 内部结构更改得更好,以匹配 OAuth 中使用的术语 (#5)
- 允许使用 guzzle v2 (#7)
- 改进了身份验证回调处理逻辑 (#8)
- 允许在纯文本代码挑战中使用 . 和 ~ (#13)
- 如果无法获取 client_id,不再进行硬失败 (#14)
- 改进了所有默认模板的样式
- 小修,重新生成文档
v0.1.0
2021-06-24
- 初始发布