diff --git a/web/documentserver-example/csharp-mvc/Helpers/DocumentConverter.cs b/web/documentserver-example/csharp-mvc/Helpers/DocumentConverter.cs index 488c7cd3..34877c31 100644 --- a/web/documentserver-example/csharp-mvc/Helpers/DocumentConverter.cs +++ b/web/documentserver-example/csharp-mvc/Helpers/DocumentConverter.cs @@ -25,12 +25,14 @@ */ using System; +using System.Collections.Generic; using System.IO; using System.Net; using System.Text; using System.Text.RegularExpressions; using System.Web.Configuration; using System.Web.Helpers; +using System.Web.Script.Serialization; namespace OnlineEditorsExampleMVC.Helpers { @@ -107,15 +109,30 @@ namespace OnlineEditorsExampleMVC.Helpers request.Accept = "application/json"; request.Timeout = ConvertTimeout; - var bodyString = string.Format("{{\"async\": {0},\"filetype\": \"{1}\",\"key\": \"{2}\",\"outputtype\": \"{3}\",\"title\": \"{4}\",\"url\": \"{5}\"}}", - isAsync.ToString().ToLower(), - fromExtension.Trim('.'), - documentRevisionId, - toExtension.Trim('.'), - title, - documentUri); + var body = new Dictionary() { + { "async", isAsync }, + { "filetype", fromExtension.Trim('.') }, + { "key", documentRevisionId }, + { "outputtype", toExtension.Trim('.') }, + { "title", title }, + { "url", documentUri } + }; - var bytes = Encoding.UTF8.GetBytes(bodyString); + if (JwtManager.Enabled) + { + var payload = new Dictionary + { + { "payload", body } + }; + + var payloadToken = JwtManager.Encode(payload); + var bodyToken = JwtManager.Encode(body); + request.Headers.Add("Authorization", "Bearer " + payloadToken); + + body.Add("token", bodyToken); + } + + var bytes = Encoding.UTF8.GetBytes(new JavaScriptSerializer().Serialize(body)); request.ContentLength = bytes.Length; using (var requestStream = request.GetRequestStream()) { diff --git a/web/documentserver-example/csharp-mvc/Helpers/JwtManager.cs b/web/documentserver-example/csharp-mvc/Helpers/JwtManager.cs new file mode 100644 index 00000000..abeffe0b --- /dev/null +++ b/web/documentserver-example/csharp-mvc/Helpers/JwtManager.cs @@ -0,0 +1,109 @@ +/* + * + * (c) Copyright Ascensio System SIA 2019 + * + * The MIT License (MIT) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * +*/ + +using System; +using System.Collections.Generic; +using System.Security.Cryptography; +using System.Text; +using System.Web.Configuration; +using System.Web.Script.Serialization; + +namespace OnlineEditorsExampleMVC.Helpers +{ + public static class JwtManager + { + private readonly static string Secret; + public readonly static bool Enabled; + + private readonly static JavaScriptSerializer Serializer; + + static JwtManager() + { + Secret = WebConfigurationManager.AppSettings["files.docservice.secret"] ?? ""; + Enabled = !string.IsNullOrEmpty(Secret); + Serializer = new JavaScriptSerializer(); + } + + public static string Encode(IDictionary payload) + { + var header = new Dictionary() + { + { "alg", "HS256" }, + { "typ", "JWT" } + }; + + var encHeader = Base64UrlEncode(Serializer.Serialize(header)); + var encPayload = Base64UrlEncode(Serializer.Serialize(payload)); + var hashSum = Base64UrlEncode(CalculateHash(encHeader, encPayload)); + + return string.Format("{0}.{1}.{2}", encHeader, encPayload, hashSum); + } + + public static string Decode(string token) + { + if (!Enabled || string.IsNullOrEmpty(token)) return ""; + + var split = token.Split('.'); + if (split.Length != 3) return ""; + + string hashSum = Base64UrlEncode(CalculateHash(split[0], split[1])); + if (hashSum != split[2]) return ""; + return Base64UrlDecode(split[1]); + } + + private static byte[] CalculateHash(string encHeader, string encPayload) + { + using (HMACSHA256 hasher = new HMACSHA256(Encoding.UTF8.GetBytes(Secret))) + { + var bytes = Encoding.UTF8.GetBytes(string.Format("{0}.{1}", encHeader, encPayload)); + return hasher.ComputeHash(bytes); + } + } + + private static string Base64UrlEncode(string str) + { + return Base64UrlEncode(Encoding.UTF8.GetBytes(str)); + } + + private static string Base64UrlEncode(byte[] bytes) + { + return Convert.ToBase64String(bytes) + .TrimEnd('=').Replace('+', '-').Replace('/', '_'); + } + + private static string Base64UrlDecode(string payload) + { + string b64 = payload.Replace('_', '/').Replace('-', '+'); + switch (b64.Length % 4) + { + case 2: b64 += "=="; break; + case 3: b64 += "="; break; + } + byte[] bytes = Convert.FromBase64String(b64); + return Encoding.UTF8.GetString(bytes); + } + } +} \ No newline at end of file diff --git a/web/documentserver-example/csharp-mvc/Models/FileModel.cs b/web/documentserver-example/csharp-mvc/Models/FileModel.cs index 5444cb7c..39307397 100644 --- a/web/documentserver-example/csharp-mvc/Models/FileModel.cs +++ b/web/documentserver-example/csharp-mvc/Models/FileModel.cs @@ -25,6 +25,12 @@ */ using OnlineEditorsExampleMVC.Helpers; +using System; +using System.Collections.Generic; +using System.IO; +using System.Web; +using System.Web.Mvc; +using System.Web.Script.Serialization; namespace OnlineEditorsExampleMVC.Models { @@ -56,5 +62,67 @@ namespace OnlineEditorsExampleMVC.Models return DocManagerHelper.GetCallback(FileName); } } + + public string GetDocConfig(HttpRequest request, UrlHelper url) + { + var ext = Path.GetExtension(FileName); + var config = new Dictionary() + { + { "type", request["mode"] != "embedded" ? "desktop" : "embedded" }, + { "documentType", DocumentType }, + { "document", new Dictionary() + { + { "title", FileName }, + { "url", FileUri }, + { "fileType", ext.Trim('.') }, + { "key", Key }, + { "info", new Dictionary() + { + { "author", "Me" }, + { "created", DateTime.Now.ToShortDateString() } + } }, + { "permissions", new Dictionary + { + { "edit", DocManagerHelper.EditedExts.Contains(Path.GetExtension(FileName)) }, + { "download", true } + } } + } }, + { "editorConfig", new Dictionary() + { + { "mode", DocManagerHelper.EditedExts.Contains(Path.GetExtension(FileName)) && request["mode"] != "view" ? "edit" : "view" }, + { "lang", "en" }, + { "callbackUrl", CallbackUrl }, + { "user", new Dictionary() + { + { "id", DocManagerHelper.CurUserHostAddress() }, + { "name", "John Smith" } + } }, + { "embedded", new Dictionary() + { + { "saveUrl", FileUri }, + { "embedUrl", FileUri }, + { "shareUrl", FileUri }, + { "toolbarDocked", "top" } + } }, + { "customization", new Dictionary() + { + { "about", true }, + { "feedback", true }, + { "goback", new Dictionary() + { + { "url", url.Action("Index", "Home") } + } } + } } + } } + }; + + if (JwtManager.Enabled) + { + var token = JwtManager.Encode(config); + config.Add("token", token); + } + + return new JavaScriptSerializer().Serialize(config); + } } } \ No newline at end of file diff --git a/web/documentserver-example/csharp-mvc/OnlineEditorsExampleMVC.csproj b/web/documentserver-example/csharp-mvc/OnlineEditorsExampleMVC.csproj index 4dd3677f..b5db222b 100644 --- a/web/documentserver-example/csharp-mvc/OnlineEditorsExampleMVC.csproj +++ b/web/documentserver-example/csharp-mvc/OnlineEditorsExampleMVC.csproj @@ -136,6 +136,7 @@ + diff --git a/web/documentserver-example/csharp-mvc/Views/Home/Editor.aspx b/web/documentserver-example/csharp-mvc/Views/Home/Editor.aspx index 8f57f373..9c590cd6 100644 --- a/web/documentserver-example/csharp-mvc/Views/Home/Editor.aspx +++ b/web/documentserver-example/csharp-mvc/Views/Home/Editor.aspx @@ -53,8 +53,6 @@