public void CreateToken_ValidTokenCreated_ReturnTrue()
        {
            // Arrange
            string expectedToken = "eyJhbGciOiJTSEEyNTYiLCJ0eXAiOiJKV1QifQ" +
                                   ".eyJ1c2VyIjoidGVzdCIsImNsYWltIjoiW2JlYWNoLCBibHVlLCB0ZXN0XSJ9" +
                                   ".pIJZEFcp5o9T9pzPqYZHXvQyt61RuZmNMdxbkmue3VY";
            Dictionary <string, string> testPayload = new Dictionary <string, string>()
            {
                { "user", "test" },
                { "claim", "[beach, blue, test]" }
            };
            JWTokenManager jwtManager = new JWTokenManager();

            // Act
            string actualToken = jwtManager.CreateToken(testPayload);

            // Assert
            Assert.Equal(expectedToken, actualToken);
        }
        public void CreateToken_NullPayloadPassed_ThrowArgumentNullException()
        {
            // Arrange
            bool expected = true;
            Dictionary <string, string> testPayload = null;
            JWTokenManager jwtManager = new JWTokenManager();
            bool           actual     = false;

            // Act
            try
            {
                string token = jwtManager.CreateToken(testPayload);
            }
            catch (ArgumentNullException)
            {
                actual = true;
            }

            // Assert
            Assert.Equal(expected, actual);
        }
Пример #3
0
        static void Main(string[] args)
        {
            CreateUsers();
            JWTokenManager tm = new JWTokenManager();
            Dictionary <string, string> testPayload = new Dictionary <string, string>()
            {
                { "user", "*****@*****.**" },
                { "claim", "[Post, Delete, Edit]" }
            };

            string token = tm.CreateToken(testPayload);

            Console.Out.WriteLine(token);
            Dictionary <string, string> decodedPayload = tm.DecodePayload(token);
            // Doesn't work for dictionary
            bool equalPayloads = testPayload.Equals(decodedPayload);

            Console.Out.WriteLine(equalPayloads);


            //Dictionary<string, string> test = new Dictionary<string, string>()
            //{
            //    { "fed", "food" },
            //    { "blue", "23" },
            //    { "cred", "43" }
            //};

            //test["c"] = "New 3";

            //CreateUsers();

            //var um = new UserManager();

            //User user = um.FindByUserName("*****@*****.**");
            //SessionManager sm = new SessionManager();
            //JWTokenManager tm = new JWTokenManager();
            //String token = sm.CreateSession(user.Id);
            //sm.InvalidateSession(token);
            //token = sm.CreateSession(user.Id);
            //Console.Out.WriteLine(token);
            //Console.Out.WriteLine("Attempting to validate token");
            //Dictionary<string, string> payload = null;
            //if (sm.ValidateSession(token))
            //{
            //    Console.Out.WriteLine("Getting payload");
            //    payload = tm.DecodePayload(token);
            //    Console.Out.WriteLine(payload.ToString());
            //}

            //if (sm.ValidateSession("FakeToken"))
            //{
            //    Console.Out.WriteLine("Error: FakeToken isn't a real token.");

            //}
            //else
            //{
            //    Console.Out.WriteLine("Correct: FakeToken wasn't valid.");
            //}

            //System.Threading.Thread.Sleep(50000);

            //if (!sm.ValidateSession(token))
            //{
            //    Console.Out.WriteLine("Token is now invalid. Good.");
            //}
            //else
            //{
            //    Console.Out.WriteLine("Error: Token should be invalid.");
            //}
            //string newToken = sm.RefreshSession(token, payload);
            //if (sm.ValidateSession(newToken))
            //{
            //    Console.Out.WriteLine("Good! The refresh worked!");
            //}
            //else
            //{
            //    Console.Out.WriteLine("Something is wrong with refresh.");
            //}

            //sm.InvalidateSession(newToken);
            //if (sm.ValidateSession(newToken))
            //{
            //    Console.Out.WriteLine("Something is wrong. Token should have been deleted.");
            //}
            //else
            //{
            //    Console.Out.WriteLine("The token is invalid as it should be. It was deleted after all.");
            //}

            Console.In.Read();
            Console.Out.WriteLine("Ending program.");
        }