descope/descope-php

PHP 的 Descope SDK

0.3.1 2024-09-30 13:46 UTC

README

License

概述

PHP 的 Descope SDK 提供了对 Descope 身份验证的便捷访问。您可以在 Descope 网站 上了解更多信息。

入门

要求

安装

使用 Composer 安装此包

composer require descope/descope-php

您需要在根目录中设置一个 .env 文件,其中包含您的 Descope 项目 ID,您可以从 控制台 获取,如下所示

DESCOPE_PROJECT_ID=<Descope Project ID>
DESCOPE_MANAGEMENT_KEY=<Descope Management Key>

使用 SDK

为了使用 SDK,您需要使用您的 .env 文件中定义的 Descope 项目 ID 初始化一个 DescopeSDK 对象,如下所示

require 'vendor/autoload.php';
use Descope\SDK\DescopeSDK;

$descopeSDK = new DescopeSDK([
    'projectId' => $_ENV['DESCOPE_PROJECT_ID'],
    'managementKey' => $_ENV['DESCOPE_MANAGEMENT_KEY'] // Optional, only used for Management functions
]);

此 SDK 将通过以下内置函数轻松处理 Descope JWT 令牌

密码认证

注册

$response = $descopeSDK->auth->password->signUp("loginId", "password123");
print_r($response);

登录

$response = $descopeSDK->auth->password->signIn("loginId", "password123");
print_r($response);

发送重置密码

$response = $descopeSDK->auth->password->sendReset("loginId", "https://example.com/reset");
print_r($response);

更新密码

$descopeSDK->auth->password->update("loginId", "newPassword123", "refreshToken");

替换密码

$response = $descopeSDK->auth->password->replace("loginId", "oldPassword123", "newPassword123");
print_r($response);

获取密码策略

$response = $descopeSDK->auth->password->getPolicy();
print_r($response);

SSO 认证

SSO 登录

$response = $descopeSDK->auth->sso->signIn(
    "tenant",
    "https://example.com/callback",
    "prompt",
    true,
    true,
    ["custom" => "claim"],
    "ssoAppId"
);
print_r($response);

交换令牌

$response = $descopeSDK->auth->sso->exchangeToken("code");
print_r($response);

会话管理

  1. DescopeSDK->verify($sessionToken) - 将验证 JWT 签名并返回 TRUEFALSE,具体取决于 JWT 是否有效和过期
  2. DescopeSDK->getClaims($sessionToken) - 将以数组格式返回 JWT 中的所有声明
  3. DescopeSDK->getUserDetails($refreshToken) - 将使用提供的刷新令牌返回所有用户信息(电子邮件、电话、验证状态等)

注意:要使用 verify()getClaims(),您需要将您的会话令牌传递给函数参数。要使用 getUserDetails(),您需要传递您的刷新令牌。

用户管理函数

创建用户

$response = $descopeSDK->management->user->create(
    "testuser1",
    "user@example.com",
    "1234567890",
    "Test User",
    "Test",
    "Middle",
    "User"
);
print_r($response);

更新用户

$descopeSDK->management->user->update(
    "testuser1",
    "newemail@example.com",
    "0987654321",
    "Updated User",
    "Updated",
    "Middle",
    "User"
);

删除用户

$descopeSDK->management->user->delete("testuser1");

添加租户

$response = $descopeSDK->management->user->addTenant("testuser1", "tenantId1");
print_r($response);

移除租户

$response = $descopeSDK->management->user->removeTenant("testuser1", "tenantId1");
print_r($response);

设置租户角色

$response = $descopeSDK->management->user->setTenantRoles("testuser1", "tenantId1", ["admin"]);
print_r($response);

添加租户角色

$response = $descopeSDK->management->user->addTenantRoles("testuser1", "tenantId1", ["user"]);
print_r($response);

移除租户角色

$response = $descopeSDK->management->user->removeTenantRoles("testuser1", "tenantId1", ["admin"]);
print_r($response);

设置临时密码

$descopeSDK->management->user->setTemporaryPassword("testuser1", new UserPassword(cleartext: "temporaryPassword123"));

设置活动密码

$descopeSDK->management->user->setActivePassword("testuser1", new UserPassword(cleartext: "activePassword123"));

设置密码

$descopeSDK->management->user->setPassword("testuser1", new UserPassword(cleartext: "password123"), true);

单元测试

PHP 目录包含使用 PHPUnit 的单元测试。您可以在 src/tests/DescopeSDKTest.php 文件中插入会话令牌和刷新令牌的值,并运行以验证函数是否正常工作。

要运行测试,请运行此命令

./vendor/bin/phpunit --bootstrap bootstrap.php --verbose src/tests/DescopeSDKTest.php

运行 PHP 示例应用

sample/static/descope.js 中,将 projectId 替换为您的 Descope 项目 ID,您可以在 Descope 控制台 中找到。

如果您尚未运行上述 composer 命令,请确保安装必要的 SDK 包。

然后,从根目录运行此命令以启动示例应用

php -S localhost:3000 -t sample/

现在您可以从您的网络浏览器访问 https://:3000/

此示例应用展示了使用 WebJS SDK 和 PHP 会话保留用户信息跨多个页面。它还展示了如何初始化 SDK 并使用它验证来自 login.php 发送的 formData 中的会话令牌。

其他代码示例

  1. WordPress 插件

反馈

贡献

我们感谢对此次存储库的反馈和贡献!

提出问题

要提供反馈或报告错误,请在我们的问题跟踪器上提出一个问题

本项目遵循MIT许可协议。有关更多信息,请参阅许可文件