Ejemplo n.º 1
0
        public IActionResult GetRequisitionFile(string transID)
        {
            if (HttpContext.Session.GetString(Globals.currentPSPubK) == null || HttpContext.Session.GetString(Globals.currentPAPubK) == null)
            {
                return(RedirectToAction("Login"));
            }
            else
            {
                var result = _bigChainDbService.GetMetaDataAndAssetFromTransactionId <string, double>(transID);
                var patientAgreePrivateKey = HttpContext.Session.GetString(Globals.currentPAPriK);
                var patientSignPublicKey   = HttpContext.Session.GetString(Globals.currentPSPubK);
                if (result.metadata.AccessList.Keys.Contains(patientSignPublicKey))
                {
                    var hashedKey         = result.metadata.AccessList[patientSignPublicKey];
                    var dataDecryptionKey = EncryptionService.getDecryptedEncryptionKey(hashedKey, patientAgreePrivateKey);
                    var data  = EncryptionService.getDecryptedAssetData(result.data.Data, dataDecryptionKey);
                    var asset = JsonConvert.DeserializeObject <TestRequisitionAsset>(data);
                    //get encrypted file from ipfs
                    string encryptedFileData = _bigChainDbService.GetTextFromIPFS(asset.AttachedFile.Data);
                    string fileData          = EncryptionService.getDecryptedAssetData(encryptedFileData, dataDecryptionKey);

                    byte[] fileBytes = Convert.FromBase64String(fileData);
                    return(File(fileBytes, asset.AttachedFile.Type, asset.AttachedFile.Name));
                }
                return(new EmptyResult());
            }
        }
Ejemplo n.º 2
0
        public IActionResult UploadResult(UploadResultViewModel uploadResultViewModel)
        {
            // Get's the Doctor's information for current session
            ViewBag.DoctorName = HttpContext.Session.GetString(Globals.currentUserName);
            var oldViewModel = JsonConvert.DeserializeObject <UploadResultViewModel>(TempData["viewModel"] as string);

            uploadResultViewModel.TestData        = oldViewModel.TestData;
            uploadResultViewModel.PatientAsset    = oldViewModel.PatientAsset;
            uploadResultViewModel.PatientMetadata = oldViewModel.PatientMetadata;
            TempData["viewModel"] = JsonConvert.SerializeObject(uploadResultViewModel);

            if (!ModelState.IsValid)
            {
                return(View(uploadResultViewModel));
            }

            var doctorSignPrivateKey  = HttpContext.Session.GetString(Globals.currentDSPriK);
            var doctorAgreePrivateKey = HttpContext.Session.GetString(Globals.currentDAPriK);
            var doctorSignPublicKey   = EncryptionService.getSignPublicKeyStringFromPrivate(doctorSignPrivateKey);
            var patientSignPublicKey  = HttpContext.Session.GetString(Globals.currentPSPubK);

            var transID         = uploadResultViewModel.TestData.transID;
            var testRequisition = _bigChainDbService.GetMetaDataAndAssetFromTransactionId <string, double>(transID);

            //Check MLT has access to upload data
            if (testRequisition.metadata.AccessList.Keys.Contains(doctorSignPublicKey))
            {
                //Get hash key for requisition. We will use the same key for result
                var hashedKey         = testRequisition.metadata.AccessList[doctorSignPublicKey];
                var dataDecryptionKey = EncryptionService.getDecryptedEncryptionKey(hashedKey, doctorAgreePrivateKey);
                if (uploadResultViewModel.ResultFile != null)
                {
                    var    file             = uploadResultViewModel.ResultFile;
                    string base64FileString = "";

                    // Convert "file" into base64 string "base64FileString" to save into database
                    using (var ms = new MemoryStream())
                    {
                        file.CopyTo(ms);
                        var fileBytes = ms.ToArray();
                        base64FileString = Convert.ToBase64String(fileBytes);
                    }
                    //encrypt file data and save to ipfs
                    var encryptFileData = EncryptionService.getEncryptedAssetDataKey(base64FileString, dataDecryptionKey);
                    var cid             = _bigChainDbService.UploadTextToIPFS(encryptFileData);
                    var resultFile      = new FileData()
                    {
                        Data      = cid,
                        Type      = file.ContentType,
                        Extension = file.ContentType.Split('/').Last(),
                        Name      = file.FileName
                    };
                    //Encrypt the file using the same key
                    var encryptedFile = EncryptionService.getEncryptedAssetDataKey(JsonConvert.SerializeObject(resultFile), dataDecryptionKey);

                    var asset = new AssetSaved <TestResultAsset>
                    {
                        Data = new TestResultAsset
                        {
                            RequisitionAssetID = testRequisition.id,
                            EncryptedResult    = encryptedFile
                        },
                        RandomId = _random.Next(0, 100000),
                        Type     = AssetType.TestResult
                    };
                    //Access is managed by requisition asset
                    var metadata = new MetaDataSaved <double>();
                    metadata.AccessList = new Dictionary <string, string>();

                    _bigChainDbService.SendCreateTransferTransactionToDataBase(asset, metadata, doctorSignPrivateKey, patientSignPublicKey);
                    return(RedirectToAction("PatientRecords"));
                }
                else
                {
                    ModelState.AddModelError("", "Missing test result file.");
                    return(View(uploadResultViewModel));
                }
            }
            else
            {
                ModelState.AddModelError("", "You do not have permission to upload test result.");
                return(View(uploadResultViewModel));
            }
        }
Ejemplo n.º 3
0
        public IActionResult FillPrescription(FillPrescriptionViewModel fillPrescriptionViewModel)
        {
            // Get's the Doctor's information for current session
            ViewBag.DoctorName = HttpContext.Session.GetString(Globals.currentUserName);
            var oldViewModel = JsonConvert.DeserializeObject <FillPrescriptionViewModel>(TempData["viewModel"] as string);

            fillPrescriptionViewModel.PrescriptionData = oldViewModel.PrescriptionData;
            fillPrescriptionViewModel.PatientAsset     = oldViewModel.PatientAsset;
            fillPrescriptionViewModel.PatientMetadata  = oldViewModel.PatientMetadata;
            TempData["viewModel"] = JsonConvert.SerializeObject(fillPrescriptionViewModel);

            if (!ModelState.IsValid)
            {
                return(View(fillPrescriptionViewModel));
            }
            string PHN = HttpContext.Session.GetString(Globals.currentPPHN);
            string patientSignPublicKey = HttpContext.Session.GetString(Globals.currentPSPubK);
            string keyword = fillPrescriptionViewModel.PatientKeyword;

            // 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(fillPrescriptionViewModel));
            }

            // 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(fillPrescriptionViewModel));
            }
            Image fpImg = fpList[0];

            // 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(fillPrescriptionViewModel));
            }

            // 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(fillPrescriptionViewModel));
            }

            var prescriptionData = _bigChainDbService.GetMetaDataAndAssetFromTransactionId <string, PrescriptionMetadata>
                                       (fillPrescriptionViewModel.PrescriptionData.transID);
            var oldMetadata = prescriptionData.metadata;

            if (fillPrescriptionViewModel.PrescriptionData.assetData.EndDate.CompareTo(DateTime.Now) < 0)
            {
                ModelState.AddModelError("", "The Prescription seems to have expired. Cannot fill this prescription.");
                return(View(fillPrescriptionViewModel));
            }

            if (fillPrescriptionViewModel.PrescriptionData.Metadata.RefillRemaining < fillPrescriptionViewModel.QtyFilled)
            {
                ModelState.AddModelError("", "Connot issue more than remaining refills.");
            }

            MetaDataSaved <PrescriptionMetadata> newMetadata = oldMetadata;

            newMetadata.data.LastIssueQty    = fillPrescriptionViewModel.QtyFilled;
            newMetadata.data.LastIssueDate   = DateTime.Now;
            newMetadata.data.RefillRemaining = fillPrescriptionViewModel.PrescriptionData.Metadata.RefillRemaining - fillPrescriptionViewModel.QtyFilled;

            _bigChainDbService.SendTransferTransactionToDataBase <PrescriptionMetadata>(fillPrescriptionViewModel.PrescriptionData.assetID,
                                                                                        newMetadata, patientSignPrivateKey, patientSignPublicKey, fillPrescriptionViewModel.PrescriptionData.transID);
            return(RedirectToAction("PatientRecords"));
        }