2020-02-18 13:48:38 +00:00
|
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
* (c) Copyright Ascensio System Limited 2010-2018
|
|
|
|
|
*
|
|
|
|
|
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
|
|
|
|
|
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
|
|
|
|
|
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
|
|
|
|
|
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
|
|
|
|
|
*
|
|
|
|
|
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
|
|
|
|
|
*
|
|
|
|
|
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
|
|
|
|
|
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
|
|
|
|
|
*
|
|
|
|
|
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
|
|
|
|
|
* relevant author attributions when distributing the software. If the display of the logo in its graphic
|
|
|
|
|
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
|
|
|
|
|
* in every copy of the program you distribute.
|
|
|
|
|
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using ASC.ApiSystem.Classes;
|
|
|
|
|
using ASC.ApiSystem.Models;
|
|
|
|
|
using ASC.Core.Tenants;
|
|
|
|
|
using ASC.Web.Studio.Utility;
|
2020-02-25 11:43:01 +00:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2020-02-18 13:48:38 +00:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Web;
|
|
|
|
|
|
|
|
|
|
namespace ASC.ApiSystem.Controllers
|
|
|
|
|
{
|
|
|
|
|
[ApiController]
|
2020-02-25 08:45:16 +00:00
|
|
|
|
[Route("[controller]")]
|
|
|
|
|
public class CalDavController : ControllerBase
|
2020-02-18 13:48:38 +00:00
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
|
public CommonMethods CommonMethods { get; }
|
|
|
|
|
|
|
|
|
|
public CalDavController(CommonMethods commonMethods)
|
2020-02-18 13:48:38 +00:00
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
|
CommonMethods = commonMethods;
|
2020-02-18 13:48:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region For TEST api
|
|
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
|
[HttpGet("test")]
|
|
|
|
|
public IActionResult Check()
|
2020-02-18 13:48:38 +00:00
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
|
return Ok(new
|
2020-02-18 13:48:38 +00:00
|
|
|
|
{
|
|
|
|
|
value = "CalDav api works"
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region API methods
|
|
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
|
[HttpGet("change_to_storage")]
|
|
|
|
|
public IActionResult СhangeOfCalendarStorage(string change)
|
2020-02-18 13:48:38 +00:00
|
|
|
|
{
|
|
|
|
|
if (!GetTenant(change, out Tenant tenant, out object error))
|
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
|
return BadRequest(error);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
|
var validationKey = CommonMethods.EmailValidationKeyProvider.GetEmailKey(tenant.TenantId, change + ConfirmType.Auth);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
|
|
|
|
SendToApi(Request.Scheme, tenant, "calendar/change_to_storage", new Dictionary<string, string> { { "change", change }, { "key", validationKey } });
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
|
CommonMethods.Log.Error("Error change_to_storage", ex);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
|
return StatusCode(StatusCodes.Status500InternalServerError, new
|
2020-02-18 13:48:38 +00:00
|
|
|
|
{
|
|
|
|
|
error = "apiError",
|
|
|
|
|
message = ex.Message
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
|
return Ok();
|
2020-02-18 13:48:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
|
[HttpGet("caldav_delete_event")]
|
2020-02-25 11:43:01 +00:00
|
|
|
|
[Authorize(AuthenticationSchemes = "auth.allowskip")]
|
2020-02-25 08:45:16 +00:00
|
|
|
|
public IActionResult CaldavDeleteEvent(string eventInfo)
|
2020-02-18 13:48:38 +00:00
|
|
|
|
{
|
|
|
|
|
if (!GetTenant(eventInfo, out Tenant tenant, out object error))
|
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
|
return BadRequest(error);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
|
var validationKey = CommonMethods.EmailValidationKeyProvider.GetEmailKey(tenant.TenantId, eventInfo + ConfirmType.Auth);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
|
|
|
|
SendToApi(Request.Scheme, tenant, "calendar/caldav_delete_event", new Dictionary<string, string> { { "eventInfo", eventInfo }, { "key", validationKey } });
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
|
CommonMethods.Log.Error("Error caldav_delete_event", ex);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
|
return StatusCode(StatusCodes.Status500InternalServerError, new
|
2020-02-18 13:48:38 +00:00
|
|
|
|
{
|
|
|
|
|
error = "apiError",
|
|
|
|
|
message = ex.Message
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
|
return Ok();
|
2020-02-18 13:48:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
|
[HttpPost("is_caldav_authenticated")]
|
2020-02-25 11:43:01 +00:00
|
|
|
|
[Authorize(AuthenticationSchemes = "auth.allowskip")]
|
2020-02-25 08:45:16 +00:00
|
|
|
|
public IActionResult IsCaldavAuthenticated(JObject data)
|
2020-02-18 13:48:38 +00:00
|
|
|
|
{
|
|
|
|
|
if (data == null)
|
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
|
CommonMethods.Log.Error("CalDav authenticated data is null");
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
|
return BadRequest(new
|
2020-02-18 13:48:38 +00:00
|
|
|
|
{
|
|
|
|
|
value = "false",
|
|
|
|
|
error = "portalNameEmpty",
|
|
|
|
|
message = "Argument is required"
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var username = data.Value<string>("User");
|
|
|
|
|
var password = data.Value<string>("Password");
|
|
|
|
|
|
|
|
|
|
if (!GetUserData(username, out string email, out Tenant tenant, out object error))
|
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
|
return BadRequest(error);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
|
CommonMethods.Log.Info(string.Format("Caldav auth user: {0}, tenant: {1}", email, tenant.TenantId));
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
|
|
|
|
if (email == "admin@ascsystem" && Core.Configuration.Constants.CoreSystem.ID.ToString() == password)
|
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
|
return Ok(new
|
2020-02-18 13:48:38 +00:00
|
|
|
|
{
|
|
|
|
|
value = "true"
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
|
var validationKey = CommonMethods.EmailValidationKeyProvider.GetEmailKey(tenant.TenantId, email + password + ConfirmType.Auth);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
|
|
|
|
var authData = string.Format("userName={0}&password={1}&key={2}",
|
|
|
|
|
HttpUtility.UrlEncode(email),
|
|
|
|
|
HttpUtility.UrlEncode(password),
|
|
|
|
|
HttpUtility.UrlEncode(validationKey));
|
|
|
|
|
|
|
|
|
|
SendToApi(Request.Scheme, tenant, "authentication/login", null, WebRequestMethods.Http.Post, authData);
|
|
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
|
return Ok(new
|
2020-02-18 13:48:38 +00:00
|
|
|
|
{
|
|
|
|
|
value = "true"
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
|
CommonMethods.Log.Error("Caldav authenticated", ex);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
|
return StatusCode(StatusCodes.Status500InternalServerError, new
|
2020-02-18 13:48:38 +00:00
|
|
|
|
{
|
|
|
|
|
value = "false",
|
|
|
|
|
message = ex.Message
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region private methods
|
|
|
|
|
|
|
|
|
|
private bool GetTenant(string calendarParam, out Tenant tenant, out object error)
|
|
|
|
|
{
|
|
|
|
|
tenant = null;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(calendarParam))
|
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
|
CommonMethods.Log.Error("calendarParam is empty");
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
|
|
|
|
error = new
|
|
|
|
|
{
|
|
|
|
|
value = "false",
|
|
|
|
|
error = "portalNameEmpty",
|
|
|
|
|
message = "Argument is required"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
|
CommonMethods.Log.Info(string.Format("CalDav calendarParam: {0}", calendarParam));
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
|
|
|
|
var userParam = calendarParam.Split('/')[0];
|
|
|
|
|
|
|
|
|
|
return GetUserData(userParam, out string email, out tenant, out error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool GetUserData(string userParam, out string email, out Tenant tenant, out object error)
|
|
|
|
|
{
|
|
|
|
|
email = null;
|
|
|
|
|
tenant = null;
|
|
|
|
|
error = null;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(userParam))
|
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
|
CommonMethods.Log.Error("userParam is empty");
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
|
|
|
|
error = new
|
|
|
|
|
{
|
|
|
|
|
value = "false",
|
|
|
|
|
error = "portalNameEmpty",
|
|
|
|
|
message = "Argument is required"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var userData = userParam.Split('@');
|
|
|
|
|
|
|
|
|
|
if (userData.Length < 3)
|
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
|
CommonMethods.Log.Error(string.Format("Error Caldav username: {0}", userParam));
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
|
|
|
|
error = new
|
|
|
|
|
{
|
|
|
|
|
value = "false",
|
|
|
|
|
error = "portalNameEmpty",
|
|
|
|
|
message = "PortalName is required"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
email = string.Join("@", userData[0], userData[1]);
|
|
|
|
|
|
|
|
|
|
var tenantName = userData[2];
|
|
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
|
var baseUrl = CommonMethods.CoreSettings.BaseDomain;
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(baseUrl) && tenantName.EndsWith("." + baseUrl, StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
tenantName = tenantName.Replace("." + baseUrl, "");
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
|
CommonMethods.Log.Info(string.Format("CalDav: user:{0} tenantName:{1}", userParam, tenantName));
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
|
|
|
|
var tenantModel = new TenantModel { PortalName = tenantName };
|
|
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
|
if (!CommonMethods.GetTenant(tenantModel, out tenant))
|
2020-02-18 13:48:38 +00:00
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
|
CommonMethods.Log.Error("Model without tenant");
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
|
|
|
|
error = new
|
|
|
|
|
{
|
|
|
|
|
value = "false",
|
|
|
|
|
error = "portalNameEmpty",
|
|
|
|
|
message = "PortalName is required"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tenant == null)
|
|
|
|
|
{
|
2020-02-25 08:45:16 +00:00
|
|
|
|
CommonMethods.Log.Error("Tenant not found " + tenantName);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
|
|
|
|
error = new
|
|
|
|
|
{
|
|
|
|
|
value = "false",
|
|
|
|
|
error = "portalNameNotFound",
|
|
|
|
|
message = "Portal not found"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SendToApi(string requestUriScheme,
|
|
|
|
|
Tenant tenant,
|
|
|
|
|
string path,
|
|
|
|
|
IEnumerable<KeyValuePair<string, string>> args = null,
|
|
|
|
|
string httpMethod = WebRequestMethods.Http.Get,
|
|
|
|
|
string data = null)
|
|
|
|
|
{
|
|
|
|
|
var query = args == null
|
|
|
|
|
? null
|
|
|
|
|
: string.Join("&", args.Select(arg => HttpUtility.UrlEncode(arg.Key) + "=" + HttpUtility.UrlEncode(arg.Value)).ToArray());
|
|
|
|
|
|
|
|
|
|
var url = string.Format("{0}{1}{2}{3}{4}{5}",
|
|
|
|
|
requestUriScheme,
|
|
|
|
|
Uri.SchemeDelimiter,
|
2020-02-25 08:45:16 +00:00
|
|
|
|
tenant.GetTenantDomain(CommonMethods.CoreSettings),
|
|
|
|
|
CommonMethods.CommonConstants.WebApiBaseUrl,
|
2020-02-18 13:48:38 +00:00
|
|
|
|
path,
|
|
|
|
|
string.IsNullOrEmpty(query) ? "" : "?" + query);
|
|
|
|
|
|
2020-02-25 08:45:16 +00:00
|
|
|
|
CommonMethods.Log.Info(string.Format("CalDav: SendToApi: {0}", url));
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
|
|
|
|
var webRequest = (HttpWebRequest)WebRequest.Create(url);
|
|
|
|
|
webRequest.Method = httpMethod;
|
|
|
|
|
webRequest.Accept = "application/json";
|
|
|
|
|
webRequest.ContentType = "application/x-www-form-urlencoded";
|
|
|
|
|
webRequest.ContentLength = 0;
|
|
|
|
|
|
|
|
|
|
if (data != null)
|
|
|
|
|
{
|
|
|
|
|
webRequest.ContentLength = data.Length;
|
|
|
|
|
|
|
|
|
|
using var writer = new StreamWriter(webRequest.GetRequestStream());
|
|
|
|
|
|
|
|
|
|
writer.Write(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (webRequest.GetResponse())
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|