public async Task <HttpResponseMessage> GetFile(string file_id)
        {
            var          getFileRequest = new GetFileRequest(this.Request, file_id);
            WopiResponse wopiResponse   = null;

            try
            {
                if (await Authorize(getFileRequest))
                {
                    if (await WopiProof.Validate(getFileRequest))
                    {
                        wopiResponse = await GetFile(getFileRequest);
                    }
                    else
                    {
                        getFileRequest.ResponseServerError("Proof validation failed");
                    }
                }
                else
                {
                    wopiResponse = getFileRequest.ResponseUnauthorized();
                }
            }
            catch (Exception ex)
            {
                wopiResponse = getFileRequest.ResponseServerError(ex.Message);
            }

            return(wopiResponse.ToHttpResponse());
        }
예제 #2
0
        internal async static Task <WopiProof> getWopiProof()
        {
            var wopiProof = new WopiProof();
            // Check cache for this data
            MemoryCache memoryCache = MemoryCache.Default;

            if (!memoryCache.Contains("WopiProof"))
            {
                await WopiDiscovery.Refresh();
            }
            if (memoryCache.Contains("WopiProof"))
            {
                wopiProof = (WopiProof)memoryCache["WopiProof"];
            }
            return(wopiProof);
        }
예제 #3
0
        /// <summary>
        /// Gets the WOPI proof details from the WOPI discovery endpoint and caches it appropriately
        /// </summary>
        internal async static Task <WopiProof> getWopiProof(HttpContext context)
        {
            WopiProof wopiProof = null;

            // Check cache for this data
            MemoryCache memoryCache = MemoryCache.Default;

            if (memoryCache.Contains("WopiProof"))
            {
                wopiProof = (WopiProof)memoryCache["WopiProof"];
            }
            else
            {
                HttpClient client = new HttpClient();
                using (HttpResponseMessage response = await client.GetAsync(ConfigurationManager.AppSettings["WopiDiscovery"]))
                {
                    if (response.IsSuccessStatusCode)
                    {
                        // Read the xml string from the response
                        string xmlString = await response.Content.ReadAsStringAsync();

                        // Parse the xml string into Xml
                        var discoXml = XDocument.Parse(xmlString);

                        // Convert the discovery xml into list of WopiApp
                        var proof = discoXml.Descendants("proof-key").FirstOrDefault();
                        wopiProof = new WopiProof()
                        {
                            value       = proof.Attribute("value").Value,
                            modulus     = proof.Attribute("modulus").Value,
                            exponent    = proof.Attribute("exponent").Value,
                            oldvalue    = proof.Attribute("oldvalue").Value,
                            oldmodulus  = proof.Attribute("oldmodulus").Value,
                            oldexponent = proof.Attribute("oldexponent").Value
                        };

                        // Add to cache for 20min
                        memoryCache.Add("WopiProof", wopiProof, DateTimeOffset.Now.AddMinutes(20));
                    }
                }
            }

            return(wopiProof);
        }
예제 #4
0
        public async Task RefreshAsync()
        {
            // Use the Wopi Discovery endpoint to get the data
            HttpClient client = httpClientFactory.CreateClient(Constants.HttpClientDefault);

            using (HttpResponseMessage response = await client.GetAsync(wopiOptions.DiscoveryUrl))
            {
                if (response.IsSuccessStatusCode)
                {
                    var actions = new List <WopiAction>();

                    // Read the xml string from the response
                    var xmlString = await response.Content.ReadAsStringAsync();

                    // Parse the xml string into Xml
                    var discoXml = XDocument.Parse(xmlString);

                    // Convert the discovery xml into list of WopiApp
                    var xapps = discoXml.Descendants("app");
                    foreach (var xapp in xapps)
                    {
                        // Parse the actions for the app
                        var xactions = xapp.Descendants("action");
                        foreach (var xaction in xactions)
                        {
                            actions.Add(new WopiAction()
                            {
                                app          = xapp.Attribute(nameof(WopiAction.name)).Value,
                                favIconUrl   = xapp.Attribute(nameof(WopiAction.favIconUrl)).Value,
                                checkLicense = Convert.ToBoolean(xapp.Attribute(nameof(WopiAction.checkLicense)).Value),
                                name         = xaction.Attribute(nameof(WopiAction.name)).Value,
                                ext          = (xaction.Attribute(nameof(WopiAction.ext)) != null) ? xaction.Attribute(nameof(WopiAction.ext)).Value : string.Empty,
                                progid       = (xaction.Attribute(nameof(WopiAction.progid)) != null) ? xaction.Attribute(nameof(WopiAction.progid)).Value : string.Empty,
                                @default     = (xaction.Attribute(nameof(WopiAction.@default)) != null) ? true : false,
                                urlsrc       = xaction.Attribute(nameof(WopiAction.urlsrc)).Value,
                                requires     = (xaction.Attribute(nameof(WopiAction.requires)) != null) ? xaction.Attribute(nameof(WopiAction.requires)).Value : string.Empty
                            });
                        }

                        // Cache the discovey data for an hour
                        await memoryCache.GetOrCreateAsync(Constants.DiscoveryData, entry =>
                        {
                            entry.SlidingExpiration = TimeSpan.FromHours(1);
                            return(Task.FromResult(actions));
                        });
                    }

                    // Convert the discovery xml into list of WopiApp
                    var proof     = discoXml.Descendants("proof-key").FirstOrDefault();
                    var wopiProof = new WopiProof()
                    {
                        value       = proof.Attribute(nameof(WopiProof.value)).Value,
                        modulus     = proof.Attribute(nameof(WopiProof.modulus)).Value,
                        exponent    = proof.Attribute(nameof(WopiProof.exponent)).Value,
                        oldvalue    = proof.Attribute(nameof(WopiProof.oldvalue)).Value,
                        oldmodulus  = proof.Attribute(nameof(WopiProof.oldmodulus)).Value,
                        oldexponent = proof.Attribute(nameof(WopiProof.oldexponent)).Value
                    };

                    // Add to cache for 20min
                    await memoryCache.GetOrCreateAsync(Constants.WopiProof, entry =>
                    {
                        entry.SlidingExpiration = TimeSpan.FromMinutes(20);
                        return(Task.FromResult(wopiProof));
                    });
                }
            }
        }
        public async Task <HttpResponseMessage> ProcessPostActions(string file_id)
        {
            WopiRequest  wopiRequest  = new WopiRequest(this.Request, file_id);
            WopiResponse wopiResponse = null;

            try
            {
                if (await Authorize(wopiRequest))
                {
                    if (await WopiProof.Validate(wopiRequest))
                    {
                        var filesPostOverride = WopiRequest.GetHttpRequestHeader(this.Request, WopiRequestHeaders.OVERRIDE);

                        switch (filesPostOverride)
                        {
                        case "LOCK":
                            var oldLock = WopiRequest.GetHttpRequestHeader(this.Request, WopiRequestHeaders.OLD_LOCK);
                            if (oldLock != null)
                            {
                                wopiResponse = await UnlockAndRelock(new UnlockAndRelockRequest(this.Request, file_id));
                            }
                            else
                            {
                                wopiResponse = await Lock(new LockRequest(this.Request, file_id));
                            }
                            break;

                        case "GET_LOCK":
                            wopiResponse = await GetLock(new GetLockRequest(this.Request, file_id));

                            break;

                        case "REFRESH_LOCK":
                            wopiResponse = await RefreshLock(new RefreshLockRequest(this.Request, file_id));

                            break;

                        case "UNLOCK":
                            wopiResponse = await Unlock(new UnlockRequest(this.Request, file_id));

                            break;

                        case "PUT_RELATIVE":
                            var suggestedTarget = WopiRequest.GetHttpRequestHeader(this.Request, WopiRequestHeaders.SUGGESTED_TARGET);
                            var relativeTarget  = WopiRequest.GetHttpRequestHeader(this.Request, WopiRequestHeaders.RELATIVE_TARGET);
                            if (suggestedTarget != null && relativeTarget != null)
                            {
                                // This really should be BadRequest, but the spec requires NotImplmented
                                wopiResponse = new WopiResponse()
                                {
                                    StatusCode = HttpStatusCode.NotImplemented
                                };
                            }
                            else
                            {
                                if (suggestedTarget != null)
                                {
                                    wopiResponse = await PutRelativeFileSuggested(new PutRelativeFileSuggestedRequest(this.Request, file_id));
                                }
                                else if (relativeTarget != null)
                                {
                                    wopiResponse = await PutRelativeFileSpecific(new PutRelativeFileSpecificRequest(this.Request, file_id));
                                }
                                else     // Both are null
                                {
                                    wopiResponse = new WopiResponse()
                                    {
                                        StatusCode = HttpStatusCode.BadRequest
                                    }
                                };
                            }
                            break;

                        case "RENAME_FILE":
                            wopiResponse = await RenameFile(new RenameFileRequest(this.Request, file_id));

                            break;

                        case "PUT_USER_INFO":
                            wopiResponse = await PutUserInfo(new PutUserInfoRequest(this.Request, file_id));

                            break;

                        case "DELETE":
                            wopiResponse = await DeleteFile(new DeleteFileRequest(this.Request, file_id));

                            break;

                        default:
                            wopiResponse = wopiRequest.ResponseServerError(string.Format("Invalid {0} header value: {1}", WopiRequestHeaders.OVERRIDE, filesPostOverride));
                            break;
                        }
                    }
                    else
                    {
                        wopiResponse = wopiRequest.ResponseServerError("Proof validation failed");
                    }
                }
                else
                {
                    wopiResponse = wopiRequest.ResponseUnauthorized();
                }
            }
            catch (Exception ex)
            {
                wopiResponse = wopiRequest.ResponseServerError(ex.Message);
            }
            return(wopiResponse.ToHttpResponse());
        }
예제 #6
0
        public async static Task Refresh()
        {
            // Use the Wopi Discovery endpoint to get the data
            HttpClient client = new HttpClient();

            using (HttpResponseMessage response = await client.GetAsync(ConfigurationManager.AppSettings["WopiDiscovery"]))
            {
                if (response.IsSuccessStatusCode)
                {
                    var memoryCache = MemoryCache.Default;
                    var actions     = new List <WopiAction>();

                    // Read the xml string from the response
                    var xmlString = await response.Content.ReadAsStringAsync();

                    // Parse the xml string into Xml
                    var discoXml = XDocument.Parse(xmlString);

                    // Convert the discovery xml into list of WopiApp
                    var xapps = discoXml.Descendants("app");
                    foreach (var xapp in xapps)
                    {
                        // Parse the actions for the app
                        var xactions = xapp.Descendants("action");
                        foreach (var xaction in xactions)
                        {
                            actions.Add(new WopiAction()
                            {
                                app          = xapp.Attribute("name").Value,
                                favIconUrl   = xapp.Attribute("favIconUrl").Value,
                                checkLicense = Convert.ToBoolean(xapp.Attribute("checkLicense").Value),
                                name         = xaction.Attribute("name").Value,
                                ext          = (xaction.Attribute("ext") != null) ? xaction.Attribute("ext").Value : String.Empty,
                                progid       = (xaction.Attribute("progid") != null) ? xaction.Attribute("progid").Value : String.Empty,
                                isDefault    = (xaction.Attribute("default") != null) ? true : false,
                                urlsrc       = xaction.Attribute("urlsrc").Value,
                                requires     = (xaction.Attribute("requires") != null) ? xaction.Attribute("requires").Value : String.Empty
                            });
                        }

                        // Cache the discovey data for an hour
                        memoryCache.Add("DiscoData", actions, DateTimeOffset.Now.AddHours(1));
                    }

                    // Convert the discovery xml into list of WopiApp
                    var proof     = discoXml.Descendants("proof-key").FirstOrDefault();
                    var wopiProof = new WopiProof()
                    {
                        value       = proof.Attribute("value").Value,
                        modulus     = proof.Attribute("modulus").Value,
                        exponent    = proof.Attribute("exponent").Value,
                        oldvalue    = proof.Attribute("oldvalue").Value,
                        oldmodulus  = proof.Attribute("oldmodulus").Value,
                        oldexponent = proof.Attribute("oldexponent").Value
                    };

                    // Add to cache for 20min
                    memoryCache.Add("WopiProof", wopiProof, DateTimeOffset.Now.AddMinutes(20));
                }
            }
        }