Ejemplo n.º 1
0
        public void TestCustomizedJwt()
        {
            var payload = new JsonObjectNode();

            payload.SetValue("abcdefg", "hijklmn");
            payload.SetValue("opqrst", HashUtility.ComputeSHA3512String("uvwxyz"));
            var hash = HashSignatureProvider.CreateHS512("key");
            var jwt  = new JsonWebToken <JsonObjectNode>(payload, hash);

            Assert.AreEqual(true, jwt.CanSign);
            Assert.AreEqual(payload, jwt.Payload);
            Assert.AreEqual(hash.Name, jwt.AlgorithmName);
            var header = jwt.ToAuthenticationHeaderValue();

            Assert.IsTrue(header.Parameter.Length > 10);
            var jwt2 = JsonWebToken <JsonObjectNode> .Parse(header.Parameter, hash);

            Assert.AreEqual(header.Parameter, jwt2.ToEncodedString());
            var parser = new JsonWebToken <JsonObjectNode> .Parser(header.Parameter);

            Assert.AreEqual(header.Parameter, parser.ToString());
        }
Ejemplo n.º 2
0
        public void TestStandardJwt()
        {
            var payload = new JsonWebTokenPayload
            {
                Issuer   = "Someone",
                Subject  = "Local Test Auth",
                Audience = new[] { "BackupOperator", "PowerUser" }
            };
            var hash = HashSignatureProvider.CreateHS512("key");
            var jwt  = new JsonWebToken <JsonWebTokenPayload>(payload, hash);

            Assert.AreEqual(true, jwt.CanSign);
            Assert.AreEqual(payload, jwt.Payload);
            Assert.AreEqual(hash.Name, jwt.AlgorithmName);
            var header = jwt.ToAuthenticationHeaderValue();

            Assert.IsTrue(header.Parameter.Length > 10);
            var jwt2 = JsonWebToken <JsonWebTokenPayload> .Parse(header.Parameter, hash);

            Assert.AreEqual(header.Parameter, jwt2.ToEncodedString());
            var parser = new JsonWebToken <JsonWebTokenPayload> .Parser(header.Parameter);

            Assert.AreEqual(header.Parameter, parser.ToString());
        }
Ejemplo n.º 3
0
        protected override async Task OnProcessAsync(CancellationToken cancellationToken = default)
        {
            var codeTokenReq = new CodeTokenRequest(new CodeTokenRequestBody
            {
                Code = "hijklmn\r\nopq\trst"
            }, "abcd", "efg")
            {
                ScopeString = "test plain"
            };
            await Task.Run(() => { }, cancellationToken);

            var tokenUrl = codeTokenReq.ToJsonString();

            codeTokenReq = CodeTokenRequest.Parse(tokenUrl);
            tokenUrl     = codeTokenReq.ToQueryData().ToString();
            codeTokenReq = CodeTokenRequest.Parse(tokenUrl);
            tokenUrl     = codeTokenReq.ToJsonString();
            codeTokenReq = CodeTokenRequest.Parse(tokenUrl);
            Console.WriteLine(codeTokenReq.ToQueryData().ToString());
            Console.WriteLine();

            // JWT HS512
            var hs  = HashSignatureProvider.CreateHS512("a secret string");
            var jwt = new JsonWebToken <Net.HttpClientVerb.NameAndDescription>(new Net.HttpClientVerb.NameAndDescription
            {
                Name        = "abcd",
                Description = "efg"
            }, hs);
            var header = jwt.ToAuthenticationHeaderValue();

            jwt = JsonWebToken <Net.HttpClientVerb.NameAndDescription> .Parse(header.ToString(), hs);

            var jwtStr = jwt.ToEncodedString();

            Console.WriteLine(jwtStr != header.Parameter ? "Failed JWT HS512 testing." : jwtStr);
            Console.WriteLine();

            // RSA.
            var rsa        = RSA.Create();
            var privateKey = rsa.ExportParameters(true).ToPrivatePEMString(true);

            Console.WriteLine(privateKey);
            var publicKey = rsa.ExportParameters(false).ToPublicPEMString();

            Console.WriteLine(publicKey);
            var privateKeyP = RSAParametersConvert.Parse(privateKey).Value;
            var privateKeyS = privateKeyP.ToPrivatePEMString(true);
            var publicKeyP  = RSAParametersConvert.Parse(publicKey).Value;
            var publicKeyS  = publicKeyP.ToPublicPEMString();

            Console.WriteLine("They are {0}.", (privateKey == privateKeyS) && (publicKey == publicKeyS) ? "same" : "different");
            Console.WriteLine();

            // JWT RS512
            var rs = RSASignatureProvider.CreateRS512(rsa);

            jwt    = new JsonWebToken <Net.HttpClientVerb.NameAndDescription>(jwt.Payload, rs);
            header = jwt.ToAuthenticationHeaderValue();
            jwt    = JsonWebToken <Net.HttpClientVerb.NameAndDescription> .Parse(header.ToString(), rs);

            jwtStr = jwt.ToEncodedString();
            Console.WriteLine(jwtStr != header.Parameter ? "Failed JWT RS512 testing." : header.Parameter);
            Console.WriteLine();
        }