jeichorn/g-authify-php

GAuthify PHP 客户端库

v2.0.1 2015-10-27 19:47 UTC

This package is auto-updated.

Last update: 2024-09-27 13:13:09 UTC


README

直接链接到库.

这是GAuthify的PHP API客户端。GAuthify REST API可以帮助网站快速通过Google Authenticator、短信和电子邮件添加多因素认证。此包是该API的一个简单封装。

该库已被Joshua Eichorn @Pagely(https://pagely.com) 分支,以便使用composer进行命名空间化和安装

安装

使用composer安装

composer require jeichorn/g-authify-php

包含vendor/autoload.php,并使用类

include 'vendor/autoload.php';
use GAuthify\GAuthify;

为了快速测试确保一切正常运行,请执行

include 'vendor/autoload.php';
use GAuthify\GAuthify;
$gauthify = new GAuthify(<api_key>);
$gauthify->quick_test(<test_email>(optional), <test_number>(optional));

使用方法

####初始化:#### 首先通过此处注册账户以获取API密钥。

首先实例化一个GAuthify对象

include 'vendor/autoload.php';
use GAuthify\GAuthify;
$auth_instance = new GAuthify(<api_key>);

####创建用户:####

auth_instance->create_user(<unique_id>, <display_name>, <email> *optional, <sms_number> *optional, <voice_number> *optional)
  • unique_id:用于识别用户的id。可以是数据库中用户的PK。
  • display_name:将在库中显示的名称
  • email:有效的电子邮件地址
  • sms_number:有效的短信手机号码(目前仅限美国!)
  • voice_number:有效的电话号码(目前仅限美国!)
  • meta:要添加到元数据的键/值对字典
  • 返回:用户哈希或抛出Error

返回的用户哈希将包含在GAuthify.com仪表板页面上概述的参数。您可以向用户展示可在其Google Authenticator应用程序中扫描的二维码,或者您可以通过链接/iframe指令URL。

####更新用户:####

auth_instance->update_user(<unique_id>, <email> *optional, <sms_number> *optional, <voice_number> *optional, <reset_key> *optional)
  • unique_id:用于识别用户的id。可以是数据库中用户的PK。
  • display_name:将在库中显示的名称
  • email:有效的电子邮件地址
  • sms_number:有效的短信手机号码(目前仅限美国!)
  • voice_number:有效的电话号码(目前仅限美国!)
  • meta:要添加到元数据的键/值对字典
  • reset_key:如果设置为['true' ,'t', '1']中的任何值,Google Authenticator的密钥将重置为新的一个。
  • 返回:更新后的用户哈希或抛出Error

####删除用户:####

auth_instance->delete_user(<unique_id>)
  • unique_id:用于识别用户的id。可以是数据库中用户的PK。
  • 返回:True或抛出Error

####获取所有用户:####

auth_instance->get_all_users()
  • 返回用户哈希列表

####获取用户:####

auth_instance->get_user(<unique_id>)
  • unique_id:用于识别用户的id。可以是数据库中用户的PK。
  • 返回用户哈希或抛出Error

####通过Token获取用户:####

auth_instance->get_user_by_token(<token>)
  • token:由ezGAuth提供的以gat开头的35字符token
  • 返回用户哈希或抛出Error

####检查认证码:####

auth_instance->check_auth(<unique_id>, <auth_code>, safe_mode = False)
  • unique_id:用于识别用户的id。可以是数据库中用户的PK。
  • auth_code:从Google Authenticator、短信、电子邮件或OTP获取的代码
  • safe_mode:如果设置为true,所有请求期间的异常都将被抑制,并且检查将返回True。这实际上是在服务器出现异常时暂时绕过双因素认证。
  • 返回:True/False (bool)或抛出Error

####发送电子邮件:####

auth_instance->send_email(<unique_id>, <email> *optional)
  • unique_id:用于识别用户的id。可以是数据库中用户的PK。
  • email:有效的电子邮件地址
  • 返回用户哈希或抛出Error

####发送短信:####

auth_instance->send_sms(<unique_id>, <sms_number> *optional)
  • unique_id:用于识别用户的id。可以是数据库中用户的PK。
  • sms_number:有效的短信美国电话号码(目前仅限美国!)
  • 返回用户哈希或抛出Error

####发送语音:####

auth_instance->send_voice(<unique_id>, <voice_number> *optional)
  • unique_id:用于识别用户的id。可以是数据库中用户的PK。
  • voice_number:有效的美国电话号码(目前仅限美国!)
  • 返回用户哈希或抛出Error

错误

可以从服务器通过以下方式获取最新的json格式错误

auth_instance->api_errors()

它们很少改变,并且将与旧版本兼容。

主要的错误类是GAuthifyError,可以使用如下方式

use GAuthify\GAuthifyError;

try{
    <code here>
}
catch (GAuthifyError $e){
    print(e->msg) # The error message
    print(e->http_status) # The http status code
    print(e->error_code) # A error code listed in the GAuthfiy application
    print(e->response_body) # The raw http response
}

以下错误扩展了GAuthifyError

  • ApiKeyError - 包装401响应(API密钥问题)
  • RateLimitError - 包装402响应(计划限制等)
  • ParameterError - 包装406响应(格式错误的unique_id、sms、电话号码等)
  • NotFoundError - 包装404错误(请求不存在的unique_id)
  • ConflictError - 包装409错误(现有资源)
  • ServerError - 包装500和其他服务器错误