编程语言
首页 > 编程语言> > php – JWT for GitHub Integrations API的错误凭证

php – JWT for GitHub Integrations API的错误凭证

作者:互联网

我注册了GitHub integration并下载了RSA私钥(PEM文件).在我的PHP脚本中,我使用this library来编码我的令牌,这样:

use Firebase\JWT\JWT;

$token = [
        "iat" => time(),
        "exp" => time() + 3600,
        "iss" => $my_integration_id
];
$key = file_get_contents($path_to_pem_file);
echo JWT::encode($token, $key, "RS256");

PEM文件是这样的(我已经重新生成了私钥):

-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEA1bT61yW2fo4SI6TZXo+3dsyyxitcsw0kdhoGufsEkgdqbpeF
2/TfJtwYj9Veq11LNHuyoYk51DxUIyoC2Ul45E/s9kOw+qjXDJAOf3z9kq+obcOL
145ivQwsRF8bB1MQf8YsE/jAhVdxdIfRbw4UG8Fn0RPce/WQ18RI7Li4xfM138RP
2MQhZsx289ale7n+OtDzzajC4oj9qlaNFFKpSuCVGf6fqnktCOoFu1+ouB/+u3oc
eneS/qmSxwI7N7MrxOTnoHqhxIiatNFisK85Anc/WZ9duWS9i88GhtK86YMaWnCj
5nodzFwSF6SzBS+iOrI2AJennOTjsOrtjjQXzQIDAQABAoIBAQDKVhoRTfGljRn7
iX8NGwFXh6PUIQYZuN3GvAmWWOYQ5NiOcQQaJ5SIlGbk9940XZZw0JVmgCiym5QF
3ybnV7sQX+Q5ngmYqfdCO7qeVlc3ZP0RP83Nf1BZMYdQDMPogWrjr1vYZPLSzOHs
tRNEFI5RYSLMQAPHkc9bmsp45sR7FXj+tZEjM3NkcF4n8Jud/ARVAX/tzOWfF6A4
rhtGJfqYCvEF69J/dsHyy28egawrTxqPd6d1BKLdOh05xHkFL26wtgJWpHcNAZNZ
DUZs0Twe6Cw3XYQtlUD6D5mkj3zzh5hZ4xsoKftNfD2ZA1N0RVks0j5wrUjPI6jV
bVDRvvbBAoGBAPp2yjhTuchwpVOcGVYnqF1jVgXssSYdiydUglKtxzhta7c23xqK
v5B/cWdsz8mIKFmOcjr97GFUT8K6sZG77im05gDBCV7YuIMwekNmZ3m1MqUWtiZY
sEqCarIYPzQk9iPPxyZrmOVL/5KqlgRj9YmYFzjLpfnaxvk4rIMyVF21AoGBANpu
NLw7LfufPeSbIkSIeZ6X1ndspuZDzWVjgi0hmVdozMwxT1wmfoinJ27gxhKStwmG
9kbYE4QbGBSAlkJqoSf8QWVIH+/Qq9/9mbi1T4p+AgJSYiz1voOGnOk0RoJku6Zn
9XkcGXCUh5ckxWyYbXjHqBqHTXHeJzk9zoRfBuC5AoGBAI1/yPUTkBUGVtCqksHS
u9KuyS2lr9spu7DnlIzjMnbVkpI6hMa49krLRSU6GxXvP2SBDlX3mLDP8hPAZ4s+
0elsErkAVavo4izzmU80rmbbEJamAxgc6NPfZCnjeumZrBlFTGyPQxzf165jg0dN
ta0baJskqktC50BrXULtjy41AoGANa4j06aPlGj8IvRlJYaMrQcPs+XF1o0PKz2b
PlAliS++NOhhYnorWpYJwNwIkYPw66x5VcvAcBTghBld1BC0bkk2IRfqkVstAi0m
3Bfi5lw1TjDE49u46EXqxf0M3vq2ixy9XPgk4GAAOorU4e1L6gTXMT3TMIqyBQVF
fY6E+DECgYEA22HhPud5KLAX6drbMEe54uttiUrs/K3nft/xTYWU1BIG9r/HPKQh
3OWD/Dknek+i5hW89OOROPaUmnjaTR3/ARAxTlpxDdBWKu2mCXS/LcdEWdy7teEu
dDbqqotO4jmTh94FVWgVLDVVmrVSp++fwstI6foPGPABlbsrJxyGUsc=
-----END RSA PRIVATE KEY-----

我将整个字符串传递给JWT :: encode,得到了这个输出:

eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpYXQiOjE0NzQzOTI0NjQsImV4cCI6MTQ3NDM5NjA2NCwiaXNzIjoyMDF9.ZodSJyrq_SJfLTx4ifnCRWC9cbX1NmqOY3bADcuU7Wc18oZnH3EtZErvLgW6-reuR1-flsNbrji2dL9Zsm3rE-1FtEambcs1Xxynz3PE42yFMimQ5WyiAHdJhbh8-fB0hLIxn2I0oBiJbpZp92Rm2E2xxA1m4lmAubatY0JDR3FS4MmjR-pOZRbkgA4QwaTb0Q7R0tSUaoJo1ETLZoApp5ofsyt5hciB7wogTigA08H4r8TBUuSY4LeHWX9IfmDqHh5gVLDHhFXuLp1Qfm-8ltnLMgcelTmA9QE9NxxVcHQtYvNnE-EvXsh04Oqiyg51eq5cDoc4wA_WhTB4w-9NWg

我将此输出(没有尾随换行,我可以确认)存储到Bash变量$JWT中,并试图像这样访问GitHub API:

curl -i -H "Accept: application/vnd.github.machine-man-preview+json"\
        -H "Authorization: Bearer $JWT" https://api.github.com/zen

但是,GitHub的JSON响应是:

{
  "message": "Bad credentials",
  "documentation_url": "https://developer.github.com/v3"
}

我怀疑它失败了,因为GitHub API zen不适用于集成令牌,但这似乎不是问题.特别是,消息说它是Bad Credentials.但是我已经按照GitHub documentation中的所有步骤进行了操作.我是否错误地编写了PHP脚本?

解决方法:

在过去的三天里,我花了很多时间来对抗401不良凭证.

刚刚开始工作,我可以告诉你,你最初的假设是正确的:如果端点没有设置为集成,那么你将获得401.

您应该检查的另一件事是您发送的时间是整数.发送浮动将导致401.
(我正在使用Python,并且datetime类没有转换到Unix纪元的方法,所以我编写了自己的方法,它返回了几分之一秒)

因此:尝试针对端点进行现有代码,该端点已记录为已启用集成,您可能会发现它可行.

请记住,您的第一个请求应该是使用标题Authorization:Bearer $JWT进行GET / integration /安装

然后从响应中读取access_tokens_url,并使用与上一个请求相同的授权标头POST到该URL.
(如果您的集成已启用多个安装,那么您将拥有多个URL)

然后,您将在响应中获得一个令牌字段,此时您应该修改标题,以便它的授权:令牌$TOKEN.

您还会获得使用访问令牌发回的到期时间,您应该存储该访问令牌,以便在必要时重新进行身份验证.

标签:php,authentication,rsa,jwt,github-api
来源: https://codeday.me/bug/20190623/1267533.html