mkH*_*Hun 14 php rest access-token box-api box
我正在尝试使用box app用户ID创建访问令牌.我使用以下代码创建了box app用户
curl https://api.box.com/2.0/users \
-H "Authorization: Bearer <TOKEN>" \
-d '{"name": "Ned Stark", "is_platform_access_only": true}' \
-X POST
Run Code Online (Sandbox Code Playgroud)
然后给出以下结果
{"type":"user","id":"2199107004","name":"Ned Stark","login":"AppUser_399382_9BNZHI03nJ@boxdevedition.com","created_at":"2017-08-03T00:58:04-07:00"
Run Code Online (Sandbox Code Playgroud)
是否可以使用box app用户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>
}
Run Code Online (Sandbox Code Playgroud)
然后我生成了头和有效负载,如下所示
$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));
Run Code Online (Sandbox Code Playgroud)
在此之后,我遇到了如何在这里实现私钥和公钥.实际上我有从BOX API下载的JSON文件.我无法理解是什么JTI
?如何在此添加公钥和/或私钥JSON文件?怎么做?
我按照文档手动生成私钥,如下所示
openssl genrsa -aes256 -out private_key.pem 2048
Run Code Online (Sandbox Code Playgroud)
然后我给了密码"12345".并生成公钥如下,
openssl rsa -pubout -in private_key.pem -out public_key.pem
Run Code Online (Sandbox Code Playgroud)
然后我在BOX-API中添加了公钥,我编写了如下代码,
$data = file_get_contents('private_key.pem');
$result = openssl_pkey_get_private($data,"12345");
print_r($result);
Run Code Online (Sandbox Code Playgroud)
它给出了以下结果
Resource id #4
Run Code Online (Sandbox Code Playgroud)
这些看起来不像加密数据.以及如何在php中调用box api时实现私有和公共.
小智 3
我不建议您自己实现此协议,因为已经有几个库实现了此协议。不过,我将答案分为两部分,第一部分解释如何使用开源包来解决您的问题,第二部分帮助您解决是否想要进行私钥签名的问题。
有几个 php 包支持 JWT 签名,在撰写本文时最常用的是lcobucci/jwt,但这里还可以找到其他实现: https: //packagist.org/search/?q=杰威特
您可以使用composer来安装它。由于 4.0 版本目前没有文档记录,我建议您安装 3.2 并查看该版本的README文件。
您可以在您的项目中使用以下命令: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
Run Code Online (Sandbox Code Playgroud)
使用公钥和私钥时,您始终必须确保私钥的安全。然而,您可以轻松地向全世界发布您的公钥,而不会影响安全性。
签名是使用私钥完成的,因为您不希望人们能够伪造您的签名,所以使用公共部分签名将使每个人都可以这样做。这也意味着验证步骤始终使用公钥,因为每个人都应该能够做到这一点。
您提供的代码示例只是加载私钥,但不对其执行任何操作。为了签名,您需要使用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;
Run Code Online (Sandbox Code Playgroud)
如果您仍然想实现完整的协议,我建议您再看一下该包。正如评论中已经建议的完整规范:
https://www.rfc-editor.org/rfc/rfc7519.txt
最后提示:JWT 对 base64 使用一些与 php 不同的字符,因此请务必正确处理。
归档时间: |
|
查看次数: |
949 次 |
最近记录: |