JWT令牌签名在jwt.io中起作用,但在我的代码中不起作用

问题描述 投票:0回答:1

[尝试使用jose-jwt签名JWT令牌,但由于某些原因,我很痛苦地无法理解为什么它无法按预期工作。

签名部分正在我的测试中工作,但是如果我使用它来请求服务器资源,它将失败。但是,如果我将其粘贴到jwt.io并粘贴我的私钥,则当我尝试使用此新令牌请求服务器资源时,它可以完美工作

因此,即使它似乎可以在我的.net代码库中运行,也实际上并未在令牌上签名。

我编写了很少的测试,它确实可以在签名和验证方面正常工作,没有任何问题。 gist

public class TestUsingJoseJwt
{
    public string Identifier { get; set; }
    public string Nonce { get; set; }
    public JsonNetSerializer JsonNetSerializer { get; set; }

    [SetUp]
    public void SetupTest()
    {
        Identifier = Guid.NewGuid().ToString();
        Nonce = Guid.NewGuid().ToString();
        JsonNetSerializer = new JsonNetSerializer();
    }

    [Test]
    public void CanGenerateSignedJwtUsingJose()
    {
        // Get the private/ public keys
        var privateKeyTexts = File.ReadAllText("FULL PATH TO MY PRIVATE KEY");
        var publicKeyTexts = File.ReadAllText("FULL PATH TO MY PUBLIC KEY");

        var signedClaim = CreateJwtToken("AAC_1c0b51b0-d673-4872-898e-ce9d6d3f0482", Identifier, Nonce);

        var token = CreateToken(signedClaim, privateKeyTexts);
        var payload = DecodeToken(token, publicKeyTexts);

        var expected = JsonNetSerializer.Serialize(signedClaim);

        payload.Should()
            .Be(expected, "provided object should be correctly serialized in the token");
    }

    public static string CreateToken(SignedClaim signedClaim, string privateRsaKey)
    {
        RSAParameters rsaParams;
        using (var tr = new StringReader(privateRsaKey))
        {
            var pemReader = new PemReader(tr);
            var keyPair = pemReader.ReadObject() as AsymmetricCipherKeyPair;
            if (keyPair == null)
            {
                throw new Exception("Could not read RSA private key");
            }
            var privateRsaParams = keyPair.Private as RsaPrivateCrtKeyParameters;
            rsaParams = DotNetUtilities.ToRSAParameters(privateRsaParams);
        }
        using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
        {
            rsa.ImportParameters(rsaParams);
            //Dictionary<string, object> payload = claims.ToDictionary(k => k.Type, v => (object)v.Value);
            var payload = JsonConvert.SerializeObject(signedClaim);
            return Jose.JWT.Encode(payload, rsa, JwsAlgorithm.RS256);
        }
    }

    public string DecodeToken(string token, string publicRsaKey)
    {
        RSAParameters rsaParams;

        using (var tr = new StringReader(publicRsaKey))
        {
            var pemReader = new PemReader(tr);
            var publicKeyParams = pemReader.ReadObject() as RsaKeyParameters;
            if (publicKeyParams == null)
            {
                throw new Exception("Could not read RSA public key");
            }
            rsaParams = DotNetUtilities.ToRSAParameters(publicKeyParams);
        }
        using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
        {
            rsa.ImportParameters(rsaParams);
            // This will throw if the signature is invalid
            return Jose.JWT.Decode(token, rsa, JwsAlgorithm.RS256);
        }
    }

    public SignedClaim CreateJwtToken(string consentId, string identifier, string nonce)
    {
        return new SignedClaim
        {
            Aud = "https://sandbox.api.endpoint",
            Scope = "openid accounts",
            Iss = "bdn-8Zpkl9dCbN2tArm2TrgretrtKSexT2XxQ26uL8B",
            ResponseType = "code id_token",
            RedirectUri = "TEST_REDIRECT_URL",
            State = identifier,
            Exp = DateTimeOffset.UtcNow.AddMinutes(10).ToUnixTimeSeconds(),
            Nonce = nonce,
            ClientId = "bdn-8Zpkl9dCbN2tAeerertreterexT2XxQ26uL8B",
            Claims = new Claims
            {
                IdToken = new IdToken
                {
                    Acr = new Acr
                    {
                        Essential = true,
                        Value = "urn:openbanking:psd2:sca",
                    },
                    OpenbankingIntentId = new Acr
                    {
                        Essential = true,
                        Value = consentId,
                    },
                },
                Userinfo = new Userinfo
                {
                    OpenbankingIntentId = new Acr
                    {
                        Value = consentId,
                        Essential = true,
                    },
                },
            },
        };
    }
}

我几乎比以前更加困惑:((在我手动复制/粘贴jwt.io中的私钥但代码无法签名时,谁能阐明它为什么起作用?)请给令牌吗?

这是示例令牌请求

eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJodHRwczovL3Nhbm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BpnTzKQ7a3RPsL_rJUc9IbXOgXBoF-OmlAFNu8F4l5gMmrL146WSSytkUBVQQhDxxc0JQHcz3TqbapxcWSuchu0-nGR8cIXimvQKyaWpG0OISUA-4H5dH3GHNbxOBDT7wTKYLzdG5tj_d9beQ

[尝试使用jose-jwt签名JWT令牌,但由于某些原因,我很痛苦地无法理解为什么它无法按预期工作。签名部分正在我的测试中工作,但是如果我将其用于...

.net security jwt public-key-encryption open-banking
1个回答
0
投票

结果是让事情弄乱的是Json序列化。

© www.soinside.com 2019 - 2024. All rights reserved.