我正在尝试使用box应用程序用户id创建访问令牌。我使用下面的代码来创建box应用程序用户
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"是否可以使用box应用程序用户id生成访问令牌。
编辑的
我已经在BOX中生成了公钥。然后我就有了公开密钥和私钥细节的文件,
{
"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));在此之后,我在这里遇到了如何实现私钥和公钥的问题。实际上,我的JSON文件是从BOX下载的。我不明白JTI是什么?如何在这个文件中添加公钥和私钥JSON文件?该怎么做呢?
我已经按照文档手动生成私钥,如下所示
openssl genrsa -aes256 -out private_key.pem 2048然后我给出密码为"12345“。生成公钥如下,
openssl rsa -pubout -in private_key.pem -out public_key.pem然后我在BOX中添加了公钥,并编写了如下代码,
$data = file_get_contents('private_key.pem');
$result = openssl_pkey_get_private($data,"12345");
print_r($result);它给出了以下结果
Resource id #4这些看起来不像是加密的数据。以及如何在php中调用box时实现私有和公共。
发布于 2017-08-31 19:49:12
我不建议您自己实现这一点,因为已经有几个库实现了这个协议。然而,我将我的答案分成两部分,第一部分解释如何使用开源包解决问题,第二部分帮助您完成私钥签名。
使用包
有几个php包支持JWT签名,在编写时使用最多的是lcobucci/jwt,但这里还有其他实现:https://packagist.org/search/?q=jwt。
您可以使用作曲家来安装它。由于Version4.0目前还没有文档化,所以我建议您安装3.2并查看该版本的自述文件文件。
您可以在您的项目中使用:composer require lcobucci/jwt:^3.2要求这样做。
您的代码示例建议您需要RSA256,库中有一个示例:
<?php
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Signer\Keychain; // just to make our life simpler
use Lcobucci\JWT\Signer\Rsa\Sha256; // you can use Lcobucci\JWT\Signer\Ecdsa\Sha256 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不同的字符,所以一定要正确处理。
https://stackoverflow.com/questions/45478218
复制相似问题