public void ShouldCreateAToken()
        {
            // arrange
            var value   = "some value";
            var encoder = new JwtTokenService("this is a secret");

            // act
            var token = encoder.Encode(value, "http://my.website.com", "http://my.tokenissuer.com");

            // assert
            Assert.That(token, Is.Not.Empty);
        }
        public void ShouldFailToDecodeIfAudienceIsInvalid()
        {
            // arrange
            var value   = "some value";
            var service = new JwtTokenService("this is a secret");
            var token   = service.Encode(value, "http://unknown.website.com", "http://my.tokenissuer.com"); // unknown audience

            // act
            ActualValueDelegate <string> testDelegate = () => service.Decode(token, _validAudiences, _validIssuers);

            // assert
            Assert.That(testDelegate, Throws.TypeOf <SecurityTokenInvalidAudienceException>());
        }
        public void ShouldDecodeAToken()
        {
            // arrange
            var value   = "some value";
            var service = new JwtTokenService("this is a secret");
            var token   = service.Encode(value, "http://my.website.com", "http://my.tokenissuer.com");

            // act
            var decoded = service.Decode(token, _validAudiences, _validIssuers);

            // assert
            Assert.That(decoded, Is.EqualTo(value));
        }
        public void ShouldFailToDecodeIfTokenIsExpired()
        {
            // arrange
            var value   = "some value";
            var service = new JwtTokenService("this is a secret", 0.0d);                                     // no clock skew allowed
            var token   = service.Encode(value, "http://my.website.com", "http://my.tokenissuer.com", 0.1d); // short lifetime

            Thread.Sleep(250);

            // act
            ActualValueDelegate <string> testDelegate = () => service.Decode(token, _validAudiences, _validIssuers);

            // assert
            Assert.That(testDelegate, Throws.TypeOf <SecurityTokenExpiredException>());
        }
        public void ShouldFailToDecodeIfSecretIsInvalid()
        {
            // arrange
            var value   = "some value";
            var encoder = new JwtTokenService("this is a secret");
            var token   = encoder.Encode(value, "http://my.website.com", "http://my.tokenissuer.com");

            // create a separate service for decoding
            var decoder = new JwtTokenService("this is a different secret"); // different secret to encoder

            // act
            ActualValueDelegate <string> testDelegate = () => decoder.Decode(token, _validAudiences, _validIssuers);

            // assert
            Assert.That(testDelegate, Throws.TypeOf <SignatureVerificationFailedException>());
        }
Beispiel #6
0
        private static void Main(string[] args)
        {
            try
            {
                var arguments = Arguments.Parse(args);

                // check for help request
                if (arguments.ContainsKey("?") || arguments.ContainsKey("H") || arguments.ContainsKey("HELP"))
                {
                    ShowHelp();

                    Environment.Exit(1);
                }

                // parse args
                var issuer   = arguments.GetValueOrDefault(string.Empty, "issuer");
                var audience = arguments.GetValueOrDefault(string.Empty, "audience");
                var data     = arguments.GetValueOrDefault(string.Empty, "data");
                var secret   = arguments.GetValueOrDefault(string.Empty, "secret");
                var duration = int.Parse(arguments.GetValueOrDefault("720", "duration"));

                // create token
                var encoder = new JwtTokenService(secret);
                var token   = encoder.Encode(data, audience, issuer, TimeSpan.FromHours(duration).TotalSeconds);

                System.Console.WriteLine(token);

                Environment.Exit(0);
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("ERROR " + ex.FormatException());

                Environment.Exit(1);
            }
        }