Exemplo n.º 1
0
        private OfficeDocumentResponse GetOfficeDocument(string sidDocumentIdValue, string identityToken)
        {
            var documentManagementFactory = new DocumentManagementFactory();
            var officeDocumentStore       = OfficeDocumentStore.Instance();
            var umaResourceId             = officeDocumentStore.GetUmaResourceId(sidDocumentIdValue).Result;

            if (string.IsNullOrWhiteSpace(umaResourceId))
            {
                return(null);
            }

            var grantedToken = officeDocumentStore.GetOfficeDocumentAccessTokenViaUmaGrantType(umaResourceId).Result;

            if (grantedToken == null)
            {
                return(null);
            }

            var getOfficeDocumentResponse = documentManagementFactory.GetOfficeDocumentClient().GetResolve(sidDocumentIdValue, Constants.DocumentApiConfiguration, grantedToken.AccessToken).Result;

            if (getOfficeDocumentResponse.ContainsError)
            {
                return(null);
            }

            return(getOfficeDocumentResponse.OfficeDocument);
        }
 private void HandleRibbonLoad(object sender, RibbonUIEventArgs e)
 {
     DisplayLogin(true);
     _authenticationStore = AuthenticationStore.Instance();
     _officeDocumentStore = OfficeDocumentStore.Instance();
     _authenticationStore.Authenticated += HandleAuthenticate;
     AuthenticationStore.Instance().Restore();
 }
Exemplo n.º 3
0
        private string DecryptOfficeDocument(string sidDocumentIdValue, string identityToken, string content)
        {
            var splittedContent = content.Split('.');

            if (splittedContent.Length != 3)
            {
                return(null);
            }

            var encryptionHelper = new EncryptionHelper();
            var kid                 = splittedContent[0];
            var credentials         = splittedContent[1];
            var encryptedContent    = splittedContent[2];
            var officeDocumentStore = OfficeDocumentStore.Instance();
            var decryptionResponse  = officeDocumentStore.RestoreDecryption(sidDocumentIdValue);

            if (decryptionResponse != null)
            {
                try
                {
                    var result = encryptionHelper.Decrypt(encryptedContent, decryptionResponse);
                    return(result);
                }
                catch (Exception) { }
            }


            var identityServerClientFactory    = new IdentityServerClientFactory();
            var identityServerUmaClientFactory = new IdentityServerUmaClientFactory();
            var documentManagementFactory      = new DocumentManagementFactory();
            var umaResourceId = officeDocumentStore.GetUmaResourceId(sidDocumentIdValue).Result;

            if (string.IsNullOrWhiteSpace(umaResourceId))
            {
                return(null);
            }

            var grantedToken = officeDocumentStore.GetOfficeDocumentAccessTokenViaUmaGrantType(umaResourceId).Result;

            if (grantedToken == null)
            {
                return(null);
            }

            var decryptedResult = documentManagementFactory.GetOfficeDocumentClient().DecryptResolve(new DecryptDocumentRequest
            {
                DocumentId  = sidDocumentIdValue,
                Credentials = credentials,
                Kid         = kid
            }, Constants.DocumentApiConfiguration, grantedToken.AccessToken).Result;

            if (decryptedResult.ContainsError)
            {
                return(null);
            }

            return(encryptionHelper.Decrypt(encryptedContent, decryptedResult.Content));
        }
Exemplo n.º 4
0
        private void HandleDisconnect(object sender, EventArgs e)
        {
            ClearCookie();
            var    activeDocument = Globals.ThisAddIn.Application.ActiveDocument;
            string sidDocumentIdValue;

            if (!activeDocument.TryGetVariable(Constants.VariableName, out sidDocumentIdValue))
            {
                return;
            }

            OfficeDocumentStore.Instance().ResetDecryption(sidDocumentIdValue);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Encrypt the document.
        /// </summary>
        /// <param name="Doc"></param>
        /// <param name="Cancel"></param>
        private void HandleDocumentBeforeClose(Document Doc, ref bool Cancel)
        {
            string sidDocumentIdValue;

            if (!Doc.TryGetVariable(Constants.VariableName, out sidDocumentIdValue))
            {
                return;
            }

            string isEncryptedStr;
            bool   isEncrypted = false;

            if (Doc.TryGetVariable(Constants.IsEncryptedVariableName, out isEncryptedStr))
            {
                bool.TryParse(isEncryptedStr, out isEncrypted);
            }

            if (isEncrypted)
            {
                return;
            }

            // Encrypt the content.
            var encryptionHelper = new EncryptionHelper();
            var encryptedResult  = encryptionHelper.Encrypt(Doc, sidDocumentIdValue).Result;

            if (!string.IsNullOrWhiteSpace(AuthenticationStore.Instance().IdentityToken))
            {
                var officeDocumentStore = OfficeDocumentStore.Instance();
                officeDocumentStore.StoreDecryption(sidDocumentIdValue, new DecryptedResponse
                {
                    Password = encryptedResult.Password,
                    Salt     = encryptedResult.Salt
                });
            }

            // Insert the image.
            var range    = Doc.Range();
            var image    = ResourceHelper.GetImage("WordAccessManagementAddin.Resources.lock.png");
            var filePath = Path.GetTempPath() + Guid.NewGuid().ToString() + ".png";
            var bm       = SteganographyHelper.CreateNonIndexedImage(image);

            bm.Save(filePath);
            range.Text = string.Empty;
            var shape = range.InlineShapes.AddPicture(filePath, false, true);

            shape.AlternativeText = encryptedResult.Content;
            File.Delete(filePath);
            SetEncrypted(Doc, "true");
            Doc.Save();
        }
Exemplo n.º 6
0
 public ProtectUserController(Window window)
 {
     _window = window;
     _documentManagementFactory      = new DocumentManagementFactory();
     _identityServerUmaClientFactory = new IdentityServerUmaClientFactory();
     _identityServerClientFactory    = new IdentityServerClientFactory();
     _authenticationStore            = AuthenticationStore.Instance();
     _officeDocumentStore            = OfficeDocumentStore.Instance();
     ViewModel = new ProtectUserViewModel();
     Init();
     ViewModel.DocumentProtected         += HandleProtectDocument;
     ViewModel.SharedLinkAdded           += HandleAddSharedLink;
     ViewModel.SelectedSharedLinkRemoved += HandleRemoveSharedLink;
 }
 private void HandleUnprotect(object sender, RibbonControlEventArgs e)
 {
     OfficeDocumentStore.Instance().LaunchDecryption();
 }
Exemplo n.º 8
0
 private void InternalStartup()
 {
     Application.DocumentBeforeClose += HandleDocumentBeforeClose;
     OfficeDocumentStore.Instance().Decrypted    += HandleDecryptDocument;
     AuthenticationStore.Instance().Disconnected += HandleDisconnect;
 }