Beispiel #1
0
        public void VerifyNbfUtf8()
        {
            var key     = LitJWT.Algorithms.HS256Algorithm.GenerateRandomRecommendedKey();
            var encoder = new JwtEncoder(new LitJWT.Algorithms.HS256Algorithm(key));
            var decoder = new JwtDecoder(new LitJWT.Algorithms.HS256Algorithm(key));

            {
                var payload = new PayloadNbf {
                    Bar = 1, Foo = "foo", nbf = (DateTimeOffset.UtcNow + TimeSpan.FromSeconds(10)).ToUnixTimeSeconds()
                };
                var result = encoder.EncodeAsUtf8Bytes(payload, null, (x, writer) => writer.Write(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(x))));


                var decodeResult = decoder.TryDecode(result, x => JsonConvert.DeserializeObject <Payload>(Encoding.UTF8.GetString(x)), out var decodedPayload);

                decodeResult.Should().Be(DecodeResult.FailedVerifyNotBefore);
            }
            {
                var payload = new PayloadNbf {
                    Bar = 1, Foo = "foo", nbf = (DateTimeOffset.UtcNow - TimeSpan.FromSeconds(10)).ToUnixTimeSeconds()
                };
                var result = encoder.EncodeAsUtf8Bytes(payload, null, (x, writer) => writer.Write(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(x))));


                var decodeResult = decoder.TryDecode(result, x => JsonConvert.DeserializeObject <Payload>(Encoding.UTF8.GetString(x)), out var decodedPayload);

                decodeResult.Should().Be(DecodeResult.Success);
            }
        }
Beispiel #2
0
        public void ThreadSafeDecode()
        {
            var payload = FixtureFactory.Create <Payload>();
            var key     = LitJWT.Algorithms.HS256Algorithm.GenerateRandomRecommendedKey();
            var encoder = new JwtEncoder(new LitJWT.Algorithms.HS256Algorithm(key));
            var decoder = new JwtDecoder(encoder.SignAlgorithm);

            var result = encoder.EncodeAsUtf8Bytes(payload, null, (x, writer) => writer.Write(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(x))));

            void TestFunc()
            {
                var decodeResult = decoder.TryDecode(result,
                                                     x => JsonConvert.DeserializeObject <Payload>(Encoding.UTF8.GetString(x)), out var decodedPayload);

                decodeResult.Should().Be(DecodeResult.Success);
            }

            var testRuns = new List <Task>();

            for (int i = 0; i < 100; i++)
            {
                testRuns.Add(Task.Run(TestFunc));
            }

            Task.WaitAll(testRuns.ToArray());
        }
Beispiel #3
0
        public void StandardDecodeUtf8()
        {
            foreach (var payload in FixtureFactory.CreateMany <Payload>(99))
            {
                var key     = LitJWT.Algorithms.HS256Algorithm.GenerateRandomRecommendedKey();
                var encoder = new JwtEncoder(new LitJWT.Algorithms.HS256Algorithm(key));

                var result = encoder.EncodeAsUtf8Bytes(payload, null, (x, writer) => writer.Write(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(x))));

                var decoder = new JwtDecoder(new LitJWT.Algorithms.HS256Algorithm(key));

                var decodeResult = decoder.TryDecode(result, x => JsonConvert.DeserializeObject <Payload>(Encoding.UTF8.GetString(x)), out var decodedPayload);

                decodeResult.Should().Be(DecodeResult.Success);
                decodedPayload.Foo.Should().Be(payload.Foo);
                decodedPayload.Bar.Should().Be(payload.Bar);
            }
        }
Beispiel #4
0
        public void PayloadJson()
        {
            var key     = LitJWT.Algorithms.HS256Algorithm.GenerateRandomRecommendedKey();
            var encoder = new JwtEncoder(new LitJWT.Algorithms.HS256Algorithm(key));
            var decoder = new JwtDecoder(new LitJWT.Algorithms.HS256Algorithm(key));

            foreach (var payload in FixtureFactory.CreateMany <Payload>(99))
            {
                {
                    var result = encoder.EncodeAsUtf8Bytes(payload, null, (x, writer) => writer.Write(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(x))));
                    var json   = decoder.GetPayloadJson(result);
                    json.Should().Be(JsonConvert.SerializeObject(payload));
                }

                {
                    var result = encoder.Encode(payload, null, (x, writer) => writer.Write(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(x))));
                    var json   = decoder.GetPayloadJson(result);
                    json.Should().Be(JsonConvert.SerializeObject(payload));
                }
            }
        }
Beispiel #5
0
        public void FailUtf8()
        {
            var payload = FixtureFactory.Create <Payload>();
            var key     = LitJWT.Algorithms.HS256Algorithm.GenerateRandomRecommendedKey();
            var encoder = new JwtEncoder(new LitJWT.Algorithms.HS256Algorithm(key));

            var result = encoder.EncodeAsUtf8Bytes(payload, null, (x, writer) => writer.Write(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(x))));

            var decoder = new JwtDecoder(new LitJWT.Algorithms.HS256Algorithm(key));

            {
                var span = result.ToArray().AsSpan();
                span[4] = (byte)'?';

                var decodeResult = decoder.TryDecode(span, x => JsonConvert.DeserializeObject <Payload>(Encoding.UTF8.GetString(x)), out var decodedPayload);

                decodeResult.Should().Be(DecodeResult.InvalidBase64UrlHeader);
            }
            {
                var span = result.ToArray().AsSpan();

                var decoder2     = new JwtDecoder(new LitJWT.Algorithms.HS384Algorithm(key));
                var decodeResult = decoder2.TryDecode(span, x => JsonConvert.DeserializeObject <Payload>(Encoding.UTF8.GetString(x)), out var decodedPayload);

                decodeResult.Should().Be(DecodeResult.AlgorithmNotExists);
            }
            {
                var span = result.ToArray().AsSpan();
                span[span.Length - 10] = (byte)'A';
                span[span.Length - 11] = (byte)'B';
                span[span.Length - 12] = (byte)'C'; // maybe break signature

                var decodeResult = decoder.TryDecode(span, x => JsonConvert.DeserializeObject <Payload>(Encoding.UTF8.GetString(x)), out var decodedPayload);

                decodeResult.Should().Be(DecodeResult.FailedVerifySignature);
            }
        }