Example #1
0
        public IActionResult RequestAccess(RequestAccessViewModel requestAccessViewModel)
        {
            // Description: Authenticates a patient's identity when a Doctor requests access to their medical information
            // Get's the Doctor's information for current session
            ViewBag.DoctorName = HttpContext.Session.GetString(Globals.currentUserName);
            if (!ModelState.IsValid)
            {
                return(View(requestAccessViewModel));
            }
            string PHN = HttpContext.Session.GetString(Globals.currentPPHN);
            string patientSignPublicKey  = HttpContext.Session.GetString(Globals.currentPSPubK);
            string doctorSignPrivatekey  = HttpContext.Session.GetString(Globals.currentDSPriK);
            string doctorSignPublicKey   = EncryptionService.getSignPublicKeyStringFromPrivate(doctorSignPrivatekey);
            string doctorAgreePrivatekey = HttpContext.Session.GetString(Globals.currentDAPriK);
            string doctorAgreePublicKey  = EncryptionService.getAgreePublicKeyStringFromPrivate(doctorAgreePrivatekey);
            string keyword = requestAccessViewModel.keyword;

            // Searches for a patient with the specified PHN
            Assets <UserCredAssetData> userAsset = _bigChainDbService.GetUserAssetFromTypeID(AssetType.Patient, PHN);

            if (userAsset == null)
            {
                ModelState.AddModelError("", "Could not find a patient profile with PHN: " + PHN);
                return(View(requestAccessViewModel));
            }

            // Decrypt the patient's fingerprint data stored in the Blockchain
            byte[]        dbFpData = null;
            string        patientSignPrivateKey, patientAgreePrivateKey;
            List <string> dbList   = userAsset.data.Data.FingerprintData;
            List <Image>  dbfpList = new List <Image>();

            try
            {
                foreach (string db in dbList)
                {
                    EncryptionService.decryptFingerprintData(PHN, keyword, db, out dbFpData);
                    dbfpList.Add(FingerprintService.byteToImg(dbFpData));
                }
                EncryptionService.getPrivateKeyFromIDKeyword(PHN, keyword, userAsset.data.Data.PrivateKeys, out patientSignPrivateKey, out patientAgreePrivateKey);
            }
            catch
            {
                ModelState.AddModelError("", "Keyword may be incorrect");
                return(View(requestAccessViewModel));
            }

            // Send request to the Client Computer to authenticate with fingerprint
            int          numScans = 1;
            List <Image> fpList   = FingerprintService.authenticateFP("24.84.225.22", numScans); // DEBUG: Jacob's Computer

            // Check if fingerprint data is valid
            if (fpList.Count < numScans)
            {
                ModelState.AddModelError("", "Something went wrong with the fingerprint scan, try again.");
                return(View(requestAccessViewModel));
            }
            Image fpImg = fpList[0];

            // Compare the scanned fingerprint with the one saved in the database
            if (!FingerprintService.compareFP(fpImg, dbfpList))
            {
                ModelState.AddModelError("", "The fingerprint did not match, try again.");
                return(View(requestAccessViewModel));
            }

            // Choose the types of records we want to get
            AssetType[] typeList   = { AssetType.TestRequisition };
            var         recordList = _bigChainDbService.GetAllTypeRecordsFromPPublicKey <string>
                                         (typeList, patientSignPublicKey);

            foreach (var record in recordList)
            {
                MetaDataSaved <object> metadata = record.metadata;
                if (!metadata.AccessList.Keys.Contains(doctorSignPublicKey))
                {
                    var hashedKey         = metadata.AccessList[patientSignPublicKey];
                    var dataDecryptionKey = EncryptionService.getDecryptedEncryptionKey(hashedKey, patientAgreePrivateKey);
                    var newHash           = EncryptionService.getEncryptedEncryptionKey(dataDecryptionKey, patientAgreePrivateKey, doctorAgreePublicKey);
                    metadata.AccessList[doctorSignPublicKey] = newHash;
                    _bigChainDbService.SendTransferTransactionToDataBase(record.id, metadata,
                                                                         patientSignPrivateKey, patientSignPublicKey, record.transID);
                }
            }
            return(RedirectToAction("PatientRecords"));
        }
        public IActionResult PatientRecords()
        {
            ViewBag.UserName = HttpContext.Session.GetString(Globals.currentUserName);
            if (HttpContext.Session.GetString(Globals.currentPSPubK) == null || HttpContext.Session.GetString(Globals.currentPAPubK) == null)
            {
                return(RedirectToAction("Login"));
            }
            else
            {
                Assets <PatientCredAssetData> userAsset = _bigChainDbService.GetPatientAssetFromID(HttpContext.Session.GetString(Globals.currentUserID));

                var patientSignPrivateKey  = HttpContext.Session.GetString(Globals.currentPSPriK);
                var patientAgreePrivateKey = HttpContext.Session.GetString(Globals.currentPAPriK);
                var patientSignPublicKey   = HttpContext.Session.GetString(Globals.currentPSPubK);

                PatientCredMetadata userMetadata = _bigChainDbService.GetMetadataFromAssetPublicKey <PatientCredMetadata>(userAsset.id, patientSignPublicKey);

                var doctorNotesList = _bigChainDbService.GetAllTypeRecordsFromPPublicKey <string, double>
                                          (AssetType.DoctorNote, patientSignPublicKey);
                var prescriptionsList = _bigChainDbService.GetAllTypeRecordsFromPPublicKey <string, PrescriptionMetadata>
                                            (AssetType.Prescription, patientSignPublicKey);
                var testRequisitionList = _bigChainDbService.GetAllTypeRecordsFromPPublicKey <string, double>
                                              (AssetType.TestRequisition, patientSignPublicKey);
                Dictionary <string, string> testResults = null;
                if (testRequisitionList.Any())
                {
                    testResults = _bigChainDbService.GetAssociatedTestResults(testRequisitionList);
                }
                var doctorNotes      = new List <DoctorNoteFullData>();
                var prescriptions    = new List <PrescriptionFullData>();
                var testRequisitions = new List <TestRequisitionFullData>();
                foreach (var doctorNote in doctorNotesList)
                {
                    var hashedKey         = doctorNote.metadata.AccessList[patientSignPublicKey];
                    var dataDecryptionKey = EncryptionService.getDecryptedEncryptionKey(hashedKey, patientAgreePrivateKey);
                    var data     = EncryptionService.getDecryptedAssetData(doctorNote.data.Data, dataDecryptionKey);
                    var newEntry = new DoctorNoteFullData
                    {
                        assetData = JsonConvert.DeserializeObject <DoctorNote>(data),
                        Metadata  = doctorNote.metadata.data,
                        assetID   = doctorNote.id,
                        transID   = doctorNote.transID
                    };
                    doctorNotes.Add(newEntry);
                }
                foreach (var prescription in prescriptionsList)
                {
                    var hashedKey         = prescription.metadata.AccessList[patientSignPublicKey];
                    var dataDecryptionKey = EncryptionService.getDecryptedEncryptionKey(hashedKey, patientAgreePrivateKey);
                    var data     = EncryptionService.getDecryptedAssetData(prescription.data.Data, dataDecryptionKey);
                    var newEntry = new PrescriptionFullData
                    {
                        assetData = JsonConvert.DeserializeObject <Prescription>(data),
                        Metadata  = prescription.metadata.data,
                        assetID   = prescription.id,
                        transID   = prescription.transID
                    };
                    prescriptions.Add(newEntry);
                }
                foreach (var testrequisition in testRequisitionList)
                {
                    var hashedKey         = testrequisition.metadata.AccessList[patientSignPublicKey];
                    var dataDecryptionKey = EncryptionService.getDecryptedEncryptionKey(hashedKey, patientAgreePrivateKey);
                    var data     = EncryptionService.getDecryptedAssetData(testrequisition.data.Data, dataDecryptionKey);
                    var newEntry = new TestRequisitionFullData
                    {
                        assetData = JsonConvert.DeserializeObject <TestRequisitionAsset>(data),
                        Metadata  = testrequisition.metadata.data,
                        assetID   = testrequisition.id,
                        transID   = testrequisition.transID
                    };
                    if (testResults != null && testResults.Keys.Contains(testrequisition.id))
                    {
                        var decryptedResultFile = EncryptionService.getDecryptedAssetData(testResults[testrequisition.id], dataDecryptionKey);
                        newEntry.ResultFile = JsonConvert.DeserializeObject <FileData>(decryptedResultFile);
                    }
                    testRequisitions.Add(newEntry);
                }
                var patientInfo = userAsset.data.Data;
                var patientOverviewViewModel = new PatientOverviewViewModel
                {
                    PatientAsset     = patientInfo,
                    PatientMetadata  = userMetadata,
                    PatientAge       = patientInfo.DateOfBirth.CalculateAge(),
                    DoctorNotes      = doctorNotes.OrderByDescending(d => d.assetData.DateOfRecord).ToList(),
                    Prescriptions    = prescriptions.OrderByDescending(p => p.assetData.PrescribingDate).ToList(),
                    TestRequisitions = testRequisitions.OrderByDescending(p => p.assetData.DateOrdered).ToList()
                };

                return(View(patientOverviewViewModel));
            }
        }