验证Auth0 JWT抛出无效的算法 [英] Verifying Auth0 JWT throws invalid algorigthm
问题描述
我已经创建了Auth0客户端,我正在登录并收到此令牌:
I have created an Auth0 client, I am logging in and receive this token:
eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik1rVkdOa1l5T1VaQ1JqTkRSVE5EUmtNeU5rVkROMEUyUTBVMFJrVXdPVEZEUkVVNU5UQXpOZyJ9.eyJpc3MiOiJodHRwczovL3RvdGFsY29tbW56LmF1LmF1dGgwLmNvbS8iLCJzdWIiOiJnb29nbGUtb2F1dGgyfDEwMzI5NzA4OTYyMTk5NjUwMjY2MiIsImF1ZCI6ImxTWUtXMUZZdENkMWJLQmdXRWN0MWpCbmtDU3R2dW5SIiwiaWF0IjoxNTA5ODYyMTI1LCJleHAiOjE1MTAyMjIxMjV9.kjmckPxLJ4H9R11XiBBxSNZEvQFVEIgAY_jj2LBy4sEJozBB8ujGE7sq9vEIjMms-Lv2q9WzFQPrqcxyBcYC4Je4QojMgvqLDCodtpot0QUle8QfGmonc1vZYIZyX-wqyOXtRqhoZVEKTeLhm9Le2CV4_a3BwgjkE1LjcDx01GZfsnaId8mh10kGk-DBmr5aVc8MxglLCq5Uk8Zbl2vDc__UMDgx1eQPQg-zve4fUf8zHcxizypYTnF_v0dEAT00L2j5J41SFYdWvP6ReQ3vhVYew2o9iM6u1s75HE-xW8s4pzV4BZAQtgfgIeCd6aVGZs76bcnQXBLej1B7zaPBvA
我现在想要做的是使用 jsonwebtoken 来验证令牌.该令牌使用 RS256 算法签名.
What I am trying to do now is to verify the token using jsonwebtoken. The token is signed with an RS256 algorithm.
我以.pem
的形式下载了签名证书,并且正在成功使用它来验证令牌,如下所示:
I downloaded the signing certificate as a .pem
and I am successfully using it to verify the token like this:
var cert = fs.readFileSync('certificate.pem');
jwt.verify(token, cert, {algorithm: 'RS256'}, (err, decoded) => {
console.log(err)
console.log(decoded)
});
我想做的事不起作用,是使用机密(在Auth0客户端设置中称为 Client Secret ,是一个字符串)来验证令牌.
What I want to do and it doesn't work is verifying the token using the secret (which is called Client Secret in the Auth0 client settings and is a string).
jwt.verify(token, MYSECRET, {algorithm: 'RS256'}, (err, decoded) => {
console.log(err)
console.log(decoded)
});
此代码始终会引发错误:
This code always throws an error:
{ JsonWebTokenError: invalid algorithm
at Object.module.exports [as verify] (C:\code\aws\learn-authorizer\node_modules\jsonwebtoken\verify.js:90:17)
at Object.<anonymous> (C:\code\aws\learn-authorizer\testme.js:25:5)
at Module._compile (module.js:624:30)
at Object.Module._extensions..js (module.js:635:10)
at Module.load (module.js:545:32)
at tryModuleLoad (module.js:508:12)
at Function.Module._load (module.js:500:3)
at Function.Module.runMain (module.js:665:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3 name: 'JsonWebTokenError', message: 'invalid algorithm' }
我的问题是:如何使用密钥而不是使用证书文件来验证RS256令牌? (我也尝试过创建一个使用HS256算法的新客户端,但出现相同的错误).
My question is: how can I verify the RS256 token using the secret key as opposed to using the certificate file? (I also tried creating a new client which is using the HS256 algorithm, but I get the same error).
推荐答案
如果仅使用 密钥,则RS256将不起作用,因为它基于私钥/公钥对.仅使用密钥通常表示H256.在我的回答中,我假设您所说的MYSECRET
只是certificate.pem
的内容.
If you are using only a secret key then using RS256 won't work, as it's based on a private/public key pair. Using only a secret key usually indicates H256. In my answer I assume that what you call MYSECRET
is just the content of certificate.pem
.
无论如何,我认为您的字符串必须包含
Anyways, I would assume your string has to contain
-----BEGIN RSA PRIVATE KEY-----
和
-----END RSA PRIVATE KEY-----
或 PUBLIC ,而不是 PRIVATE .
您可以在来源中看到此内容.错误消息中提到的行包含:
You can see this in source. The lines mentioned in your error message contains:
if (!~options.algorithms.indexOf(header.alg)) {
return done(new JsonWebTokenError('invalid algorithm'));
}
和options.algorithms
定义为
if (!options.algorithms) {
options.algorithms = ~secretOrPublicKey.toString().indexOf('BEGIN CERTIFICATE') ||
~secretOrPublicKey.toString().indexOf('BEGIN PUBLIC KEY') ?
[ 'RS256','RS384','RS512','ES256','ES384','ES512' ] :
~secretOrPublicKey.toString().indexOf('BEGIN RSA PUBLIC KEY') ?
[ 'RS256','RS384','RS512' ] :
[ 'HS256','HS384','HS512' ];
}
如果开始和结束时都没有RSA内容,它将寻找以下算法:'HS256','HS384','HS512'
.
If you don't have the RSA things at the start and end it will look for the following algorithms: 'HS256','HS384','HS512'
.
我以前没有在JWT中使用RS256,但是我在ssh中使用了RS256,我知道它对于拥有标头非常敏感.该字符串必须采用完全正确的格式.
I haven't used RS256 with JWT before, but I have used it with ssh, and I know that it's very sensitive to having the header. The string has to be in the exactly correct format.
这篇关于验证Auth0 JWT抛出无效的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!