legalthings / authorizer
HTTP请求授权器
v0.1.11
2020-08-31 10:35 UTC
Requires
- guzzlehttp/guzzle: ^5.0 || ^6.0
Requires (Dev)
- codeception/codeception: ^2.1
This package is auto-updated.
Last update: 2024-08-29 04:09:05 UTC
README
使用授权库,一个web服务可以为资源生成一个访问令牌。库使用公钥加密来加密访问令牌。这意味着只有拥有私有解密密钥的系统才能获取资源。
要求
- PHP >= 5.5.0
所需的PHP扩展由composer标记
安装
可以使用composer安装此库。
composer require legalthings/authorizer
工作原理
系统A有一个需要授权的资源。它只允许系统B访问该资源。客户端可以使用该资源,但没有直接访问权。同时使用系统A和系统B的客户端希望系统A与系统B共享特定的资源。
客户端请求后,系统A将为资源生成一个访问令牌。它下载系统B的公钥加密密钥,并使用它来加密访问令牌。这个加密令牌返回给客户端。
客户端将资源链接和加密令牌传递给系统B。系统B将解密加密令牌,并使用它来下载资源。
示例
系统A(有资源)
use LegalThings/Authorizer; Authorizer::$globalSecret = 'some-secret-which-stays-the-same'; $pdf = basename($_GET['pdf']); if (isset($_GET['authzgen'])) { if (parse_url($_GET['authzgen'], PHP_URL_HOST) !== 'system-b.example.com') { http_response_code(403); echo "Will only grant access for system-b.example.com"; exit(); } $encryptedToken = Authorizer::sign($pdf, $_GET['authzgen']); // authzgen is a string with the format: {{public_key_url}};{{time_from}};{{time_to}} header('Content-Type: text/plain'); echo $encryptedToken; exit(); } $mayAccess = isset($_GET['authz']) && Authorizer::verify($pdf, $_GET['authz']); // authz is the decrypted secret if (!$mayAccess) { http_response_code(403); echo "Access denied"; exit(); } // Get and output resource header('Content-Type: application/pdf'); readfile('path/to/resources/' . $pdf);
系统B(可以下载和使用资源)
use LegalThings/Authorizer; $link = $_POST['link']; if (isset($_POST['token'])) { $encryptedToken = $_POST['token']; $token = Authorizer::decrypt($encryptedSecret, 'path/to/private_key.pem'); $link .= (strstr($link, '?') ? '&' : '?') . 'authz=' . $token; } $pdf = file_get_contents($link); // Let's do something with the PDF $username = $_SESSION['username']; file_put_contents("../userdata/$username/" . md5(microtime()) . ".pdf", $pdf);
客户端
LINK="http://system-a.example.com/get-pdf.php?pdf=abc.pdf" ENCRYPTED_TOKEN=$(curl --get "$LINK" --data-urlencode "authzgen=http://system-b.example.com/authorizer.pem") curl --post "http://system-b.example.com/use-pdf.php" --data-urlencode "link=$LINK" --data-urlencode "authz=$ENCRYPTED_TOKEN"
这有什么用?
这是一种允许两个系统之间以最小耦合共享资源的方法。
系统B可以使用互联网上的任何PDF。通过实现Authorizer
,它为只想与系统B共享资源的服务提供了实现此目的的手段。