Files
xIdentityModels/Models/XUserClaimsInfoDto.cs
T
2024-01-25 04:42:47 +03:30

195 lines
7.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using xCommons.Constants;
using xCommons.Extensions;
using xCommons.Helpers;
using xExceptions.Constants;
using xIdentityModels.Constants;
using xModels.Base;
namespace xIdentityModels.Models {
public class XUserClaimsInfoDto : XBaseDto {
//
#region Properties ...
public string AccessToken { get; }
public string RefreshToken { get; }
public long ExpiresAt { get; }
public string UserId { get; }
public string UserName { get; }
public string FirstName { get; }
public string LastName { get; }
public string Picture { get; }
public string Email { get; }
public string PhoneNumber { get; }
public XGender Gender { get; }
public bool IsBanned { get; }
public bool IsEnabled { get; }
public bool EmailConfirmed { get; }
public bool PhoneNumberConfirmed { get; }
public long? ExpiredOn { get; }
public long? AuthenticatedOn { get; }
public DateTime? BrithDate { get; }
public IEnumerable<string> Issuers { get; } = new HashSet<string> ();
public IEnumerable<string> Audiences { get; } = new HashSet<string> ();
public IEnumerable<string> ClientIds { get; } = new HashSet<string> ();
public IEnumerable<string> Scopes { get; } = new HashSet<string> ();
public IEnumerable<string> Roles { get; } = new HashSet<string> ();
#endregion
//
public XUserClaimsInfoDto () { }
public XUserClaimsInfoDto (
string accessToken,
string refreshToken,
long expiresAt,
IEnumerable<Claim> claims
) {
//
// refreshToken.IsNullOrEmpty ()
if (claims.IsNull () ||
accessToken.IsNullOrEmpty ()) {
XException.InvalidArgs.Throw ();
}
//
// Attach XLogin Properties ...
AccessToken = accessToken;
RefreshToken = refreshToken;
ExpiresAt = expiresAt;
//
if (!claims.IsNull ()) {
UserId = claims.FirstOrDefault (c => c.Type == XCustomClaims.UserId)?.Value ?? "";
UserName = claims.FirstOrDefault (c => c.Type == XCustomClaims.UserName)?.Value ?? "";
FirstName = claims.FirstOrDefault (c => c.Type == XCustomClaims.FirstName)?.Value ?? "";
LastName = claims.FirstOrDefault (c => c.Type == XCustomClaims.LastName)?.Value ?? "";
Picture = claims.FirstOrDefault (c => c.Type == XCustomClaims.Picture)?.Value ?? "";
Email = claims.FirstOrDefault (c => c.Type == XCustomClaims.Email)?.Value ?? null;
PhoneNumber = claims.FirstOrDefault (c => c.Type == XCustomClaims.PhoneNumber)?.Value ?? null;
//
// Handle Gender ...
var genders = ObjectHelper.ToEnumerableKeys<XGender> ();
var genderStr = claims.FirstOrDefault (c => c.Type == XCustomClaims.Gender).Value ?? null;
if (genderStr.IsNullOrEmpty ()) {
Gender = XGender.Male;
} else {
Gender = genderStr.GetValue<XGender> ();
}
//
// Handle IsBanned ...
var isBannedStr = claims.FirstOrDefault (c => c.Type == XCustomClaims.IsBanned).Value ?? null;
if (isBannedStr.IsNullOrEmpty ()) {
IsBanned = false;
} else {
//
var isBanned = false;
Boolean.TryParse (isBannedStr, out isBanned);
//
IsBanned = isBanned;
}
//
// Handle IsEnabled ...
var isEnabledStr = claims.FirstOrDefault (c => c.Type == XCustomClaims.IsEnabled).Value ?? null;
if (isEnabledStr.IsNullOrEmpty ()) {
IsEnabled = false;
} else {
//
var isEnabled = false;
Boolean.TryParse (isEnabledStr, out isEnabled);
//
IsEnabled = isEnabled;
}
//
// Handle Email Confirmed ...
var emailConfirmedStr = claims.FirstOrDefault (c => c.Type == XCustomClaims.EmailVerified).Value ?? null;
if (emailConfirmedStr.IsNullOrEmpty ()) {
EmailConfirmed = false;
} else {
//
var emailConfirmed = false;
Boolean.TryParse (emailConfirmedStr, out emailConfirmed);
//
EmailConfirmed = emailConfirmed;
}
//
// Handle PhoneNumber Confirmed ...
var phoneNumberConfirmedStr = claims.FirstOrDefault (c => c.Type == XCustomClaims.PhoneNumberVerified).Value ?? null;
if (phoneNumberConfirmedStr.IsNullOrEmpty ()) {
PhoneNumberConfirmed = false;
} else {
//
var phoneNumberConfirmed = false;
Boolean.TryParse (phoneNumberConfirmedStr, out phoneNumberConfirmed);
//
PhoneNumberConfirmed = phoneNumberConfirmed;
}
//
// Handle ExpiredOn ...
var expiredOnStr = claims.FirstOrDefault (c => c.Type == XCustomClaims.ExpiredOn).Value ?? null;
if (expiredOnStr.IsNullOrEmpty ()) {
ExpiredOn = null;
} else {
//
long expiredOn = 0;
long.TryParse (expiredOnStr, out expiredOn);
//
ExpiredOn = expiredOn;
}
//
// Handle AuthenticatedOn ...
var authenticatedOnStr = claims.FirstOrDefault (c => c.Type == XCustomClaims.AuthenticatedOn).Value ?? null;
if (authenticatedOnStr.IsNullOrEmpty ()) {
AuthenticatedOn = null;
} else {
//
long authenticatedOn = 0;
long.TryParse (authenticatedOnStr, out authenticatedOn);
//
AuthenticatedOn = authenticatedOn;
}
//
// Handle BrithDate ...
var brithDateStr = claims.FirstOrDefault (c => c.Type == XCustomClaims.BrithDate)?.Value ?? "";
if (brithDateStr.IsNullOrEmpty ()) {
// BrithDate = null;
} else {
//
DateTime bDate;
DateTime.TryParse (brithDateStr, out bDate);
//
BrithDate = bDate;
}
//
Issuers = claims.Where (c => c.Type == XCustomClaims.Issuer) ?
.Select (c => c.Value) ?? new HashSet<string> ();
Audiences = claims.Where (c => c.Type == XCustomClaims.Audience) ?
.Select (c => c.Value) ?? new HashSet<string> ();
ClientIds = claims.Where (c => c.Type == XCustomClaims.ClientId) ?
.Select (c => c.Value) ?? new HashSet<string> ();
Scopes = claims.Where (c => c.Type == XCustomClaims.Scope) ?
.Select (c => c.Value) ?? new HashSet<string> ();
Roles = claims.Where (c => c.Type == XCustomClaims.Role) ?
.Select (c => c.Value) ?? new HashSet<string> ();
}
}
}
}