public void PlainTextSecretTest()
 {
     using (JwtServiceArgs args = new JwtServiceArgs
     {
         Secret = _secret
     })
         using (JwtService service = new JwtService(args))
         {
             PropertyInfo prop = typeof(JwtService)
                                 .GetProperty("Secret", BindingFlags.NonPublic | BindingFlags.Instance);
             Assert.Equal(_secret, prop.GetValue(service));
         }
 }
Exemple #2
0
        public void InitializerPropertiesTest()
        {
            Mock <IJsonSerializer>   mSerializer   = new Mock <IJsonSerializer>();
            IJsonSerializer          serializer    = mSerializer.Object;
            Mock <IDateTimeProvider> mProvider     = new Mock <IDateTimeProvider>();
            IDateTimeProvider        provider      = mProvider.Object;
            Mock <IJwtValidator>     mValidator    = new Mock <IJwtValidator>();
            IJwtValidator            validator     = mValidator.Object;
            Mock <IBase64UrlEncoder> mUrlEncoder   = new Mock <IBase64UrlEncoder>();
            IBase64UrlEncoder        urlEncoder    = mUrlEncoder.Object;
            Mock <IJwtDecoder>       mDecoder      = new Mock <IJwtDecoder>();
            IJwtDecoder            decoder         = mDecoder.Object;
            Mock <IJwtAlgorithm>   mAlgorithm      = new Mock <IJwtAlgorithm>();
            IJwtAlgorithm          algorithm       = mAlgorithm.Object;
            Mock <IJwtEncoder>     mEncoder        = new Mock <IJwtEncoder>();
            IJwtEncoder            encoder         = mEncoder.Object;
            Mock <ILoggingService> mLoggingService = new Mock <ILoggingService>();
            ILoggingService        loggingService  = mLoggingService.Object;

            using (SecureString secret = _secret.ToSecureString())
                using (JwtServiceArgs args = new JwtServiceArgs(false, null, null)
                {
                    Secret = _secret,
                    SecureSecret = secret,
                    Serializer = serializer,
                    Provider = provider,
                    Validator = validator,
                    UrlEncoder = urlEncoder,
                    Decoder = decoder,
                    Algorithm = algorithm,
                    Encoder = encoder,
                    LoggingService = loggingService
                })
                {
                    Assert.Equal(_secret, args.Secret);
                    Assert.Equal(secret, args.SecureSecret);
                    Assert.Equal(_secret, args.SecureSecret.ToPlainText());
                    Assert.Equal(serializer, args.Serializer);
                    Assert.Equal(provider, args.Provider);
                    Assert.Equal(validator, args.Validator);
                    Assert.Equal(urlEncoder, args.UrlEncoder);
                    Assert.Equal(decoder, args.Decoder);
                    Assert.Equal(algorithm, args.Algorithm);
                    Assert.Equal(encoder, args.Encoder);
                    Assert.Equal(loggingService, args.LoggingService);
                }
        }
 public void EnvironmentalVariableSecretTest()
 {
     try
     {
         Environment.SetEnvironmentVariable("JWT_SECRET", _secret);
         using (JwtServiceArgs args = new JwtServiceArgs())
             using (JwtService service = new JwtService(args))
             {
                 PropertyInfo prop = typeof(JwtService)
                                     .GetProperty("Secret", BindingFlags.NonPublic | BindingFlags.Instance);
                 Assert.Equal(_secret, prop.GetValue(service));
             }
     }
     finally
     {
         Environment.SetEnvironmentVariable("JWT_SECRET", null);
     }
 }
        public void EncodeJwtPayloadTest()
        {
            using (JwtServiceArgs args = new JwtServiceArgs
            {
                Secret = _secret
            })
                using (JwtService service = new JwtService(args))
                {
                    Foobar foobar = new Foobar
                    {
                        Bar = 4,
                        Foo = 1
                    };
                    string expected = args.Encoder.Encode(foobar, _secret);

                    Assert.Equal(expected, service.EncodeJwtPayload(foobar));
                }
        }
Exemple #5
0
        public void ConstructorPropertiesTest()
        {
            Mock <ILoggingService> mLoggingService = new Mock <ILoggingService>();
            ILoggingService        loggingService  = mLoggingService.Object;

            using (SecureString secret = _secret.ToSecureString())
                using (JwtServiceArgs args = new JwtServiceArgs(false, loggingService, secret))
                {
                    Assert.Null(args.Secret);
                    Assert.Equal(secret, args.SecureSecret);
                    Assert.Equal(_secret, args.SecureSecret.ToPlainText());
                    Assert.Null(args.Serializer);
                    Assert.Null(args.Provider);
                    Assert.Null(args.Validator);
                    Assert.Null(args.UrlEncoder);
                    Assert.Null(args.Decoder);
                    Assert.Null(args.Algorithm);
                    Assert.Null(args.Encoder);
                    Assert.Equal(loggingService, args.LoggingService);
                }
        }
        public void DecodeJwtPayloadTest()
        {
            using (JwtServiceArgs args = new JwtServiceArgs
            {
                Secret = _secret
            })
                using (JwtService service = new JwtService(args))
                {
                    Foobar foobar = new Foobar
                    {
                        Bar = 4,
                        Foo = 1
                    };
                    string baseString = args.Encoder.Encode(foobar, _secret);
                    Foobar expected   = args.Decoder.DecodeToObject <Foobar>(baseString, _secret, true);
                    Foobar actual     = service.DecodeJwtPayload <Foobar>(baseString);

                    Assert.NotEqual(expected, actual);
                    Assert.Equal(expected.Bar, actual.Bar);
                    Assert.Equal(expected.Foo, actual.Foo);
                    Assert.Equal(0, expected.Bar);
                }
        }
Exemple #7
0
 public void DefaultPropertiesTest()
 {
     try
     {
         Environment.SetEnvironmentVariable("JWT_SECRET", _secret);
         using (JwtServiceArgs args = new JwtServiceArgs())
         {
             Assert.Null(args.SecureSecret);
             Assert.Equal(_secret, args.Secret);
             Assert.NotNull(args.Serializer);
             Assert.NotNull(args.Provider);
             Assert.NotNull(args.Validator);
             Assert.NotNull(args.UrlEncoder);
             Assert.NotNull(args.Decoder);
             Assert.NotNull(args.Algorithm);
             Assert.NotNull(args.Encoder);
             Assert.Null(args.LoggingService);
         }
     }
     finally
     {
         Environment.SetEnvironmentVariable("JWT_SECRET", null);
     }
 }
        public void PropertiesTest()
        {
            using (JwtServiceArgs args = new JwtServiceArgs
            {
                Secret = _secret
            })
                using (JwtService service = new JwtService(args))
                {
                    PropertyInfo prop = typeof(JwtService)
                                        .GetProperty("Secret", BindingFlags.NonPublic | BindingFlags.Instance);
                    Assert.Equal(_secret, prop.GetValue(service));

                    FieldInfo field = typeof(JwtService)
                                      .GetField("_secureSecret", BindingFlags.NonPublic | BindingFlags.Instance);
                    Assert.Equal(_secret, ((SecureString)field.GetValue(service)).ToPlainText());

                    prop = typeof(JwtService)
                           .GetProperty("_args", BindingFlags.NonPublic | BindingFlags.Instance);
                    Assert.Equal(args, prop.GetValue(service));
                    Assert.NotNull(prop.GetValue(service));

                    prop = typeof(JwtService)
                           .GetProperty("Serializer", BindingFlags.NonPublic | BindingFlags.Instance);
                    Assert.Equal(args.Serializer, prop.GetValue(service));
                    Assert.NotNull(prop.GetValue(service));

                    prop = typeof(JwtService)
                           .GetProperty("Provider", BindingFlags.NonPublic | BindingFlags.Instance);
                    Assert.Equal(args.Provider, prop.GetValue(service));
                    Assert.NotNull(prop.GetValue(service));

                    prop = typeof(JwtService)
                           .GetProperty("Validator", BindingFlags.NonPublic | BindingFlags.Instance);
                    Assert.Equal(args.Validator, prop.GetValue(service));
                    Assert.NotNull(prop.GetValue(service));

                    prop = typeof(JwtService)
                           .GetProperty("UrlEncoder", BindingFlags.NonPublic | BindingFlags.Instance);
                    Assert.Equal(args.UrlEncoder, prop.GetValue(service));
                    Assert.NotNull(prop.GetValue(service));

                    prop = typeof(JwtService)
                           .GetProperty("Decoder", BindingFlags.NonPublic | BindingFlags.Instance);
                    Assert.Equal(args.Decoder, prop.GetValue(service));
                    Assert.NotNull(prop.GetValue(service));

                    prop = typeof(JwtService)
                           .GetProperty("Algorithm", BindingFlags.NonPublic | BindingFlags.Instance);
                    Assert.Equal(args.Algorithm, prop.GetValue(service));
                    Assert.NotNull(prop.GetValue(service));

                    prop = typeof(JwtService)
                           .GetProperty("Encoder", BindingFlags.NonPublic | BindingFlags.Instance);
                    Assert.Equal(args.Encoder, prop.GetValue(service));
                    Assert.NotNull(prop.GetValue(service));

                    prop = typeof(JwtService)
                           .GetProperty("LoggingService", BindingFlags.NonPublic | BindingFlags.Instance);
                    // in this case both will be null
                    Assert.Equal(args.LoggingService, prop.GetValue(service));
                    Assert.Null(prop.GetValue(service));
                }
        }