From de36e7bfd15f760801c65462129649d0412d1240 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Sun, 4 Oct 2026 18:11:36 +0330 Subject: [PATCH] last ... --- Constants/XApiKeyScope.cs | 19 ++++++ Constants/XApplicationType.cs | 7 +++ Extensions/xIdentityModelsExtensions.cs | 53 +++++++++++++++- Models/XApiKey.cs | 84 +++++++++++++++++++++++++ Models/XApiKeyUsage.cs | 43 +++++++++++++ Models/XApplication.cs | 62 ++++++++++++++++++ 6 files changed, 267 insertions(+), 1 deletion(-) create mode 100644 Constants/XApiKeyScope.cs create mode 100644 Constants/XApplicationType.cs create mode 100644 Models/XApiKey.cs create mode 100644 Models/XApiKeyUsage.cs create mode 100644 Models/XApplication.cs diff --git a/Constants/XApiKeyScope.cs b/Constants/XApiKeyScope.cs new file mode 100644 index 0000000..d6d5cc0 --- /dev/null +++ b/Constants/XApiKeyScope.cs @@ -0,0 +1,19 @@ +using xExceptions.Attributes; + +namespace xIdentityModels.Constants +{ + public enum XApiKeyScope + { + [StringValue("read")] + Read, + + [StringValue("write")] + Write, + + [StringValue("admin")] + Admin, + + [StringValue("manage")] + Manage + } +} \ No newline at end of file diff --git a/Constants/XApplicationType.cs b/Constants/XApplicationType.cs new file mode 100644 index 0000000..65db979 --- /dev/null +++ b/Constants/XApplicationType.cs @@ -0,0 +1,7 @@ +namespace xIdentityModels.Constants +{ + public enum XApplicationType + { + Web, Mobile, Desktop, IoT, ThirdParty, Service + } +} \ No newline at end of file diff --git a/Extensions/xIdentityModelsExtensions.cs b/Extensions/xIdentityModelsExtensions.cs index 6daae10..4efbc11 100644 --- a/Extensions/xIdentityModelsExtensions.cs +++ b/Extensions/xIdentityModelsExtensions.cs @@ -5,6 +5,8 @@ using xIdentityModels.Models; using xIdentityModels.Constants; using System.Collections.Generic; using xIdentityModels.Configurations; +using Microsoft.OpenApi.Expressions; +using System; namespace xIdentityModels.Extensions { @@ -161,7 +163,7 @@ namespace xIdentityModels.Extensions ) { // - var result = + var result = !source.IsNullOrDefault() && source.Roles .Any(r => roles @@ -195,5 +197,54 @@ namespace xIdentityModels.Extensions // return result; } + + /// + /// Check Specified Api Key is Revoked or not ... + /// + /// + /// + public static bool IsRevoked(this XApiKey source) + { + // + var result = + !source.IsNullOrDefault() && + source.RevokedAt.HasValue; + + // + return result; + } + + /// + /// Check Specified Api Key is Expired or not ... + /// + /// + /// + public static bool IsExpired(this XApiKey source) + { + // + var result = + !source.IsNullOrDefault() && + DateTime.UtcNow >= source.ExpiresAt; + + // + return result; + } + + /// + /// Check Specified Api Key is Active or not ... + /// + /// + /// + public static bool IsActive(this XApiKey source) + { + // + var result = + !source.IsNullOrDefault() && + !source.IsRevoked() && + !source.IsExpired(); + + // + return result; + } } } \ No newline at end of file diff --git a/Models/XApiKey.cs b/Models/XApiKey.cs new file mode 100644 index 0000000..8b8ec5e --- /dev/null +++ b/Models/XApiKey.cs @@ -0,0 +1,84 @@ +using System; +using xModels.Base; +using System.ComponentModel.DataAnnotations; + +namespace xIdentityModels.Models +{ + /// + /// an Specified Api Key ... + /// + public class XApiKey : XBaseGuidIDEntity + { + /// + /// Related Application Id ... + /// + [Required] + public int ApplicationId { get; set; } + + /// + /// Hash ... + /// SHA256 of ApiKey ... + /// + [Required] + [StringLength(512)] + public string KeyHash { get; set; } + + /// + /// a Prefix ... + /// + [StringLength(100)] + public string KeyPrefix { get; set; } + + /// + /// Expiration Type ... + /// + [Required] + public DateTime ExpiresAt { get; set; } + + /// + /// Cration Time ... + /// + public DateTime CreatedOn { get; set; } + + /// + /// Last Usage Date ... + /// + public DateTime? LastUsedAt { get; set; } + + /// + /// Revoked Time ... + /// + public DateTime? RevokedAt { get; set; } + + /// + /// Revoker User Id ... + /// + public string RevokedBy { get; set; } + + /// + /// a Json Array String ... + /// XApiKeyScope ... + /// + [StringLength(1000)] + public string AllowedScopes { get; set; } + + /// + /// a Comma-separated List which Specified White List IP ... + /// + [StringLength(2000)] + public string AllowedIPs { get; set; } + + /// + /// Rate Limit Per Minute ... + /// + public int RateLimitPerMinute { get; set; } = 60; + + // + #region Navigation ... + /// + /// Related Application ... + /// + public virtual XApplication Application { get; set; } + #endregion + } +} \ No newline at end of file diff --git a/Models/XApiKeyUsage.cs b/Models/XApiKeyUsage.cs new file mode 100644 index 0000000..ab2cb47 --- /dev/null +++ b/Models/XApiKeyUsage.cs @@ -0,0 +1,43 @@ +using System; +using xModels.Base; + +namespace xIdentityModels.Models +{ + public class XApiKeyUsage : XBaseLongIDEntity + { + /// + /// Api Key Id ... + /// + public Guid ApiKeyId { get; set; } + + /// + /// Called Endpoint ... + /// + public string Endpoint { get; set; } + + /// + /// Called Http Method ... + /// + public string HttpMethod { get; set; } + + /// + /// Client IP Address ... + /// + public string ClientIP { get; set; } + + /// + /// Status Code ... + /// + public int StatusCode { get; set; } + + /// + /// Request Time ... + /// + public DateTime RequestedOn { get; set; } + + /// + /// Response Time in Milliseconds ... + /// + public long ResponseTimeMs { get; set; } + } +} \ No newline at end of file diff --git a/Models/XApplication.cs b/Models/XApplication.cs new file mode 100644 index 0000000..7d89082 --- /dev/null +++ b/Models/XApplication.cs @@ -0,0 +1,62 @@ +using System; +using xModels.Base; +using xIdentityModels.Constants; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; + +namespace xIdentityModels.Models +{ + /// + /// an Application in xDashboard ... + /// + public class XApplication : XBaseIntIDEntity + { + /// + /// Name ... + /// + [Required] + [StringLength(255)] + public string Name { get; set; } + + /// + /// Descriptor ... + /// + [StringLength(1000)] + public string Description { get; set; } + + /// + /// Owner Id ... + /// + [Required] + [StringLength(255)] + public string OwnerId { get; set; } + + /// + /// Application Type ... + /// + public XApplicationType Type { get; set; } + + /// + /// Specified Application is Active or not ... + /// + public bool IsActive { get; set; } = true; + + /// + /// Application Create Time ... + /// + public DateTime CreatedOn { get; set; } + + /// + /// Application Update Type ... + /// + public DateTime UpdatedAt { get; set; } + + // + #region Navigation ... + /// + /// a Collection of ApiKeys which Related to this Application ... + /// + public virtual ICollection ApiKeys { get; set; } + #endregion + } +} \ No newline at end of file