Authing 文档文档
快速开始
概念
使用指南
开发集成 arrow
  • V2 文档
  • V3 文档
元数据
应用集成
身份自动化
加入 APN
开发集成
多租户(内测版)
控制台文档
多租户控制台
租户控制台
Saas 应用 Demo
快速开始
概念
使用指南
开发集成 arrow
  • V2 文档
  • V3 文档
元数据
应用集成
身份自动化
加入 APN
开发集成
多租户(内测版)
控制台文档
多租户控制台
租户控制台
Saas 应用 Demo
旧版
  • 单页 Web 应用

  • 移动、客户端应用

  • 标准 Web 应用

    • C#

    • Go

    • Java

    • Node.js

    • PHP

      • 安装使用
      • 用户认证模块
      • 管理模块

    • Python

  • 框架集成

  • 其他

  1. 开发集成
  2. /
  3. 标准 Web 应用
  4. /
  5. PHP
  6. /
  7. 用户认证模块

¶ 认证模块

此模块是基于 OIDC 标准协议进行认证的,支持获取认证地址、认证、获取令牌、检查令牌、登出等方法。本模块只支持在服务端调用。

require "vendor/autoload.php";

use Authing\AuthenticationClient;

$authentication = new AuthenticationClient(options);//初始化

$authentication->buildAuthUrl(); //生成用户登录链接
$authentication->getLoginStateByAuthCode(); //用授权码获取用户登录态
$authentication->getUserInfo(); //通过 Access Token 获取用户身份信息
$authentication->refreshLoginState(); //通过 Refresh Token 刷新用户的登录态,延长过期时间
$authentication->buildLogoutUrl(); //生成登出 URL
$authentication->loginWithRedirect(); //将用户浏览器重定向到 Authing 的认证发起 URL 进行认证
$authentication->handleRedirectCallback(); //应用回调端点处理认证返回结果
$authentication->logoutWithRedirect(); //将浏览器重定向到 Authing 的登出发起 URL 进行登出

¶ 初始化

require "vendor/autoload.php";

use Authing\AuthenticationClient;

// 使用 appId、appSecret、host、redirectUri 进行初始化
$authentication = new AuthenticationClient(options);

¶ 参数

  • appId <String> Authing 应用 ID ;
  • appSecret <String> Authing 应用 Secret;
  • host <String> 应用对应的用户池域名,例如 pool.authing.cn;
  • redirectUri <String> 认证完成后的重定向目标 URL, 认证时会进行校验,需要和控制台的设置保持一致。
  • logoutRedirectUri <String> 登出完成后的重定向目标 URL。
  • scope <String> 令牌具备的资源权限(应用侧向 Authing 请求的权限),以空格分隔,默认为 'openid profile',成功获取的权限会出现在 Access Token 的 scope 字段中。
  • serverJWKS <String> 服务端的 JWKS 公钥,用于验证 Token 签名,默认会通过网络请求从服务端的 JWKS 端点自动获取。
  • cookieKey <String> 存储认证上下文的 Cookie 名称。

¶ 示例

$authentication = new AuthenticationClient(array(
    "appId" => "62ba7bxxxxx597d3",
    "appSecret" => "69fed9ed06xxxxx522ce43dc6",
    "host" => "xxxxx.authing.cn",
    "redirectUri" => "https://xxxxx.com",
));

¶ 生成用户登录链接

$authentication->buildAuthUrl(options);

调用方法,生成用户登录链接返回给客户端,在合适的时机触发登录认证流程

¶ 参数

  • scope <String> 令牌具备的资源权限(应用侧向 Authing 请求的权限),覆盖初始化参数中的对应设置。
  • state <String> 随机字符串,选填,默认自动生成。
  • nonce <String> 随机字符串,选填,默认自动生成。
  • redirectUri <String> 回调地址,覆盖初始化参数中的对应设置。
  • forced <Boolean> 即便用户已经登录也强制显示登录页。

¶ 示例

$data = $authentication->buildAuthUrl();

¶ 示例数据

array(3) {
  ["url"]=>
  string(216) "https://xxxxx.authing.cn/oidc/auth?redirect_uri=https%3A%2F%2Fxxxxx.com&response_mode=query&response_type=code&client_id=62bxxxxxb597d3&scope=openid+profile&state=MMYMMlr3xMG3SfS&nonce=rrrxMMrrYfMf3M"
  ["state"]=>
  string(15) "MMYMMlr3xMG3SfS"
  ["nonce"]=>
  string(14) "rrrxMMrrYfMf3M"
}

¶ 用授权码获取用户登录态

$authentication->getLoginStateByAuthCode(code, redirectUri);

使用授权码 Code 获取用户的登录态信息。

¶ 参数

  • code <String> 授权码 Code,用户在认证成功后,Authing 会将授权码 Code 发送到回调地址,每个 Code 只能使用一次。
  • redirectUri <String> 发起认证时传入的回调地址。

¶ 示例

$data = $authentication->getLoginStateByAuthCode("H5Sm-cxxxHVTbqqcOO", "https://xxx.com");

¶ 示例数据

array(6) {
  ["accessToken"]=>
  string(718) "xxx"
  ["idToken"]=>
  string(823) "xxx"
  ["refreshToken"]=>
  NULL
  ["expireAt"]=>
  int(1209600)
  ["parsedIDToken"]=>
  array(21) {
    ["sub"]=>
    string(24) "62946ee4xxxcc44ffab6"
    ["birthdate"]=>
    NULL
    ["family_name"]=>
    NULL
    ["gender"]=>
    string(1) "M"
    ["given_name"]=>
    NULL
    ["locale"]=>
    NULL
    ["middle_name"]=>
    NULL
    ["name"]=>
    string(9) "xxx"
    ["nickname"]=>
    string(6) "xxx"
    ["picture"]=>
    string(84) "https://i2.hdslb.com/bfs/face/1f3f6ae9666xxxf5e5c7d3e9d366a.jpg@100Q.webp"
    ["preferred_username"]=>
    NULL
    ["profile"]=>
    NULL
    ["updated_at"]=>
    string(24) "2022-06-28T11:29:07.054Z"
    ["website"]=>
    NULL
    ["zoneinfo"]=>
    NULL
    ["nonce"]=>
    string(16) "rlGMxxxlf"
    ["at_hash"]=>
    string(22) "rizO2pxxxnZ-euWjQ"
    ["aud"]=>
    string(24) "62ba7b1xxx597d3"
    ["exp"]=>
    int(1657700008)
    ["iat"]=>
    int(1656490408)
    ["iss"]=>
    string(36) "https://xxxxx.authing.cn/oidc"
  }
  ["parsedAccessToken"]=>
  array(7) {
    ["jti"]=>
    string(21) "eswqqxxxpjWC59"
    ["sub"]=>
    string(24) "62946exxxffab6"
    ["iat"]=>
    int(1656490408)
    ["exp"]=>
    int(1657700008)
    ["scope"]=>
    string(14) "openid profile"
    ["iss"]=>
    string(36) "https://xxxxx.authing.cn/oidc"
    ["aud"]=>
    string(24) "62ba7b1xxxb597d3"
  }
}

¶ 通过 Access Token 获取用户身份信息

$authentication->getUserInfo(accessToken);

使用 Access token 获取用户信息。

¶ 参数

  • accessToken <String> Access token,使用授权码 Code 换取的 Access token 的内容。详情请见使用 OIDC 授权码模式。

¶ 示例

$data = $authentication->getUserInfo("xxxxx");

¶ 示例数据

array(15) {
  ["sub"]=>
  string(24) "62946exxxxx4ffab6"
  ["birthdate"]=>
  NULL
  ["family_name"]=>
  NULL
  ["gender"]=>
  string(1) "M"
  ["given_name"]=>
  NULL
  ["locale"]=>
  NULL
  ["middle_name"]=>
  NULL
  ["name"]=>
  string(9) "xxxxx"
  ["nickname"]=>
  string(6) "xxxxx"
  ["picture"]=>
  string(84) "https://i2.hdslb.com/bfs/face/1f3f6ae9666efxxxxxe5c7d3e9d366a.jpg@100Q.webp"
  ["preferred_username"]=>
  NULL
  ["profile"]=>
  NULL
  ["updated_at"]=>
  string(24) "2022-06-28T11:29:07.054Z"
  ["website"]=>
  NULL
  ["zoneinfo"]=>
  NULL
}

字段解释:

字段名翻译
subsubject 的缩写,唯一标识,一般为用户 ID
name姓名
given_name名字
family_name姓氏
middle_name中间名
nickname昵称
preferred_username希望被称呼的名字
profile基础资料
picture头像
website网站链接
gender性别
birthdate生日
zoneinfo时区
locale区域
updated_at信息更新时间

¶ 通过 Refresh Token 刷新用户的登录态,延长过期时间

$authentication->refreshLoginState(refreshToken);

使用 Refresh token 刷新登录态,并延长 accessToken 有效时间。

¶ 参数

  • refreshToken <String> Refresh token,为了获取 Refresh Token,需要在 scope 参数中加入 offline_access, 然后可以从 $authentication->getLoginStateByAuthCode 方法的返回值中获得 refresh_token 。

¶ 示例

//$data = $authentication->buildAuthUrl("openid profile offline_access");
$data = $authentication->refreshLoginState("xxxxx");

¶ 示例数据

array(6) {
  ["accessToken"]=>
  string(738) "xxxxx"
  ["idToken"]=>
  string(821) "xxxxx"
  ["refreshToken"]=>
  string(43) "xxxxx"
  ["expireAt"]=>
  int(1209600)
  ["parsedIDToken"]=>
  array(21) {
    ["birthdate"]=>
    NULL
    ["family_name"]=>
    NULL
    ["gender"]=>
    string(1) "M"
    ["given_name"]=>
    NULL
    ["locale"]=>
    NULL
    ["middle_name"]=>
    NULL
    ["name"]=>
    string(9) "xxx"
    ["nickname"]=>
    string(6) "xxx"
    ["picture"]=>
    string(84) "https://i2.hdslb.com/bfs/face/1f3f6ae9666xxxxxd3e9d366a.jpg@100Q.webp"
    ["preferred_username"]=>
    NULL
    ["profile"]=>
    NULL
    ["updated_at"]=>
    string(24) "2022-06-28T11:29:07.054Z"
    ["website"]=>
    NULL
    ["zoneinfo"]=>
    NULL
    ["sub"]=>
    string(24) "62946eexxxc44ffab6"
    ["nonce"]=>
    string(15) "rf3xxxlM3xM"
    ["at_hash"]=>
    string(22) "fkA-0lAxxxGVpI_Q"
    ["aud"]=>
    string(24) "62ba7xxx323d3b597d3"
    ["exp"]=>
    int(1657700476)
    ["iat"]=>
    int(1656490876)
    ["iss"]=>
    string(36) "https://xxxxx.authing.cn/oidc"
  }
  ["parsedAccessToken"]=>
  array(7) {
    ["jti"]=>
    string(21) "AfKbhxxxpoNR-l"
    ["sub"]=>
    string(24) "62946ee4xxxffab6"
    ["iat"]=>
    int(1656490876)
    ["exp"]=>
    int(1657700476)
    ["scope"]=>
    string(29) "profile openid offline_access"
    ["iss"]=>
    string(36) "https://xxxxx.authing.cn/oidc"
    ["aud"]=>
    string(24) "62ba7xxx3b597d3"
  }
}

¶ 生成登出 URL

$authentication->buildLogoutUrl(options)

生成登出 URL。

¶ 参数

  • idToken <String> 用户登录时获取的 ID Token,用于无效化用户 Token,建议传入。
  • redirectUri <String> 登出完成后的重定向目标 URL,覆盖初始化参数中的对应设置。
  • state <String> 传递到目标 URL 的中间状态标识符。

¶ 示例

$data = $authentication->buildLogoutUrl();

¶ 示例数据

string(49) "https://xxxxx.authing.cn/oidc/session/end?"

¶ 将用户浏览器重定向到 Authing 的认证发起 URL 进行认证

$authentication->loginWithRedirect(options);

用户发起认证请求,你可以在服务端直接调用这个方法,通过操作请求的 response 对象,把用户的浏览器重定向到 Authing 的认证发起 URL 进行认证

¶ 参数

  • scope <String> 令牌具备的资源权限(应用侧向 Authing 请求的权限),覆盖初始化参数中的对应设置。
  • state <String> 随机字符串,选填,默认自动生成。
  • nonce <String> 随机字符串,选填,默认自动生成。
  • redirectUri <String> 回调地址,覆盖初始化参数中的对应设置。
  • forced <Boolean> 即便用户已经登录也强制显示登录页。

¶ 示例

$data = $authentication->loginWithRedirect();

¶ 示例数据

array(2) {
  ["cookie"]=>
  string(168) "X-Authing-Node-OIDC-State=eyJxxxxxdFVyaSI6Imh0dHBzOlwvXC9iYWlkdS5jb20ifQ; HttpOnly; SameSite=Lax"
  ["url"]=>
  string(216) "https://xxxxx.authing.cn/oidc/auth?redirect_uri=https%3A%2F%2Fxxxxx.com&response_mode=query&response_type=code&client_id=62xxxxx97d3&scope=openid+profile&state=YxfMl3llG3MYl&nonce=rlGMlGlY3SMMl3lf"
}

¶ 应用回调端点处理认证返回结果

$authentication->handleRedirectCallback(url, cookie)

用户完成认证后,跳转到回调地址,通过调用本方法,校验 state 值,并消费 code 获取相应的登录信息

¶ 参数

  • url <String> 完整的回调地址 URL。
  • cookie <String> 上下文 Cookie。

¶ 示例

$data = $authentication->handleRedirectCallback("https://www.xxxxx.com/?code=EVU5_SSjI57IxxxxxpFFom4FdE5EH9IZX&state=MGMY333GYf3rMS3f", "X-Authing-Node-OIDC-State=eyJzdGF0ZSI6IlNZcnhsZlNsWXJZZllTIiwibm9uY2UiOiJsWXxxxxxRwczpcL1wvYmFpZHUuY29tIn0; HttpOnly; SameSite=Lax");

¶ 示例数据

array(6) {
  ["accessToken"]=>
  string(718) "xxxxx"
  ["idToken"]=>
  string(819) "xxxxx"
  ["refreshToken"]=>
  NULL
  ["expireAt"]=>
  int(1209600)
  ["parsedIDToken"]=>
  array(21) {
    ["sub"]=>
    string(24) "62946exxxxxc44ffab6"
    ["birthdate"]=>
    NULL
    ["family_name"]=>
    NULL
    ["gender"]=>
    string(1) "M"
    ["given_name"]=>
    NULL
    ["locale"]=>
    NULL
    ["middle_name"]=>
    NULL
    ["name"]=>
    string(9) "xxxxx"
    ["nickname"]=>
    string(6) "xxxxx"
    ["picture"]=>
    string(84) "https://i2.hdslb.com/bfs/face/1f3f6ae9666ef214xxxxxd3e9d366a.jpg@100Q.webp"
    ["preferred_username"]=>
    NULL
    ["profile"]=>
    NULL
    ["updated_at"]=>
    string(24) "2022-06-28T11:29:07.054Z"
    ["website"]=>
    NULL
    ["zoneinfo"]=>
    NULL
    ["nonce"]=>
    string(13) "lYxGxxxxxSG"
    ["at_hash"]=>
    string(22) "VkZ_MGxxxxxYkZoDlg"
    ["aud"]=>
    string(24) "62ba7bxxxxx3d3b597d3"
    ["exp"]=>
    int(1657699719)
    ["iat"]=>
    int(1656490119)
    ["iss"]=>
    string(36) "https://xxxxx.authing.cn/oidc"
  }
  ["parsedAccessToken"]=>
  array(7) {
    ["jti"]=>
    string(21) "vlXxJxxxxxafAaCx-"
    ["sub"]=>
    string(24) "62946xxxxxbcc44ffab6"
    ["iat"]=>
    int(1656490119)
    ["exp"]=>
    int(1657699719)
    ["scope"]=>
    string(14) "openid profile"
    ["iss"]=>
    string(36) "https://xxxxx.authing.cn/oidc"
    ["aud"]=>
    string(24) "62ba7xxxxx23d3b597d3"
  }
}

¶ 将浏览器重定向到 Authing 的登出发起 URL 进行登出

$authentication->logoutWithRedirect(options);

¶ 参数

  • idToken <String> 用户登录时获取的 ID Token,用于无效化用户 Token,建议传入。
  • redirectUri <String> 登出完成后的重定向目标 URL,覆盖初始化参数中的对应设置。
  • state <String> 传递到目标 URL 的中间状态标识符。

¶ 示例

$data = $authentication->logoutWithRedirect();

¶ 示例数据

string(49) "https://xxxxx.authing.cn/oidc/session/end?"
上一篇: 安装使用 下一篇: 获取用户信息
  • 初始化
  • 生成用户登录链接
  • 用授权码获取用户登录态
  • 通过 Access Token 获取用户身份信息
  • 通过 Refresh Token 刷新用户的登录态,延长过期时间
  • 生成登出 URL
  • 将用户浏览器重定向到 Authing 的认证发起 URL 进行认证
  • 应用回调端点处理认证返回结果
  • 将浏览器重定向到 Authing 的登出发起 URL 进行登出

用户身份管理

集成第三方登录
手机号闪验 (opens new window)
通用登录表单组件
自定义认证流程

企业内部管理

单点登录
多因素认证
权限管理

开发者

开发文档
框架集成
博客 (opens new window)
GitHub (opens new window)
社区用户中心 (opens new window)

公司

400 888 2106
sales@authing.cn
北京市朝阳区北辰世纪中心 B 座 16 层(总)
成都市高新区天府五街 200 号 1 号楼 B 区 4 楼 406 室(分)

京ICP备19051205号

beian京公网安备 11010802035968号

© 北京蒸汽记忆科技有限公司