icanid/icanid-sdk-php

ICAN ID PHP SDK。

0.0.16 2022-12-29 07:17 UTC

This package is auto-updated.

Last update: 2024-09-29 06:08:48 UTC


README

Build Status Total Downloads Latest Stable Version PHP Support Code Coverage License FOSSA

Auth0 允许您快速集成应用程序的认证和授权,以便您可以专注于核心业务。(了解更多

我们的 PHP SDK 提供了一个直观且经过严格测试的接口,通过 PHP 的现代版本访问 Auth0 的认证和管理 API 端点。

这是我们提供的许多库之一,支持众多平台。(更多库

要求

安装

推荐通过 Composer 安装 SDK

$ composer require auth0/auth0-php

有关设置 Composer 和其他安装方法的指南,请参阅我们的 文档

入门指南

要开始,您需要创建一个 免费 Auth0 账户 并注册一个 应用程序

认证 API

首先实例化 SDK 并传递来自应用程序设置页面的相关详情

use Auth0\SDK\Auth0;

$auth0 = new Auth0([
  // The values below are found on the Application settings tab.
  'domain'        => '{YOUR_TENANT}.auth0.com',
  'client_id'     => '{YOUR_APPLICATION_CLIENT_ID}',
  'client_secret' => '{YOUR_APPLICATION_CLIENT_SECRET}',

  // This is your application URL that will be used to process the login.
  // Save this URL in the "Allowed Callback URLs" field on the Application settings tab
  'redirect_uri' => 'https://{YOUR_APPLICATION_CALLBACK_URL}',
]);

注意: 在生产应用程序中,您不应将这些值硬编码。考虑使用环境变量来存储和传递这些值到您的应用程序,如我们的 文档 中建议的那样。

使用 SDK,向 Auth0 的端点发送请求非常简单。例如,使用 Auth0 的 通用登录 并检索用户详情只需几行代码即可完成

// Do we have an authenticated session available?
if ($user = $auth0->getUser()) {
  // Output the authenticated user
  print_r($user);
  exit;
}

// No session was available, so redirect to Universal Login page
$auth0->login();

有关如何使用认证 API 客户端的更多示例,请参阅我们的 文档网站

管理 API

此 SDK 还提供 Auth0 管理 API 的接口,要访问它,需要指定相应的受众并颁发一个特定于您租户管理 API 的访问令牌。

获取此类访问令牌的过程在我们的 文档 中描述。

use Auth0\SDK\API\Management;

$mgmt_api = new Management('{YOUR_ACCESS_TOKEN}', 'https://{YOUR_TENANT}.auth0.com');

SDK 为管理 API 的端点提供了便捷的接口。例如,要搜索用户

$results = $mgmt_api->users()->getAll([
  'q' => 'josh'
]);

if (! empty($results)) {
  echo '<h2>User Search</h2>';

  foreach ($results as $datum) {
    printf(
      '<p><strong>%s</strong> &lt;%s&gt; - %s</p>',
      !empty($datum['nickname']) ? $datum['nickname'] : 'No nickname',
      !empty($datum['email']) ? $datum['email'] : 'No email',
      $datum['user_id']
    );
  }
}

目前,要查看哪些端点被覆盖,请阅读 \Auth0\SDK\API\Management 类,此处提供

示例

组织

组织 是一组功能,为构建和维护 SaaS 和企业对企业(B2B)应用程序的开发人员提供更好的支持。

使用组织,您可以

  • 代表团队、商业客户、合作伙伴公司或任何应该以不同方式访问您的应用程序的用户逻辑分组,作为组织。
  • 以各种方式管理他们的会员资格,包括用户邀请。
  • 为每个组织配置品牌化的联合登录流程。
  • 实现基于角色的访问控制,以便用户在登录不同组织时可以拥有不同的角色。
  • 使用组织API将管理功能集成到您的产品中,这样那些企业就可以管理自己的组织。

请注意,组织功能目前仅适用于我们企业版和初创企业订阅计划上的客户。

使用组织登录

使用您的组织ID配置身份验证API客户端

use Auth0\SDK\Auth0;

$auth0 = new Auth0([
  // Found in your Auth0 dashboard, under Organization settings:
  'organization' => '{YOUR_ORGANIZATION_ID}',

  // Found in your Auth0 dashboard, under Application settings:
  'domain'       => '{YOUR_TENANT}.auth0.com',
  'client_id'    => '{YOUR_APPLICATION_CLIENT_ID}',
  'redirect_uri' => 'https://{YOUR_APPLICATION_CALLBACK_URL}',
]);

使用配置的组织重定向到通用登录页面

$auth0->login();

接受用户邀请

Auth0组织允许用户通过电子邮件链接被邀请,这将使用户返回到您的应用程序。用户将到达的URL基于您配置的应用程序登录URI,您可以在Auth0仪表板中的应用程序设置中更改它。

当用户使用邀请链接到达您的应用程序时,您可以期望提供三个查询参数:invitationorganizationorganization_name。这些参数将通过GET请求始终提供。

提供了一个辅助函数来处理提取这些查询参数并自动重定向到通用登录页面

// Expects the Auth0 SDK to be configured first, as demonstrated above.
$auth0->handleInvitation();

如果您希望对这一过程有更多的控制,提供了一个用于提取查询参数的单独辅助函数getInvitationParameters(),您可以使用它自行启动通用登录重定向

// Expects the Auth0 SDK to be configured first, as demonstrated above.

// Returns an object containing the invitation query parameters, or null if they aren't present
if ($invite = $auth0->getInvitationParameters()) {
  // Does the invite organization match your intended organization?
  if ($invite->organization !== '{YOUR_ORGANIZATION_ID}') {
    throw new Exception("This invitation isn't intended for this service. Please have your administrator check the service configuration and request a new invitation.");
  }

  // Redirect to Universal Login using the emailed invitation
  $auth0->login(null, null, [
    'invitation'   => $invite->invitation,
    'organization' => $invite->organization
  ]);
}

通过通用登录页面成功认证后,用户将使用您配置的redirect_uri返回到您的应用程序,其令牌将被自动验证,用户将拥有一个认证会话。使用getUser()检索有关认证用户的详细信息。

验证指导

在上面的示例中,我们的应用程序正在使用单个、配置好的组织。通过使用organization选项初始化SDK,我们告诉内部ID令牌验证器(IdTokenVerifier)验证org_id声明是否存在,并且它与我们所提供的相匹配。

您的应用程序可能不知道组织ID,或者可能需要支持多个组织。

您的应用程序应自行验证org_id声明,以确保接收到的值是预期并由您的应用程序所知的。

这可以通过读取getUser()方法返回的“org_id”值来实现。一个例子可能如下所示

use Auth0\SDK\Auth0;

// Example: a list of organizations our app supports
$allowedOrganizations = ['org_123', 'org_456'];
$defaultOrganization = $allowedOrganizations[0];

// For this scenario, do not pass any `organization` during SDK initialization. You'll handle the organization validation yourself.
$auth0 = new Auth0([
  // Found in your Auth0 dashboard, under Application settings:
  'domain'       => '{YOUR_TENANT}.auth0.com',
  'client_id'    => '{YOUR_APPLICATION_CLIENT_ID}',
  'redirect_uri' => 'https://{YOUR_APPLICATION_CALLBACK_URL}',
]);

// Are they authenticated?
if ($user = $auth0->getUser()) {
  // Do they have an organization claim?
  if (! isset($user['org_id'])) {
    // They do not; stop processing their request.
    throw new Exception('Please sign in using an organization.');
  }

  // Does the claim match an expected organization?
  if (! in_array($user['org_id'], $allowedOrganizations)) {
    // It does not; stop processing their request.
    throw new Exception('Access denied.');
  }
}

// Do we have an incoming invitation?
if ($invite = $auth0->getInvitationParameters()) {
  // Is the invite for an expected organization?
  if (! in_array($invite->organization, $allowedOrganizations)) {
    throw new Exception("This invitation isn't intended for this service. Please have your administrator check the service configuration and request a new invitation.");
  }

  // Redirect to Universal Login using the invitation
  $auth0->login(null, null, [
    'invitation'   => $invite->invitation,
    'organization' => $invite->organization
  ]);
}

// Redirect to Universal Login using our default organization
$auth0->login(null, null, [
  'organization' => $defaultOrganization
]);

如果声明无法验证,则您的应用程序应拒绝令牌为无效。有关更多信息,请参阅https://auth0.com/docs/organizations/using-tokens

文档

贡献

我们感谢您对项目的反馈和贡献!在您开始之前,请查看以下内容

支持 + 反馈

  • Auth0社区是一个宝贵的资源,可以提问并找到答案,由Auth0团队和一群热情的开发者组成
  • 对于代码级别的支持(如功能请求和错误报告),我们鼓励您在我们的repo上打开问题
  • 对于付费计划上的客户,我们的支持中心提供有知识丰富的支持专家可供开启工单

关于我们的支持方案,更多详细信息可在我们的网站上找到。点击此处访问。

漏洞报告

请勿在公共GitHub问题跟踪器上报告安全漏洞。有关披露安全问题的负责任披露计划详细说明了披露程序。

什么是 Auth0?

Auth0可以帮助您

为什么选择Auth0?

许可证

Auth0 PHP SDK是开源软件,采用MIT许可证。有关更多信息,请参阅LICENSE文件。

FOSSA Status