2022-04-28 13:56:12 +00:00
|
|
|
|
// (c) Copyright Ascensio System SIA 2010-2022
|
|
|
|
|
//
|
|
|
|
|
// This program is a free software product.
|
|
|
|
|
// You can redistribute it and/or modify it under the terms
|
|
|
|
|
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
|
|
|
|
|
// Foundation. In accordance with Section 7(a) of the GNU AGPL 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 details, see
|
|
|
|
|
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
|
|
|
|
|
//
|
|
|
|
|
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
|
|
|
|
|
//
|
|
|
|
|
// The interactive user interfaces in modified source and object code versions of the Program must
|
|
|
|
|
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
|
|
|
|
|
//
|
|
|
|
|
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
|
|
|
|
|
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
|
|
|
|
|
// trademark law for use of our trademarks.
|
|
|
|
|
//
|
|
|
|
|
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
|
|
|
|
|
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
|
|
|
|
|
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
|
|
|
|
|
|
|
|
|
|
namespace ASC.Web.Api.Controllers.Settings;
|
|
|
|
|
|
|
|
|
|
public class SsoController : BaseSettingsController
|
|
|
|
|
{
|
|
|
|
|
private readonly SettingsManager _settingsManager;
|
|
|
|
|
private readonly PermissionContext _permissionContext;
|
|
|
|
|
private readonly TenantExtra _tenantExtra;
|
|
|
|
|
private readonly CoreBaseSettings _coreBaseSettings;
|
|
|
|
|
private readonly UserManager _userManager;
|
|
|
|
|
private readonly MessageService _messageService;
|
|
|
|
|
|
|
|
|
|
public SsoController(
|
|
|
|
|
ApiContext apiContext,
|
|
|
|
|
WebItemManager webItemManager,
|
|
|
|
|
IMemoryCache memoryCache,
|
|
|
|
|
IHttpContextAccessor httpContextAccessor,
|
|
|
|
|
SettingsManager settingsManager,
|
|
|
|
|
PermissionContext permissionContext,
|
|
|
|
|
TenantExtra tenantExtra,
|
|
|
|
|
CoreBaseSettings coreBaseSettings,
|
|
|
|
|
UserManager userManager,
|
|
|
|
|
MessageService messageService) : base(apiContext, memoryCache, webItemManager, httpContextAccessor)
|
|
|
|
|
{
|
|
|
|
|
_settingsManager = settingsManager;
|
|
|
|
|
_permissionContext = permissionContext;
|
|
|
|
|
_tenantExtra = tenantExtra;
|
|
|
|
|
_coreBaseSettings = coreBaseSettings;
|
|
|
|
|
_userManager = userManager;
|
|
|
|
|
_messageService = messageService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the current portal SSO settings.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <short>
|
|
|
|
|
/// Get the SSO settings
|
|
|
|
|
/// </short>
|
|
|
|
|
/// <category>SSO</category>
|
|
|
|
|
/// <returns>SSO settings</returns>
|
2022-05-23 15:48:23 +00:00
|
|
|
|
[HttpGet("ssov2")]
|
2022-04-28 13:56:12 +00:00
|
|
|
|
public SsoSettingsV2 GetSsoSettingsV2()
|
|
|
|
|
{
|
|
|
|
|
CheckSsoPermissions();
|
|
|
|
|
|
|
|
|
|
var settings = _settingsManager.Load<SsoSettingsV2>();
|
|
|
|
|
|
2022-05-13 08:52:49 +00:00
|
|
|
|
if (string.IsNullOrEmpty(settings.SpLoginLabel))
|
|
|
|
|
{
|
|
|
|
|
settings.SpLoginLabel = SsoSettingsV2.SSO_SP_LOGIN_LABEL;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-28 13:56:12 +00:00
|
|
|
|
return settings;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the default portal SSO settings.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <short>
|
|
|
|
|
/// Get the default SSO settings
|
|
|
|
|
/// </short>
|
|
|
|
|
/// <category>SSO</category>
|
|
|
|
|
/// <returns>Default SSO settings</returns>
|
2022-05-23 15:48:23 +00:00
|
|
|
|
[HttpGet("ssov2/default")]
|
2022-04-28 13:56:12 +00:00
|
|
|
|
public SsoSettingsV2 GetDefaultSsoSettingsV2()
|
|
|
|
|
{
|
|
|
|
|
CheckSsoPermissions();
|
2022-05-13 08:52:49 +00:00
|
|
|
|
return _settingsManager.GetDefault<SsoSettingsV2>();
|
2022-04-28 13:56:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the constants of the SSO settings.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <short>
|
|
|
|
|
/// Get the constants of the SSO settings
|
|
|
|
|
/// </short>
|
|
|
|
|
/// <category>SSO</category>
|
|
|
|
|
/// <returns>Constants of the SSO settings</returns>
|
2022-05-23 15:48:23 +00:00
|
|
|
|
[HttpGet("ssov2/constants")]
|
2022-04-28 13:56:12 +00:00
|
|
|
|
public object GetSsoSettingsV2Constants()
|
|
|
|
|
{
|
|
|
|
|
return new
|
|
|
|
|
{
|
|
|
|
|
SsoNameIdFormatType = new SsoNameIdFormatType(),
|
|
|
|
|
SsoBindingType = new SsoBindingType(),
|
|
|
|
|
SsoSigningAlgorithmType = new SsoSigningAlgorithmType(),
|
|
|
|
|
SsoEncryptAlgorithmType = new SsoEncryptAlgorithmType(),
|
|
|
|
|
SsoSpCertificateActionType = new SsoSpCertificateActionType(),
|
|
|
|
|
SsoIdpCertificateActionType = new SsoIdpCertificateActionType()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Saves the SSO settings for the current portal.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <short>
|
|
|
|
|
/// Save the SSO settings
|
|
|
|
|
/// </short>
|
|
|
|
|
/// <category>SSO</category>
|
|
|
|
|
/// <param name="serializeSettings">Serialized SSO settings</param>
|
|
|
|
|
/// <returns>SSO settings</returns>
|
2022-05-23 15:48:23 +00:00
|
|
|
|
[HttpPost("ssov2")]
|
2022-05-17 09:29:24 +00:00
|
|
|
|
public SsoSettingsV2 SaveSsoSettingsV2(SsoSettingsRequestsDto model)
|
2022-04-28 13:56:12 +00:00
|
|
|
|
{
|
|
|
|
|
CheckSsoPermissions();
|
|
|
|
|
|
|
|
|
|
var serializeSettings = model.SerializeSettings;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(serializeSettings))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException(Resource.SsoSettingsCouldNotBeNull);
|
2022-06-28 09:36:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var options = new JsonSerializerOptions
|
|
|
|
|
{
|
|
|
|
|
AllowTrailingCommas = true,
|
|
|
|
|
PropertyNameCaseInsensitive = true
|
|
|
|
|
};
|
2022-04-28 13:56:12 +00:00
|
|
|
|
|
2022-06-28 09:36:34 +00:00
|
|
|
|
var settings = JsonSerializer.Deserialize<SsoSettingsV2>(serializeSettings, options);
|
2022-04-28 13:56:12 +00:00
|
|
|
|
|
|
|
|
|
if (settings == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException(Resource.SsoSettingsCouldNotBeNull);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(settings.IdpSettings.EntityId))
|
|
|
|
|
{
|
|
|
|
|
throw new Exception(Resource.SsoSettingsInvalidEntityId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(settings.IdpSettings.SsoUrl) || !CheckUri(settings.IdpSettings.SsoUrl))
|
|
|
|
|
{
|
|
|
|
|
throw new Exception(string.Format(Resource.SsoSettingsInvalidBinding, "SSO " + settings.IdpSettings.SsoBinding));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(settings.IdpSettings.SloUrl) && !CheckUri(settings.IdpSettings.SloUrl))
|
|
|
|
|
{
|
|
|
|
|
throw new Exception(string.Format(Resource.SsoSettingsInvalidBinding, "SLO " + settings.IdpSettings.SloBinding));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(settings.FieldMapping.FirstName) ||
|
|
|
|
|
string.IsNullOrWhiteSpace(settings.FieldMapping.LastName) ||
|
|
|
|
|
string.IsNullOrWhiteSpace(settings.FieldMapping.Email))
|
|
|
|
|
{
|
|
|
|
|
throw new Exception(Resource.SsoSettingsInvalidMapping);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(settings.SpLoginLabel))
|
|
|
|
|
{
|
|
|
|
|
settings.SpLoginLabel = SsoSettingsV2.SSO_SP_LOGIN_LABEL;
|
|
|
|
|
}
|
|
|
|
|
else if (settings.SpLoginLabel.Length > 100)
|
|
|
|
|
{
|
|
|
|
|
settings.SpLoginLabel = settings.SpLoginLabel.Substring(0, 100);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!_settingsManager.Save(settings))
|
|
|
|
|
{
|
|
|
|
|
throw new Exception(Resource.SsoSettingsCantSaveSettings);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!settings.EnableSso)
|
|
|
|
|
{
|
|
|
|
|
ConverSsoUsersToOrdinary();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var messageAction = settings.EnableSso ? MessageAction.SSOEnabled : MessageAction.SSODisabled;
|
|
|
|
|
|
|
|
|
|
_messageService.Send(messageAction);
|
|
|
|
|
|
|
|
|
|
return settings;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Resets the SSO settings of the current portal.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <short>
|
|
|
|
|
/// Reset the SSO settings
|
|
|
|
|
/// </short>
|
|
|
|
|
/// <category>SSO</category>
|
|
|
|
|
/// <returns>Default SSO settings</returns>
|
2022-05-23 15:48:23 +00:00
|
|
|
|
[HttpDelete("ssov2")]
|
2022-04-28 13:56:12 +00:00
|
|
|
|
public SsoSettingsV2 ResetSsoSettingsV2()
|
|
|
|
|
{
|
|
|
|
|
CheckSsoPermissions();
|
|
|
|
|
|
2022-05-13 08:52:49 +00:00
|
|
|
|
var defaultSettings = _settingsManager.GetDefault<SsoSettingsV2>();
|
2022-04-28 13:56:12 +00:00
|
|
|
|
|
2022-05-13 08:52:49 +00:00
|
|
|
|
if (!_settingsManager.Save(defaultSettings))
|
2022-04-28 13:56:12 +00:00
|
|
|
|
{
|
|
|
|
|
throw new Exception(Resource.SsoSettingsCantSaveSettings);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ConverSsoUsersToOrdinary();
|
|
|
|
|
|
|
|
|
|
_messageService.Send(MessageAction.SSODisabled);
|
|
|
|
|
|
|
|
|
|
return defaultSettings;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ConverSsoUsersToOrdinary()
|
|
|
|
|
{
|
|
|
|
|
var ssoUsers = _userManager.GetUsers().Where(u => u.IsSSO()).ToList();
|
|
|
|
|
|
2022-05-13 08:52:49 +00:00
|
|
|
|
if (!ssoUsers.Any())
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-28 13:56:12 +00:00
|
|
|
|
foreach (var existingSsoUser in ssoUsers)
|
|
|
|
|
{
|
|
|
|
|
existingSsoUser.SsoNameId = null;
|
|
|
|
|
existingSsoUser.SsoSessionId = null;
|
|
|
|
|
|
|
|
|
|
existingSsoUser.ConvertExternalContactsToOrdinary();
|
|
|
|
|
|
|
|
|
|
_userManager.SaveUserInfo(existingSsoUser);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static bool CheckUri(string uriName)
|
|
|
|
|
{
|
2022-05-13 08:52:49 +00:00
|
|
|
|
return Uri.TryCreate(uriName, UriKind.Absolute, out var uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
|
2022-04-28 13:56:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CheckSsoPermissions()
|
|
|
|
|
{
|
|
|
|
|
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
|
|
|
|
|
|
|
|
|
|
if (!_coreBaseSettings.Standalone
|
|
|
|
|
&& (!SetupInfo.IsVisibleSettings(ManagementType.SingleSignOnSettings.ToString())
|
|
|
|
|
|| !_tenantExtra.GetTenantQuota().Sso))
|
|
|
|
|
{
|
|
|
|
|
throw new BillingException(Resource.ErrorNotAllowedOption, "Sso");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|