logonbox / authenticator
一个API,用于在您的PHP应用程序中使用LogonBox Authenticator凭证
Requires
- php: >=7.3
- nategood/httpful: 0.3.*
- phpseclib/phpseclib: 3.0.*
Requires (Dev)
- phpunit/phpunit: ^9.5
README
使用此API将LogonBox Authenticator集成到您的PHP应用程序的身份验证流程中。
LogonBox Authenticator使用类似于SSH私钥身份验证的认证机制,其中用户密钥在凭证服务器上的授权密钥列表中公布。此API将读取受信任的公钥,然后将签名请求负载提交给凭证服务器,由相应的私钥进行签名。
作为签名操作的一部分,用户必须在LogonBox Authenticator应用程序中授权请求。一旦授权,负载将由私钥签名,该私钥仅由应用程序的安全存储器持有。
为了验证用户,API将验证返回的签名以获得身份验证结果。
关于LogonBox Authenticator
使用LogonBox的2-Factor Authentication app 为Android和iOS保护您的员工、密码和应用程序。
其他语言
使用方法
直接签名
如果您正在使用不同的协议,无法通过网络浏览器重定向用户,或者想要提供自己的用户界面,您可以通过API独家执行认证。
<?php use Authenticator\AuthenticatorClient; use Logger\AppLogger; use RemoteService\RemoteServiceImpl; require_once __DIR__ . '/../../../vendor/autoload.php'; try { $remoteService = new RemoteServiceImpl("some.directory", 443, new AppLogger()); $authenticatorClient = new AuthenticatorClient($remoteService); $authenticatorClient->debug(true); $principal = "some@mail.com"; $response = $authenticatorClient->authenticate($principal); $result = $response->verify(); echo $result ? "Verified ....." : "Rejected ......" . PHP_EOL; } catch (Exception $e) { echo $e; }
服务器重定向
如果您正在将用户登录到Web应用程序,您可以创建一个请求,并将用户重定向到凭证服务器上的URL,在那里他们将收到提示在设备上授权请求。这消除了您创建自己的用户界面的需要,并提供了现代、干净的认证流程。
身份验证完成后,服务器将重定向回您的Web应用程序,并带有身份验证响应,您可以将它传递到API进行验证。
login.php(此HTML响应将要求您提供用于认证的用户密钥的id)
<?php echo ' <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Login</title> </head> <body> <form method="post" action="start.php"> <input type="text" name="user" value="" /> <button type="submit" name="submit">Submit</button> </form> </body> </html> ';
start.php(此文件将使用终端用户提交的用户id来启动认证过程,这还设置了一个重定向URL,认证服务器将重定向到该URL)
<?php use Authenticator\AuthenticatorClient; use Logger\AppLogger; use RemoteService\RemoteServiceImpl; require '../../../vendor/autoload.php'; if(session_status() == PHP_SESSION_NONE) { session_start(); } try { $remoteService = new RemoteServiceImpl("some.directory", 443, new AppLogger()); $authenticatorClient = new AuthenticatorClient($remoteService); $user = $_POST["user"]; $authenticatorRequest = $authenticatorClient ->generateRequest($user, "https:///src/sample/server_redirect/authenticator-finish.php?response={response}"); $_SESSION["encodedPayload"] = $authenticatorRequest->getEncodedPayload(); header("Location: " . $authenticatorRequest->getSignUrl(), true, 302); } catch (Exception $e) { echo $e; }
authenticator-finish.php(此文件将接收认证服务器返回的已验证签名响应)
<?php use Authenticator\AuthenticatorClient; use Authenticator\AuthenticatorRequest; use Logger\AppLogger; use RemoteService\RemoteServiceImpl; require '../../../vendor/autoload.php'; if(session_status() == PHP_SESSION_NONE) { session_start(); } try { $response = $_GET["response"]; $encodedPayload = $_SESSION["encodedPayload"]; $remoteService = new RemoteServiceImpl("some.directory", 443, new AppLogger()); $authenticatorClient = new AuthenticatorClient($remoteService); $authenticatorRequest = new AuthenticatorRequest($authenticatorClient, $encodedPayload); $authenticatorResponse = $authenticatorRequest->processResponse($response); echo "The verification result => " . $authenticatorResponse->verify(); } catch (Exception $e) { echo $e . PHP_EOL; }
调试
使用默认的echo输出,使用了一个简单的Logger接口。您可以在创建客户端对象后启用此功能。
$authenticatorClient->debug(true);
这对于测试应该是足够的。要将日志记录集成到您更广泛的应用程序中,只需为AuthenticatorClient的constructor提供一个LoggerService实现即可。
<?php use Authenticator\AuthenticatorClient; use Logger\MyAppLogger; use RemoteService\RemoteServiceImpl; require_once __DIR__ . '/../../../vendor/autoload.php'; try { $remoteService = new RemoteServiceImpl("some.directory", 443, new MyAppLogger()); $authenticatorClient = new AuthenticatorClient($remoteService); // ..... logic TODO } catch (Exception $e) { echo $e; }
最低要求
使用PHP 7.3.33测试了当前稳定版本。
