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.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
namespace ASC.ApiSystem.Controllers;
|
2020-05-19 13:22:47 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
[Scope]
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("[controller]")]
|
|
|
|
|
public class CalDavController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private CommonMethods CommonMethods { get; }
|
|
|
|
|
private EmailValidationKeyProvider EmailValidationKeyProvider { get; }
|
|
|
|
|
private CoreSettings CoreSettings { get; }
|
|
|
|
|
private CommonConstants CommonConstants { get; }
|
|
|
|
|
public InstanceCrypto InstanceCrypto { get; }
|
|
|
|
|
private ILog Log { get; }
|
|
|
|
|
private IHttpClientFactory ClientFactory { get; }
|
|
|
|
|
|
|
|
|
|
public CalDavController(
|
|
|
|
|
CommonMethods commonMethods,
|
|
|
|
|
EmailValidationKeyProvider emailValidationKeyProvider,
|
|
|
|
|
CoreSettings coreSettings,
|
|
|
|
|
CommonConstants commonConstants,
|
|
|
|
|
InstanceCrypto instanceCrypto,
|
|
|
|
|
IOptionsMonitor<ILog> option)
|
|
|
|
|
{
|
|
|
|
|
CommonMethods = commonMethods;
|
|
|
|
|
EmailValidationKeyProvider = emailValidationKeyProvider;
|
|
|
|
|
CoreSettings = coreSettings;
|
|
|
|
|
CommonConstants = commonConstants;
|
|
|
|
|
InstanceCrypto = instanceCrypto;
|
|
|
|
|
Log = option.Get("ASC.ApiSystem");
|
|
|
|
|
}
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
#region For TEST api
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
[HttpGet("test")]
|
|
|
|
|
public IActionResult Check()
|
|
|
|
|
{
|
|
|
|
|
return Ok(new
|
2020-02-18 13:48:38 +00:00
|
|
|
|
{
|
2022-04-22 13:24:54 +00:00
|
|
|
|
value = "CalDav api works"
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
#endregion
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
#region API methods
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
[HttpGet("change_to_storage")]
|
|
|
|
|
public IActionResult СhangeOfCalendarStorage(string change)
|
|
|
|
|
{
|
|
|
|
|
if (!GetTenant(change, out var tenant, out var error))
|
2020-02-18 13:48:38 +00:00
|
|
|
|
{
|
2022-04-22 13:24:54 +00:00
|
|
|
|
return BadRequest(error);
|
|
|
|
|
}
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var validationKey = EmailValidationKeyProvider.GetEmailKey(tenant.Id, change + ConfirmType.Auth);
|
|
|
|
|
|
|
|
|
|
SendToApi(Request.Scheme, tenant, "calendar/change_to_storage", new Dictionary<string, string> { { "change", change }, { "key", validationKey } });
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.Error("Error change_to_storage", ex);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
return StatusCode(StatusCodes.Status500InternalServerError, new
|
2020-02-18 13:48:38 +00:00
|
|
|
|
{
|
2022-04-22 13:24:54 +00:00
|
|
|
|
error = "apiError",
|
|
|
|
|
message = ex.Message
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
return Ok();
|
|
|
|
|
}
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
[HttpGet("caldav_delete_event")]
|
|
|
|
|
[Authorize(AuthenticationSchemes = "auth.allowskip")]
|
|
|
|
|
public IActionResult CaldavDeleteEvent(string eventInfo)
|
|
|
|
|
{
|
|
|
|
|
if (!GetTenant(eventInfo, out var tenant, out var error))
|
|
|
|
|
{
|
|
|
|
|
return BadRequest(error);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
try
|
2020-02-18 13:48:38 +00:00
|
|
|
|
{
|
2022-04-22 13:24:54 +00:00
|
|
|
|
var validationKey = EmailValidationKeyProvider.GetEmailKey(tenant.Id, eventInfo + ConfirmType.Auth);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
SendToApi(Request.Scheme, tenant, "calendar/caldav_delete_event", new Dictionary<string, string> { { "eventInfo", eventInfo }, { "key", validationKey } });
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.Error("Error caldav_delete_event", ex);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
return StatusCode(StatusCodes.Status500InternalServerError, new
|
2020-02-18 13:48:38 +00:00
|
|
|
|
{
|
2022-04-22 13:24:54 +00:00
|
|
|
|
error = "apiError",
|
|
|
|
|
message = ex.Message
|
|
|
|
|
});
|
2020-02-18 13:48:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("is_caldav_authenticated")]
|
|
|
|
|
[Authorize(AuthenticationSchemes = "auth.allowskip")]
|
|
|
|
|
public IActionResult IsCaldavAuthenticated(UserPassword userPassword)
|
|
|
|
|
{
|
|
|
|
|
if (userPassword == null || string.IsNullOrEmpty(userPassword.User) || string.IsNullOrEmpty(userPassword.Password))
|
2020-02-18 13:48:38 +00:00
|
|
|
|
{
|
2022-04-22 13:24:54 +00:00
|
|
|
|
Log.Error("CalDav authenticated data is null");
|
|
|
|
|
|
|
|
|
|
return BadRequest(new
|
2020-02-18 13:48:38 +00:00
|
|
|
|
{
|
2022-04-22 13:24:54 +00:00
|
|
|
|
value = "false",
|
|
|
|
|
error = "portalNameEmpty",
|
|
|
|
|
message = "Argument is required"
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
if (!GetUserData(userPassword.User, out var email, out var tenant, out var error))
|
|
|
|
|
{
|
|
|
|
|
return BadRequest(error);
|
|
|
|
|
}
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Log.Info(string.Format("Caldav auth user: {0}, tenant: {1}", email, tenant.Id));
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
if (InstanceCrypto.Encrypt(email) == userPassword.Password)
|
2020-02-18 13:48:38 +00:00
|
|
|
|
{
|
2022-04-22 13:24:54 +00:00
|
|
|
|
return Ok(new
|
2020-02-18 13:48:38 +00:00
|
|
|
|
{
|
2022-04-22 13:24:54 +00:00
|
|
|
|
value = "true"
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-09-18 07:59:23 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
var validationKey = EmailValidationKeyProvider.GetEmailKey(tenant.Id, email + userPassword.Password + ConfirmType.Auth);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
var authData = $"userName={HttpUtility.UrlEncode(email)}&password={HttpUtility.UrlEncode(userPassword.Password)}&key={HttpUtility.UrlEncode(validationKey)}";
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
SendToApi(Request.Scheme, tenant, "authentication/login", null, WebRequestMethods.Http.Post, authData);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
return Ok(new
|
2020-02-18 13:48:38 +00:00
|
|
|
|
{
|
2022-04-22 13:24:54 +00:00
|
|
|
|
value = "true"
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.Error("Caldav authenticated", ex);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
return StatusCode(StatusCodes.Status500InternalServerError, new
|
|
|
|
|
{
|
|
|
|
|
value = "false",
|
|
|
|
|
message = ex.Message
|
|
|
|
|
});
|
2020-02-18 13:48:38 +00:00
|
|
|
|
}
|
2022-04-22 13:24:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
#region private methods
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
private bool GetTenant(string calendarParam, out Tenant tenant, out object error)
|
|
|
|
|
{
|
|
|
|
|
tenant = null;
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
if (string.IsNullOrEmpty(calendarParam))
|
2020-02-18 13:48:38 +00:00
|
|
|
|
{
|
2022-04-22 13:24:54 +00:00
|
|
|
|
Log.Error("calendarParam is empty");
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
error = new
|
2020-02-18 13:48:38 +00:00
|
|
|
|
{
|
2022-04-22 13:24:54 +00:00
|
|
|
|
value = "false",
|
|
|
|
|
error = "portalNameEmpty",
|
|
|
|
|
message = "Argument is required"
|
|
|
|
|
};
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
Log.Info($"CalDav calendarParam: {calendarParam}");
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
var userParam = calendarParam.Split('/')[0];
|
|
|
|
|
return GetUserData(userParam, out _, out tenant, out error);
|
|
|
|
|
}
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
private bool GetUserData(string userParam, out string email, out Tenant tenant, out object error)
|
|
|
|
|
{
|
|
|
|
|
email = null;
|
|
|
|
|
tenant = null;
|
|
|
|
|
error = null;
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
if (string.IsNullOrEmpty(userParam))
|
2020-02-18 13:48:38 +00:00
|
|
|
|
{
|
2022-04-22 13:24:54 +00:00
|
|
|
|
Log.Error("userParam is empty");
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
error = new
|
2020-02-18 13:48:38 +00:00
|
|
|
|
{
|
2022-04-22 13:24:54 +00:00
|
|
|
|
value = "false",
|
|
|
|
|
error = "portalNameEmpty",
|
|
|
|
|
message = "Argument is required"
|
|
|
|
|
};
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
var userData = userParam.Split('@');
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
if (userData.Length < 3)
|
|
|
|
|
{
|
|
|
|
|
Log.Error($"Error Caldav username: {userParam}");
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
error = new
|
2020-02-18 13:48:38 +00:00
|
|
|
|
{
|
2022-04-22 13:24:54 +00:00
|
|
|
|
value = "false",
|
|
|
|
|
error = "portalNameEmpty",
|
|
|
|
|
message = "PortalName is required"
|
|
|
|
|
};
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
email = string.Join("@", userData[0], userData[1]);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
var tenantName = userData[2];
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
var baseUrl = CoreSettings.BaseDomain;
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(baseUrl) && tenantName.EndsWith("." + baseUrl, StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
tenantName = tenantName.Replace("." + baseUrl, "");
|
|
|
|
|
}
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
Log.Info($"CalDav: user:{userParam} tenantName:{tenantName}");
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
var tenantModel = new TenantModel { PortalName = tenantName };
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
if (!CommonMethods.GetTenant(tenantModel, out tenant))
|
|
|
|
|
{
|
|
|
|
|
Log.Error("Model without tenant");
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
error = new
|
2020-02-18 13:48:38 +00:00
|
|
|
|
{
|
2022-04-22 13:24:54 +00:00
|
|
|
|
value = "false",
|
|
|
|
|
error = "portalNameEmpty",
|
|
|
|
|
message = "PortalName is required"
|
|
|
|
|
};
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
if (tenant == null)
|
|
|
|
|
{
|
|
|
|
|
Log.Error("Tenant not found " + tenantName);
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
error = new
|
2020-02-18 13:48:38 +00:00
|
|
|
|
{
|
2022-04-22 13:24:54 +00:00
|
|
|
|
value = "false",
|
|
|
|
|
error = "portalNameNotFound",
|
|
|
|
|
message = "Portal not found"
|
|
|
|
|
};
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
return false;
|
2020-02-18 13:48:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
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());
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
var url = $"{requestUriScheme}{Uri.SchemeDelimiter}{tenant.GetTenantDomain(CoreSettings)}{CommonConstants.WebApiBaseUrl}{path}{(string.IsNullOrEmpty(query) ? "" : "?" + query)}";
|
|
|
|
|
|
|
|
|
|
Log.Info($"CalDav: SendToApi: {url}");
|
2021-10-12 10:14:33 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
var request = new HttpRequestMessage();
|
|
|
|
|
request.RequestUri = new Uri(url);
|
|
|
|
|
request.Method = new HttpMethod(httpMethod);
|
|
|
|
|
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json"));
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
var httpClient = ClientFactory.CreateClient();
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
if (data != null)
|
|
|
|
|
{
|
|
|
|
|
request.Content = new StringContent(data, Encoding.UTF8, "application/x-www-form-urlencoded");
|
|
|
|
|
}
|
2020-02-18 13:48:38 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
httpClient.Send(request);
|
|
|
|
|
}
|
2020-09-18 07:59:23 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
#endregion
|
2020-09-18 07:59:23 +00:00
|
|
|
|
|
2022-04-22 13:24:54 +00:00
|
|
|
|
public class UserPassword
|
|
|
|
|
{
|
|
|
|
|
public string User { get; set; }
|
|
|
|
|
public string Password { get; set; }
|
2020-02-18 13:48:38 +00:00
|
|
|
|
}
|
2022-04-22 13:24:54 +00:00
|
|
|
|
}
|