public void DecryptionOnlyDecryptsTokens()
        {
            // Arrange
            const string name   = "name";
            const string url    = "url";
            string       token  = DpApi.Encrypt("the oauth token");
            string       secret = DpApi.Encrypt("the token secret");
            const ulong  userId = 123456;

            var data = new TwitterAccountData
            {
                AccountName      = name,
                ImageUrl         = url,
                IsDefault        = true,
                OAuthToken       = token,
                OAuthTokenSecret = secret,
                RequiresConfirm  = true,
                UserId           = userId
            };

            // Act
            data.Decrypt();

            // Assert
            Assert.AreEqual(name, data.AccountName);
            Assert.AreEqual(url, data.ImageUrl);
            Assert.AreEqual("the oauth token", data.OAuthToken);
            Assert.AreEqual("the token secret", data.OAuthTokenSecret);
            Assert.AreEqual(userId, data.UserId);
            Assert.IsTrue(data.IsDefault);
            Assert.IsTrue(data.RequiresConfirm);
        }
예제 #2
0
 public ScheduleManager(HospitalContext db, DpApi client)
 {
     _db                 = db;
     _client             = client;
     _visitRepository    = new VisitRepository(_db);
     _scheduleRepository = new ScheduleRepository(_db);
 }
예제 #3
0
        public VisitsController()
        {
            _repo = new VisitRepository(_db);
            _doctorFacilityRepo = new DoctorFacilityRepository(_db);
            _scheduleRepo       = new ScheduleRepository(_db);


            _client          = new DpApi(AppSettings.ClientId, AppSettings.ClientSecret, (Locale)AppSettings.Locale);
            _scheduleManager = new ScheduleManager(_db, _client);
            _visitManager    = new VisitManager(_db, _client, _scheduleManager);
        }
예제 #4
0
        public void EncryptingEmptyStringShouldNotBeEmptyString()
        {
            // Arrange
            const string plain = "";

            // Act
            var encrypted = DpApi.Encrypt(plain);

            // Assert
            Assert.NotEmpty(encrypted);
        }
예제 #5
0
        public VisitManager(HospitalContext db, DpApi client, ScheduleManager scheduleManager)
        {
            _db     = db;
            _client = client;

            _mappingRepo     = new DoctorMappingRepository(_db);
            _scheduleRepo    = new ScheduleRepository(_db);
            _visitRepository = new VisitRepository(_db);

            _scheduleManager = new ScheduleManager(_db, _client);
        }
예제 #6
0
 public ScheduleController()
 {
     //Let's be sure that every repo is using the same DbContext
     repo = new ScheduleRepository(context);
     doctorFacilityRepo = new DoctorFacilityRepository(context);
     doctorRepo         = new DoctorRepository(context);
     facilityRepo       = new FacilityRepository(context);
     doctorServiceRepo  = new ForeignDoctorServiceRepository(context);
     client             = new DpApi(AppSettings.ClientId, AppSettings.ClientSecret, (Locale)AppSettings.Locale);
     scheduleManager    = new ScheduleManager(context, client);
 }
예제 #7
0
        public MappingController()
        {
            var db = new HospitalContext();

            client = new DpApi(AppSettings.ClientId, AppSettings.ClientSecret, (Locale)AppSettings.Locale);

            scheduleManager    = new ScheduleManager(db, client);
            repo               = new DoctorMappingRepository(db);
            addressRepo        = new ForeignAddressRepository(db);
            doctorFacilityRepo = new DoctorFacilityRepository(db);
            doctorServiceRepo  = new ForeignDoctorServiceRepository(db);
        }
예제 #8
0
        public void MachineAndUserKeysAreDifferent()
        {
            // Arrange
            const string plain = "Hello World this is a test with some text";

            // Act
            string machine = DpApi.Encrypt(DpApi.KeyType.MachineKey, plain);
            string user    = DpApi.Encrypt(DpApi.KeyType.UserKey, plain);

            // Assert
            Assert.AreNotEqual(machine, user);
        }
예제 #9
0
파일: DpApiTests.cs 프로젝트: zAfLu/Twice
        public void MachineLevelEncryptionCanBeDecrypted()
        {
            // Arrange
            string plain = "Hello World this is a test with some text";

            // Act
            string encrypted = DpApi.Encrypt(plain);
            string decrypted = DpApi.Decrypt(encrypted);

            // Assert
            Assert.AreNotEqual(plain, encrypted);
            Assert.AreEqual(plain, decrypted);
        }
예제 #10
0
        public NotificationHandler(DpApi client, HospitalContext db = null)
        {
            _db = db ?? new HospitalContext();

            _visitRepo    = new VisitRepository(_db);
            _mappingRepo  = new DoctorMappingRepository(_db);
            _scheduleRepo = new ScheduleRepository(_db);

            _client = client;

            _scheduleManager = new ScheduleManager(_db, _client);
            _visitManager    = new VisitManager(_db, _client, _scheduleManager);
        }
예제 #11
0
        public void UserLevelEncryptionCanBeDecrypted()
        {
            // Arrange
            const string plain = "Hello World this is a test with some text";

            // Act
            string encrypted = DpApi.Encrypt(DpApi.KeyType.UserKey, plain);
            string decrypted = DpApi.Decrypt(encrypted);

            // Assert
            Assert.AreNotEqual(plain, encrypted);
            Assert.AreEqual(plain, decrypted);
        }
예제 #12
0
        public void DecryptedActionEncryptsAfterExecution()
        {
            // Arrange
            var data = new TwitterAccountData
            {
                OAuthToken       = DpApi.Encrypt("token"),
                OAuthTokenSecret = DpApi.Encrypt("secret")
            };

            // Act
            data.ExecuteDecryptedAction(d => { });

            // Assert
            Assert.AreNotEqual("token", data.OAuthToken);
            Assert.AreNotEqual("secret", data.OAuthTokenSecret);
        }
예제 #13
0
        public void EncryptedFuncPassesThroughReturnValue()
        {
            // Arrange

            var data = new TwitterAccountData
            {
                OAuthToken       = DpApi.Encrypt("token"),
                OAuthTokenSecret = DpApi.Encrypt("secret")
            };

            Func <TwitterAccountData, int> action = d => 42;

            // Act
            var value = data.ExecuteDecryptedAction(action);

            // Assert
            Assert.AreEqual(42, value);
        }
        public void Execute(IJobExecutionContext context)
        {
            var db     = new HospitalContext();
            var client = new DpApi(AppSettings.ClientId, AppSettings.ClientSecret, (Locale)AppSettings.Locale);

            var scheduleManager = new ScheduleManager(db, client);
            var repo            = new DoctorFacilityRepository(db);

            var mappedDoctors = repo.GetMapped();

            foreach (var item in mappedDoctors)
            {
                bool clearResult = scheduleManager.ClearDPCalendar(item);
                bool pushResult  = scheduleManager.PushSlots(item);

                log.Warn($"Cleaning slot result: {clearResult}");
                log.Warn($"Push slot result: {pushResult}");
            }

            repo.Dispose();
        }
예제 #15
0
        public static void Import()
        {
            string clientId     = AppSettings.ClientId;
            string clientSecret = AppSettings.ClientSecret;
            string locale       = AppSettings.Locale;

            var client     = new DpApi(clientId, clientSecret, (Locale)locale);
            var facilities = client.GetFacilities();

            foreach (var facility in facilities)
            {
                Console.WriteLine($"IMPORTING FACILITY: {facility.Name}\n");
                var foreignFacility = CreateOrUpdateFacility(facility);

                var dpDoctors = client.GetDoctors(facility.Id, true);

                foreach (var doctor in dpDoctors)
                {
                    Console.WriteLine($"-> {doctor.Name} {doctor.Surname} (ID:{doctor.Id})");
                    var foreignDoctor = CreateOrUpdateDoctor(doctor);

                    var specializations = client.GetDoctor(facility.Id, doctor.Id).Specializations;
                    Console.WriteLine("---> IMPORTING SPECIZALIZATIONS");
                    ImportSpecializations(specializations, foreignDoctor);

                    var addresses = client.GetAddresses(facility.Id, doctor.Id);
                    Console.WriteLine("---> IMPORTING ADDRESSES");
                    ImportAddresses(foreignFacility, foreignDoctor, addresses);

                    var doctorServices = client.GetDoctorServices(facility.Id, doctor.Id);
                    Console.WriteLine("---> IMPORTING DOCTOR SERVICES");
                    ImportDoctorServices(foreignDoctor, doctorServices);
                    ClearLeftOverDoctorServices(foreignDoctor, doctorServices);

                    Console.WriteLine("");
                }
            }

            db.SaveChanges();
        }
예제 #16
0
        public void Execute(IJobExecutionContext context)
        {
            DpApi _client = new DpApi(AppSettings.ClientId, AppSettings.ClientSecret, (Locale)AppSettings.Locale);
            var   db      = new HospitalContext();
            NotificationHandler handler = new NotificationHandler(_client, db);

            while (true)
            {
                Notification notification = _client.GetNotification();
                string       json         = JsonConvert.SerializeObject(notification, new JsonSerializerSettings
                {
                    ContractResolver = new SnakeCaseContractResolver(),
                    Formatting       = Formatting.Indented
                });

                if (notification != null && notification.Message == null)
                {
                    try
                    {
                        log.Warn("Parsing notification:");
                        log.Warn(json);
                        handler.HandleNotification(notification);
                    }
                    catch (Exception ex)
                    {
                        var dpEx = new DPNotificationException("Couldn't process the notifiation data", ex);
                        dpEx.Data.Add("notificationData", json);
                        log.Error("Couldn't process the notifiation data", dpEx);
                    }
                }
                else
                {
                    break;
                }
            }
        }
예제 #17
0
        public void EncryptedActionDecryptsBeforeExecution()
        {
            // Arrange
            var data = new TwitterAccountData
            {
                OAuthToken       = DpApi.Encrypt("token"),
                OAuthTokenSecret = DpApi.Encrypt("secret")
            };

            string decryptedToken              = "";
            string decryptedSecret             = "";
            Action <TwitterAccountData> action = d =>
            {
                decryptedToken  = d.OAuthToken;
                decryptedSecret = d.OAuthTokenSecret;
            };

            // Act
            data.ExecuteDecryptedAction(action);

            // Assert
            Assert.AreEqual("token", decryptedToken);
            Assert.AreEqual("secret", decryptedSecret);
        }
예제 #18
0
        private async void BtStart_OnClick(object sender, RoutedEventArgs e)
        {
            if (_selectedFile == null || _selectedFileInfo == null || CoBAlgorithm.SelectedItem == null ||
                string.IsNullOrWhiteSpace(TbFileDestination.Text))
            {
                return;
            }


            BtChooseFile.IsEnabled         = false;
            BtChecksumChooseFile.AllowDrop = false;
            RbEncrypt.IsEnabled            = false;
            RbDecrypt.IsEnabled            = false;
            TbFileDestination.IsEnabled    = false;
            CoBAlgorithm.IsEnabled         = false;
            PbPassword.IsEnabled           = false;
            RbThisAccount.IsEnabled        = false;
            RbThisComputer.IsEnabled       = false;
            BtStart.IsEnabled = false;

            PrBFileEncryption.Value   = 0;
            PrBFileEncryption.Maximum = _selectedFileInfo.Length;
            TblSpeed.Text             = "0 MiB/s";
            TblProgress.Text          = $"0 / {_lengthInMiB} MiB";

            try
            {
                var encrypt = RbEncrypt.IsChecked == true;

                if (CoBAlgorithm.Text == "Windows Data Protection (DPAPI)")
                {
                    PrBFileEncryption.IsIndeterminate = true;

                    var dpApi = new DpApi(_selectedFileInfo, TbFileDestination.Text,
                                          RbThisAccount.IsChecked == true
                            ? DataProtectionScope.CurrentUser
                            : DataProtectionScope.LocalMachine);

                    if (encrypt)
                    {
                        await dpApi.Encrypt();
                    }
                    else
                    {
                        await dpApi.Decrypt();
                    }
                }
                else
                {
                    using (var algorithm = ((Algorithm)CoBAlgorithm.SelectedIndex).GetAlgorithm())
                    {
                        if (encrypt)
                        {
                            var encryption = new Encryption
                            {
                                MainWindow  = this,
                                Algorithm   = algorithm,
                                Source      = _selectedFileInfo,
                                Destination = TbFileDestination.Text,
                                Password    = PbPassword.Password,
                                LengthInMiB = _lengthInMiB
                            };
                            await encryption.Encrypt();
                        }
                        else
                        {
                            var decryption = new Decryption
                            {
                                MainWindow  = this,
                                Algorithm   = algorithm,
                                Source      = _selectedFileInfo,
                                Destination = TbFileDestination.Text,
                                Password    = PbPassword.Password,
                                LengthInMiB = _lengthInMiB
                            };
                            await decryption.Decrypt();
                        }
                    }
                }

                await this.ShowMessageAsync("LCrypt",
                                            string.Format(Localization.SuccessfullyEncrypted, _selectedFileInfo.Name, Path.GetFileName(TbFileDestination.Text), CoBAlgorithm.Text),
                                            MessageDialogStyle.Affirmative, new MetroDialogSettings
                {
                    AffirmativeButtonText = "OK",
                    AnimateShow           = true,
                    AnimateHide           = false
                });
            }
            catch (Exception)
            {
                await this.ShowMessageAsync("LCrypt",
                                            string.Format(Localization.EncryptionDecryptionFailed, _selectedFileInfo.Name),
                                            MessageDialogStyle.Affirmative, new MetroDialogSettings
                {
                    AffirmativeButtonText = "OK",
                    AnimateShow           = true,
                    AnimateHide           = false
                });
            }
            finally
            {
                BtChooseFile.IsEnabled         = true;
                BtChecksumChooseFile.AllowDrop = false;
                RbEncrypt.IsEnabled            = true;
                RbDecrypt.IsEnabled            = true;
                TbFileDestination.IsEnabled    = true;
                CoBAlgorithm.IsEnabled         = true;
                PbPassword.IsEnabled           = true;
                RbThisAccount.IsEnabled        = true;
                RbThisComputer.IsEnabled       = true;
                BtStart.IsEnabled = true;

                PrBFileEncryption.Maximum         = 1;
                PrBFileEncryption.Value           = 0;
                PrBFileEncryption.IsIndeterminate = false;
                TblSpeed.Text    = "- MiB/s";
                TblProgress.Text = "0 / - MiB";
            }
        }
예제 #19
0
 public void Encrypt()
 {
     OAuthToken       = DpApi.Encrypt(DpApi.KeyType.UserKey, OAuthToken);
     OAuthTokenSecret = DpApi.Encrypt(DpApi.KeyType.UserKey, OAuthTokenSecret);
 }
예제 #20
0
 public void Decrypt()
 {
     OAuthToken       = DpApi.Decrypt(OAuthToken);
     OAuthTokenSecret = DpApi.Decrypt(OAuthTokenSecret);
 }
예제 #21
0
        static void Main(string[] args)
        {
            string clientId     = AppSettings.ClientId;
            string clientSecret = AppSettings.ClientSecret;
            string locale       = AppSettings.Locale;
            bool   shouldExit   = false;


            HospitalContext myDb = new HospitalContext();

            var facilityRepo       = new ForeignFacilityRepository(myDb);
            var doctorRepo         = new ForeignDoctorRepository(myDb);
            var addressRepo        = new ForeignAddressRepository(myDb);
            var specializationRepo = new ForeignSpecializationRepository(myDb);
            var doctorServiceRepo  = new ForeignDoctorServiceRepository(myDb);

            var client     = new DpApi(clientId, clientSecret, (Locale)locale);
            var facilities = client.GetFacilities();

            foreach (var facility in facilities)
            {
                Console.WriteLine($"IMPORTING FACILITY: {facility.Name}\n");
                var foreignFacility = new ForeignFacility()
                {
                    Id   = facility.Id,
                    Name = facility.Name
                };

                facilityRepo.InsertOrUpdate(foreignFacility);

                var dpDoctors = new List <DPDoctor>(client.GetDoctors(facility.Id, true));

                Console.WriteLine("DOCTORS:");
                foreach (var dpDoctor in dpDoctors)
                {
                    Console.WriteLine($"-> {dpDoctor.Name} {dpDoctor.Surname} (ID:{dpDoctor.Id})");

                    var foreignDoctor = new ForeignDoctor()
                    {
                        Id      = dpDoctor.Id,
                        Name    = dpDoctor.Name,
                        Surname = dpDoctor.Surname
                    };

                    var tempDoctor = client.GetDoctor(facility.Id, dpDoctor.Id);

                    Console.WriteLine("---> IMPORTING SPECIZALIZATIONS");
                    ImportSpecializations(specializationRepo, tempDoctor, foreignDoctor);


                    var addresses = client.GetAddresses(facility.Id, dpDoctor.Id);
                    Console.WriteLine("---> IMPORTING ADDRESSES");
                    ImportAddresses(addressRepo, dpDoctor, foreignFacility, foreignDoctor, addresses);



                    var doctorServices = client.GetDoctorServices(facility.Id, dpDoctor.Id);
                    Console.WriteLine("---> IMPORTING DOCTOR SERVICES");
                    ImportDoctorServices(doctorServiceRepo, dpDoctor, foreignDoctor, doctorServices);


                    doctorRepo.InsertOrUpdate(foreignDoctor);
                    Console.WriteLine();
                }
            }

            doctorRepo.Save();

            Console.WriteLine("DONE");

            if (shouldExit == false)
            {
                Console.ReadLine();
            }
        }
예제 #22
0
 public NotificationController()
 {
     _client = new DpApi(AppSettings.ClientId, AppSettings.ClientSecret, (Locale)AppSettings.Locale);
 }