如何在 Box-API 中使用应用用户 ID 创建访问令牌?



我正在尝试使用盒子应用程序用户 ID 创建访问令牌。我使用以下代码创建盒子应用程序用户

curl https://api.box.com/2.0/users 
-H "Authorization: Bearer <TOKEN>" 
-d '{"name": "Ned Stark", "is_platform_access_only": true}' 
-X POST

然后给出以下结果

{"type":"user","id":"2199107004","name":"Ned Stark","login":"AppUser_399382_9BNZHI03nJ@boxdevedition.com","created_at":"2017-08-03T00:58:04-07:00"

是否可以使用框应用程序用户 ID 生成访问令牌?

编辑

我已经在 BOX API 中生成了公钥。然后我有文件,其中包含公钥和私钥详细信息,如下面所示,

{
"boxAppSettings": {
"clientID": <Client ID>,
"clientSecret": <clientsecret>,
"appAuth": {
"publicKeyID": <publickeyid>,
"privateKey": "-----BEGIN ENCRYPTED PRIVATE KEY-----Key heresn-----END ENCRYPTED PRIVATE KEY-----n",
"passphrase": <phrase>
}
},
"enterpriseID": <enterpriseId>
}

然后我生成了标头和有效负载,如下所示

$header = ["typ"=> "JWT", "alg"=>"RS256","kid"=> <public key id>];
$payload = [
"iss"=> "<client id>",
"sub"=> "<APP USER ID>",
"box_sub_type"=> "user",
"aud"=>"https://api.box.com/oauth2/token",
"jti"=>"<I don't know what is this>",
"exp"=>1428699385
];
$header = base64_encode(json_encode($header));
$payload = base64_encode(json_encode($payload));

在此之后,我陷入了如何在这里实现私钥和公钥的困境。实际上,我正在从BOX API下载的JSON文件。 我不明白JTI是什么?如何在此添加公钥和/或私钥 JSON 文件?怎么办?


我已经根据文档手动生成私钥,如下所示

openssl genrsa -aes256 -out private_key.pem 2048

然后我给出了密码为"12345"。并生成公钥如下,

openssl rsa -pubout -in private_key.pem -out public_key.pem

然后我在 BOX-API 中添加了公钥,并编写了如下代码,

$data = file_get_contents('private_key.pem');
$result = openssl_pkey_get_private($data,"12345"); 
print_r($result);

它给出以下结果

Resource id #4

这些看起来不像加密数据。以及在php中调用box api时如何实现私有和公共?

我不建议您自己实现此协议,因为已经有几个库实现了此协议。但是,我将答案分为两部分,第一部分解释了如何使用开源包来解决您的问题,如果您想进行私钥签名,第二部分可以帮助您。

使用包

有几个支持JWT签名的php包,在编写本文时最常用的是lcobucci/jwt,但这里还有其他实现: https://packagist.org/search/?q=jwt

您可以使用作曲家来安装它。由于现在没有记录 4.0 版,我建议您安装 3.2 并查看该版本的自述文件。

您可以使用以下方法在项目中要求这样做:composer require lcobucci/jwt:^3.2

您的代码示例表明您需要 RSA256,该库有一个示例:

<?php
use LcobucciJWTBuilder;
use LcobucciJWTSignerKeychain; // just to make our life simpler
use LcobucciJWTSignerRsaSha256; // you can use LcobucciJWTSignerEcdsaSha256 if you're using ECDSA keys
$signer = new Sha256();
$keychain = new Keychain();
$token = (new Builder())
->setIssuer('http://example.com') // Configures the issuer (iss claim)
->setAudience('http://example.org') // Configures the audience (aud claim)
->setId('4f1g23a12aa', true) // Configures the id (jti claim), replicating as a header item
->setIssuedAt(time()) // Configures the time that the token was issue (iat claim)
->setNotBefore(time() + 60) // Configures the time that the token can be used (nbf claim)
->setExpiration(time() + 3600) // Configures the expiration time of the token (nbf claim)
->set('uid', 1) // Configures a new claim, called "uid"
->sign($signer,  $keychain->getPrivateKey('file://{path to your private key}')) // creates a signature using your private key
->getToken(); // Retrieves the generated token

签名和验证

使用公钥和私钥时,您必须始终确保私钥的安全。但是,您可以轻松地将公钥发布到世界,而不会影响安全性。

签名是使用私钥完成的,因为您不希望人们能够伪造您的签名,因此使用公共部分签名将使每个人都可以做到这一点。这也意味着验证步骤始终使用公钥,因为每个人都应该能够执行此操作。

在 PHP 中执行此操作

您提供的代码示例只是加载私钥,但不对其执行任何操作。为了进行签名,您需要将openssl_sign与变量一起使用。Resource #xx只是意味着对 php 中外部事物的引用。

<?php
// Data to sign
$payload = 'TEST';
// Generate a new key, load with: openssl_pkey_get_private
$privateKey = openssl_pkey_new(array('private_key_bits' => 512)); // NOT SECURE BUT FAST
// Extract public part from private key
$details = openssl_pkey_get_details($privateKey);
// Use openssl_pkey_get_public to load from file
$publicKey = $details['key'];
// Generated by openssl_sign
$signature = null;
// Sign with private key
openssl_sign($payload, $signature, $privateKey, OPENSSL_ALGO_SHA256);
// Use base64 because the signature contains binairy data
echo 'Signed data: '.base64_encode($signature).PHP_EOL;
// Use publicKey to verify signature
$valid = openssl_verify($payload, $signature, $publicKey, OPENSSL_ALGO_SHA256);
echo 'Signature is '.($valid ? 'Valid' : 'Invalid').PHP_EOL;

还有什么

如果您仍然想实现完整的协议,我建议您再看一下该软件包。正如评论中已经建议的那样,完整的规范:

https://www.rfc-editor.org/rfc/rfc7519.txt

最后一个提示:JWT 对 base64 使用的一些字符与 php 不同,因此请务必正确处理。

最新更新