Пример #1
0
        public string BuildJwtToken(IConfiguration config, User user, out DateTime expTime)
        {
            var    jwtConfig = config.GetSection("JwtToken");
            string key       = DataProtect.TryUnProtect(jwtConfig.GetValue("Key", AuthOptions.KEY));
            string issuer    = jwtConfig.GetValue("Issuer", AuthOptions.ISSUER);
            string audience  = jwtConfig.GetValue("Audience", AuthOptions.AUDIENCE);
            int    lifetime  = jwtConfig.GetValue("Lifetime", AuthOptions.LIFETIME);
            var    now       = DateTime.UtcNow;

            expTime = now.Add(TimeSpan.FromMinutes(lifetime));

            var claims = new List <Claim>
            {
                new Claim(ClaimsIdentity.DefaultNameClaimType, user.UserID.ToString()),
                new Claim(ClaimsIdentity.DefaultRoleClaimType, user.Role)
            };

            var jwt = new JwtSecurityToken(
                issuer: issuer,
                audience: audience,
                notBefore: now,
                claims: claims,
                expires: expTime,
                signingCredentials: new SigningCredentials(AuthOptions.GetSymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256));

            return(new JwtSecurityTokenHandler().WriteToken(jwt));
        }
Пример #2
0
 private void ConfigureServices(IServiceCollection services)
 {
     services.AddSingleton <IConsole, PxConsole>();
     Pulxer.BL.ConfigureServices(services, _config,
                                 DataProtect.TryUnProtect(_config.GetConnectionString("Pulxer")),
                                 DataProtect.TryUnProtect(_config.GetConnectionString("Leech")));
 }
Пример #3
0
 public void Encrypt_null_argNullExp()
 {
     Assert.ThrowsException <ArgumentNullException>(() =>
     {
         DataProtect.Encrypt(null, null, null);
     });
 }
Пример #4
0
        public void ConfigureServices(IServiceCollection services)
        {
            var    jwtConfig = _config.GetSection("JwtToken");
            string key       = DataProtect.TryUnProtect(jwtConfig.GetValue("Key", AuthOptions.KEY));

            services.AddAuthentication(opt =>
            {
                opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                opt.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(opt =>
            {
                //opt.RequireHttpsMetadata = _environment.IsProduction();
                opt.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer           = true,
                    ValidIssuer              = jwtConfig.GetValue("Issuer", AuthOptions.ISSUER),
                    ValidateAudience         = true,
                    ValidAudience            = jwtConfig.GetValue("Audience", AuthOptions.AUDIENCE),
                    ValidateLifetime         = true,
                    ClockSkew                = TimeSpan.Zero,
                    IssuerSigningKey         = AuthOptions.GetSymmetricSecurityKey(key),
                    ValidateIssuerSigningKey = true
                };
            });

            services.AddControllers();

            string pulxerConnectionString = DataProtect.TryUnProtect(_config.GetConnectionString("Pulxer"));
            string leechConnectionString  = DataProtect.TryUnProtect(_config.GetConnectionString("Leech"));

            Pulxer.BL.ConfigureServices(services, _config, pulxerConnectionString, leechConnectionString);
        }
Пример #5
0
 public void Decrypt_null_argNullExp()
 {
     Assert.ThrowsException <ArgumentNullException>(() =>
     {
         string server;
         string login;
         string password;
         DataProtect.Decrypt(null, out server, out login, out password); // null на входе
     });
 }
Пример #6
0
 public LpClientApp(DataProtect dataProtect, IInstrumTable instrumTable,
                    IAccountTable accountTable, IStopOrderTable stopOrderTable, IOrderTable orderTable, ITradeTable tradeTable,
                    ICashTable positionTable, IHoldingTable holdingTable, ITickDispatcher tickDisp, ILogger logger)
 {
     _dataProtect = dataProtect;
     _socket      = new LpClientSocket();
     _core        = new LpCore(_socket, false); // клиент
     _pipeFactory = new LpAppFactory(_core, instrumTable, accountTable, stopOrderTable, orderTable, tradeTable, positionTable, holdingTable, tickDisp);
     _sysPipe     = new SystemLp(_pipeFactory, _core);
     _logger      = logger;
 }
Пример #7
0
        public void EncryptDecrypt_empty_empty()
        {
            string server; string login; string password;
            string encData   = DataProtect.Encrypt("", "", "");
            bool   isSuccess = DataProtect.Decrypt(encData, out server, out login, out password);

            Assert.IsTrue(isSuccess);
            Assert.AreEqual("", server);
            Assert.AreEqual("", login);
            Assert.AreEqual("", password);
        }
Пример #8
0
        public void EncryptDecrypt_localMachineScope_correctDecrypt()
        {
            string server   = "server.com";
            string login    = "******";
            string password = "******";

            string encData = DataProtect.Encrypt(server, login, password, true);

            string server1; string login1; string password1;
            bool   isSuccess = DataProtect.Decrypt(encData, out server1, out login1, out password1);

            Assert.IsTrue(isSuccess);
            Assert.AreEqual(server, server1);
            Assert.AreEqual(login, login1);
            Assert.AreEqual(password, password1);
        }
Пример #9
0
        public void EncryptDecrypt_correctData_correctDecrypt()
        {
            string server   = "server.com";
            string login    = "******";
            string password = "******";

            string encData = DataProtect.Encrypt(server, login, password);

            string server1; string login1; string password1;
            bool   isSuccess = DataProtect.Decrypt(encData, out server1, out login1, out password1);

            Assert.IsTrue(isSuccess);
            Assert.AreEqual(server, server1);
            Assert.AreEqual(login, login1);
            Assert.AreEqual(password, password1);
        }
Пример #10
0
        public async Task Token()
        {
            var username = Request.Form["username"];
            var password = Request.Form["password"];

            var identity = GetIdentity(username, password);

            if (identity == null)
            {
                Response.StatusCode = 400;
                await Response.WriteAsync("Invalid login or password.");

                return;
            }

            var    jwtConfig = _config.GetSection("JwtToken");
            string key       = DataProtect.TryUnProtect(jwtConfig.GetValue("Key", AuthOptions.KEY));
            string issuer    = jwtConfig.GetValue("Issuer", AuthOptions.ISSUER);
            string audience  = jwtConfig.GetValue("Audience", AuthOptions.AUDIENCE);
            int    lifetime  = jwtConfig.GetValue("Lifetime", AuthOptions.LIFETIME);
            var    now       = DateTime.UtcNow;

            var jwt = new JwtSecurityToken(
                issuer: issuer,
                audience: audience,
                notBefore: now,
                claims: identity.Claims,
                expires: now.Add(TimeSpan.FromMinutes(lifetime)),
                signingCredentials: new SigningCredentials(AuthOptions.GetSymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256));
            var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt);

            var response = new
            {
                access_token = encodedJwt,
                username     = identity.Name
            };

            Response.ContentType = "application/json";
            await Response.WriteAsync(JsonConvert.SerializeObject(response,
                                                                  new JsonSerializerSettings {
                Formatting = Formatting.Indented
            }));
        }
Пример #11
0
        public void Decrypt_incorrectData_false()
        {
            string server;
            string login;
            string password;
            bool   isSuccess;

            isSuccess = DataProtect.Decrypt("", out server, out login, out password); // пустая строка на входе
            Assert.AreEqual(false, isSuccess);
            Assert.AreEqual("", server);
            Assert.AreEqual("", login);
            Assert.AreEqual("", password);

            isSuccess = DataProtect.Decrypt("привет", out server, out login, out password); // не base64 на входе
            Assert.AreEqual(false, isSuccess);
            Assert.AreEqual("", server);
            Assert.AreEqual("", login);
            Assert.AreEqual("", password);
        }
Пример #12
0
        private void FileProtect(List <string> args)
        {
            if (args.Count < 2)
            {
                _console.WriteError("Неверное число аргументов.");
                return;
            }

            bool isLocalMachile = args[1].ToLower().StartsWith('m');

            try
            {
                string resPath = DataProtect.FileProtect(args[0], ".protected", isLocalMachile);
                _console.WriteLine("Сформирован файл:" + resPath);
            }
            catch (Exception ex)
            {
                _console.WriteError(ex.ToString());
            }
        }
Пример #13
0
 ///...
 
     public static ArrivedDetails CreateFromEncryptedKey(string encrypted)
     {
         return DataProtect.deserializeXML(DataProtect.DecryptData(encrypted));
     }