Esempio n. 1
0
        public void CreateAccessToken_Tests()
        {
            // Arrange
            var user = new CurrentUser("1", null, null, "Etsoo", 1, IPAddress.Parse("127.0.0.1"), 1, CultureInfo.CurrentCulture, "CN")
            {
                JsonData = "{ body: \"In this scenario, the external client will give you the structure of JWT, normally with custom claims that they expect and provide you with an RSA private key to sign the token. The token will then be used to construct a Uri that will be sent to users and allowing them to invoke the external client endpoints.\" }"
            };

            // Act
            var token = service.CreateAccessToken(user);

            // Assert
            Assert.NotNull(token);

            // Arrange, public key verification
            using var stream = new MemoryStream(Encoding.UTF8.GetBytes(JwtText));
            var section = new ConfigurationBuilder().AddJsonStream(stream).Build().GetSection("Jwt");

            /**
             * , (string token, SecurityToken securityToken, string kid, TokenValidationParameters validationParameters) =>
             * {
             *  return new List<RsaSecurityKey> { };
             * }
             */
            var publicService = new JwtService(new ServiceCollection(), false, section, null);

            // Refresh token
            var refreshToken = service.CreateRefreshToken(new RefreshToken("1", null, IPAddress.Parse("127.0.0.1"), "CN", 1, "service"));

            // Validate refresh token
            var(claimsPrincipal, expired, kid, securityToken) = publicService.ValidateToken(refreshToken);

            Assert.IsFalse(expired);
            Assert.IsNotNull(claimsPrincipal);
            Assert.AreEqual(kid, "service");

            // Public service should not generate token
            Assert.Throws <InvalidOperationException>(() =>
            {
                publicService.CreateAccessToken(user);
            });
        }