DocSpace-client/common/ASC.FederatedLogin/Profile/LoginProfile.cs

379 lines
12 KiB
C#
Raw Normal View History

2022-03-15 18:00:53 +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.FederatedLogin.Profile;
[Serializable]
[DebuggerDisplay("{DisplayName} ({Id})")]
public class LoginProfile
2019-06-06 13:34:46 +00:00
{
public const string QueryParamName = "up";
public const string QuerySessionParamName = "sup";
public const string QueryCacheParamName = "cup";
public string Id
2019-06-06 13:34:46 +00:00
{
get => GetField(WellKnownFields.Id);
internal set => SetField(WellKnownFields.Id, value);
}
2019-06-06 13:34:46 +00:00
public string Link
{
get => GetField(WellKnownFields.Link);
internal set => SetField(WellKnownFields.Link, value);
}
2019-06-06 13:34:46 +00:00
public string Name
{
get => GetField(WellKnownFields.Name);
internal set => SetField(WellKnownFields.Name, value);
}
2019-06-06 13:34:46 +00:00
public string DisplayName
{
get => GetField(WellKnownFields.DisplayName);
internal set => SetField(WellKnownFields.DisplayName, value);
}
2019-06-06 13:34:46 +00:00
public string EMail
{
get => GetField(WellKnownFields.Email);
internal set { SetField(WellKnownFields.Email, value); }
}
2019-06-06 13:34:46 +00:00
public string Avatar
{
get => GetField(WellKnownFields.Avatar);
internal set => SetField(WellKnownFields.Avatar, value);
}
2019-06-06 13:34:46 +00:00
public string Gender
{
get => GetField(WellKnownFields.Gender);
internal set => SetField(WellKnownFields.Gender, value);
}
2019-06-06 13:34:46 +00:00
public string FirstName
{
get => GetField(WellKnownFields.FirstName);
internal set => SetField(WellKnownFields.FirstName, value);
}
2019-06-06 13:34:46 +00:00
public string LastName
{
get => GetField(WellKnownFields.LastName);
internal set => SetField(WellKnownFields.LastName, value);
}
2019-06-06 13:34:46 +00:00
public string MiddleName
{
get => GetField(WellKnownFields.MiddleName);
internal set => SetField(WellKnownFields.MiddleName, value);
}
2019-06-06 13:34:46 +00:00
public string Salutation
{
get => GetField(WellKnownFields.Salutation);
internal set => SetField(WellKnownFields.Salutation, value);
}
2019-06-06 13:34:46 +00:00
public string BirthDay
{
get => GetField(WellKnownFields.BirthDay);
internal set => SetField(WellKnownFields.BirthDay, value);
}
2019-06-06 13:34:46 +00:00
public string Locale
{
get => GetField(WellKnownFields.Locale);
internal set => SetField(WellKnownFields.Locale, value);
}
2019-06-06 13:34:46 +00:00
public string TimeZone
{
get => GetField(WellKnownFields.Timezone);
internal set => SetField(WellKnownFields.Timezone, value);
}
2019-06-06 13:34:46 +00:00
public string AuthorizationResult
{
get => GetField(WellKnownFields.Auth);
internal set => SetField(WellKnownFields.Auth, value);
}
2019-06-06 13:34:46 +00:00
public string AuthorizationError
{
get => GetField(WellKnownFields.AuthError);
internal set => SetField(WellKnownFields.AuthError, value);
}
2019-06-06 13:34:46 +00:00
public string Provider
{
get => GetField(WellKnownFields.Provider);
internal set => SetField(WellKnownFields.Provider, value);
}
2019-06-06 13:34:46 +00:00
public string RealmUrl
{
get => GetField(WellKnownFields.RealmUrl);
internal set => SetField(WellKnownFields.RealmUrl, value);
}
2019-06-06 13:34:46 +00:00
public string Hash
{
get => _signature?.Create(HashId);
set => throw new NotImplementedException();
}
2019-06-06 13:34:46 +00:00
public string Serialized
{
get => Transport();
set => throw new NotImplementedException();
}
2019-06-06 13:34:46 +00:00
public string UserDisplayName
{
get
2019-06-06 13:34:46 +00:00
{
if (!string.IsNullOrEmpty(DisplayName))
{
return DisplayName;
}
2019-06-06 13:34:46 +00:00
var combinedName = string.Join(" ",
new[] { FirstName, MiddleName, LastName }.Where(
x => !string.IsNullOrEmpty(x)).ToArray());
if (string.IsNullOrEmpty(combinedName))
2019-06-06 13:34:46 +00:00
{
combinedName = Name;
2019-06-06 13:34:46 +00:00
}
return combinedName;
2019-06-06 13:34:46 +00:00
}
}
2019-06-06 13:34:46 +00:00
public string UniqueId => $"{Provider}/{Id}";
public string HashId => HashHelper.MD5(UniqueId);
public bool IsFailed => !string.IsNullOrEmpty(AuthorizationError);
public bool IsAuthorized => !IsFailed;
2019-06-06 13:34:46 +00:00
private const char KeyValueSeparator = '→';
private const char PairSeparator = '·';
2019-06-06 13:34:46 +00:00
private readonly Signature _signature;
private readonly InstanceCrypto _instanceCrypto;
private IDictionary<string, string> _fields = new Dictionary<string, string>();
2019-10-09 15:04:46 +00:00
public LoginProfile GetMinimalProfile()
{
var profileNew = new LoginProfile(_signature, _instanceCrypto)
2019-06-06 13:34:46 +00:00
{
Provider = Provider,
Id = Id
};
return profileNew;
}
2019-06-06 13:34:46 +00:00
public static bool HasProfile(HttpContext context)
{
return context != null && HasProfile(context.Request);
}
public static bool HasProfile(HttpRequest request)
{
2022-03-09 17:15:51 +00:00
ArgumentNullException.ThrowIfNull(request);
2019-06-06 13:34:46 +00:00
return new Uri(request.GetDisplayUrl()).HasProfile();
}
public static LoginProfile GetProfile(Signature signature, InstanceCrypto instanceCrypto, HttpContext context, IMemoryCache memoryCache)
{
if (context == null)
{
return new LoginProfile(signature, instanceCrypto);
2019-06-06 13:34:46 +00:00
}
return new Uri(context.Request.GetDisplayUrl()).GetProfile(context, memoryCache, signature, instanceCrypto);
}
public LoginProfile(Signature signature, InstanceCrypto instanceCrypto, string transport) : this(signature, instanceCrypto)
{
FromTransport(transport);
}
2019-06-06 13:34:46 +00:00
public string ToJson()
{
return System.Text.Json.JsonSerializer.Serialize(this);
}
internal string GetField(string name)
{
return _fields.ContainsKey(name) ? _fields[name] : string.Empty;
}
2019-06-06 13:34:46 +00:00
internal void SetField(string name, string value)
{
2022-03-09 17:15:51 +00:00
ArgumentNullException.ThrowIfNull(name);
if (!string.IsNullOrEmpty(value))
{
if (_fields.ContainsKey(name))
2019-06-06 13:34:46 +00:00
{
_fields[name] = value;
2019-06-06 13:34:46 +00:00
}
else
2019-06-06 13:34:46 +00:00
{
_fields.Add(name, value);
2019-06-06 13:34:46 +00:00
}
}
else
{
if (_fields.ContainsKey(name))
2019-06-06 13:34:46 +00:00
{
_fields.Remove(name);
2019-06-06 13:34:46 +00:00
}
}
}
internal Uri AppendProfile(Uri uri)
{
var value = Transport();
return AppendQueryParam(uri, QueryParamName, value);
}
2019-06-06 13:34:46 +00:00
internal static LoginProfile FromError(Signature signature, InstanceCrypto instanceCrypto, Exception e)
{
var profile = new LoginProfile(signature, instanceCrypto) { AuthorizationError = e.Message };
return profile;
}
internal Uri AppendCacheProfile(Uri uri, IMemoryCache memoryCache)
{
//gen key
var key = HashHelper.MD5(Transport());
memoryCache.Set(key, this, TimeSpan.FromMinutes(15));
return AppendQueryParam(uri, QuerySessionParamName, key);
}
2019-06-06 13:34:46 +00:00
internal Uri AppendSessionProfile(Uri uri, HttpContext context)
{
//gen key
var key = HashHelper.MD5(Transport());
context.Session.SetString(key, JsonConvert.SerializeObject(this));
return AppendQueryParam(uri, QuerySessionParamName, key);
}
2019-06-06 13:34:46 +00:00
internal void ParseFromUrl(HttpContext context, Uri uri, IMemoryCache memoryCache)
{
var queryString = HttpUtility.ParseQueryString(uri.Query);
if (!string.IsNullOrEmpty(queryString[QueryParamName]))
2019-06-06 13:34:46 +00:00
{
FromTransport(queryString[QueryParamName]);
2019-06-06 13:34:46 +00:00
}
else if (!string.IsNullOrEmpty(queryString[QuerySessionParamName]))
2019-06-06 13:34:46 +00:00
{
FromTransport(context.Session.GetString(queryString[QuerySessionParamName]));
2019-06-06 13:34:46 +00:00
}
else if (!string.IsNullOrEmpty(queryString[QueryCacheParamName]))
2019-06-06 13:34:46 +00:00
{
FromTransport((string)memoryCache.Get(queryString[QueryCacheParamName]));
2019-06-06 13:34:46 +00:00
}
}
2019-06-06 13:34:46 +00:00
internal string ToSerializedString()
{
return string.Join(new string(PairSeparator, 1), _fields.Select(x => string.Join(new string(KeyValueSeparator, 1), new[] { x.Key, x.Value })).ToArray());
}
internal static LoginProfile CreateFromSerializedString(Signature signature, InstanceCrypto instanceCrypto, string serialized)
{
var profile = new LoginProfile(signature, instanceCrypto);
profile.FromSerializedString(serialized);
return profile;
}
2019-06-06 13:34:46 +00:00
internal void FromSerializedString(string serialized)
{
2022-03-09 17:15:51 +00:00
ArgumentNullException.ThrowIfNull(serialized);
2019-06-06 13:34:46 +00:00
_fields = serialized.Split(PairSeparator).ToDictionary(x => x.Split(KeyValueSeparator)[0], y => y.Split(KeyValueSeparator)[1]);
}
internal string Transport()
{
return WebEncoders.Base64UrlEncode(_instanceCrypto.Encrypt(Encoding.UTF8.GetBytes(ToSerializedString())));
}
internal void FromTransport(string transportstring)
{
var serialized = _instanceCrypto.Decrypt(WebEncoders.Base64UrlDecode(transportstring));
FromSerializedString(serialized);
}
internal LoginProfile(Signature signature, InstanceCrypto instanceCrypto)
{
_signature = signature;
_instanceCrypto = instanceCrypto;
}
protected LoginProfile(Signature signature, InstanceCrypto instanceCrypto, SerializationInfo info) : this(signature, instanceCrypto)
{
2022-03-09 17:15:51 +00:00
ArgumentNullException.ThrowIfNull(info);
2019-06-06 13:34:46 +00:00
var transformed = (string)info.GetValue(QueryParamName, typeof(string));
FromTransport(transformed);
}
private static Uri AppendQueryParam(Uri uri, string keyvalue, string value)
{
var queryString = HttpUtility.ParseQueryString(uri.Query);
if (!string.IsNullOrEmpty(queryString[keyvalue]))
2019-06-06 13:34:46 +00:00
{
queryString[keyvalue] = value;
2019-06-06 13:34:46 +00:00
}
else
2019-06-06 13:34:46 +00:00
{
queryString.Add(keyvalue, value);
2019-06-06 13:34:46 +00:00
}
var query = new StringBuilder();
2019-06-06 13:34:46 +00:00
foreach (var key in queryString.AllKeys)
2019-06-06 13:34:46 +00:00
{
query.Append($"{key}={queryString[key]}&");
}
var builder = new UriBuilder(uri) { Query = query.ToString().TrimEnd('&') };
return builder.Uri;
2019-06-06 13:34:46 +00:00
}
}