diff --git a/Extensions/xIdentityModelsExtensions.cs b/Extensions/xIdentityModelsExtensions.cs
index 949ca55..1058cf4 100644
--- a/Extensions/xIdentityModelsExtensions.cs
+++ b/Extensions/xIdentityModelsExtensions.cs
@@ -6,6 +6,7 @@ using xIdentityModels.Models;
using xIdentityModels.Constants;
using System.Collections.Generic;
using xIdentityModels.Configurations;
+using xExceptions.Constants;
namespace xIdentityModels.Extensions
{
@@ -173,6 +174,217 @@ namespace xIdentityModels.Extensions
return result;
}
+ ///
+ /// Extract Top XApiKey Scope inside an Enumerable ...
+ ///
+ ///
+ ///
+ public static XApiKeyScope GetTopXApiKeyScope(
+ this IEnumerable scopes
+ )
+ {
+ //
+ // Check Scopes Has Child ...
+ if (!scopes.HasChild())
+ {
+ XException.NotFound.Throw();
+ }
+
+ //
+ var hasAdmin = scopes
+ .Any(s =>
+ s.GetStringValue().ToNormalString() == XApiKeyScope.Admin.GetStringValue().ToNormalString());
+ var hasRead = scopes
+ .Any(s =>
+ s.GetStringValue().ToNormalString() == XApiKeyScope.Read.GetStringValue().ToNormalString());
+ var hasWrite = scopes
+ .Any(s =>
+ s.GetStringValue().ToNormalString() == XApiKeyScope.Write.GetStringValue().ToNormalString());
+ var hasManage =
+ (hasRead && hasWrite) ||
+ scopes
+ .Any(s =>
+ s.GetStringValue().ToNormalString() == XApiKeyScope.Manage.GetStringValue().ToNormalString());
+
+ //
+ // Admin ...
+ if (hasAdmin)
+ {
+ return XApiKeyScope.Admin;
+ }
+ else if (hasManage)
+ {
+ return XApiKeyScope.Manage;
+ }
+ else if (hasWrite)
+ {
+ return XApiKeyScope.Write;
+ }
+ else
+ {
+ return XApiKeyScope.Read;
+ }
+ }
+
+ ///
+ /// Check Source ApiKey Scope Has Access to Dest ...
+ ///
+ ///
+ ///
+ ///
+ public static bool HasAccess(
+ this XApiKeyScope source,
+ XApiKeyScope dest
+ )
+ {
+ //
+ var result = source == dest;
+ if (result)
+ {
+ return result;
+ }
+
+ //
+ var alls = Enumerable.Empty();
+ switch (source)
+ {
+ //
+ // Admin ...
+ case XApiKeyScope.Admin:
+ //
+ alls = [
+ XApiKeyScope.Admin,
+ XApiKeyScope.Manage,
+ XApiKeyScope.Write,
+ XApiKeyScope.Read
+ ];
+
+ //
+ result = alls.Contains(dest);
+ break;
+
+ //
+ // Manage ...
+ case XApiKeyScope.Manage:
+ //
+ alls = [
+ XApiKeyScope.Manage,
+ XApiKeyScope.Write,
+ XApiKeyScope.Read
+ ];
+
+ //
+ result = alls.Contains(dest);
+ break;
+
+ //
+ // Write ...
+ case XApiKeyScope.Write:
+ //
+ alls = [XApiKeyScope.Write];
+
+ //
+ result = alls.Contains(dest);
+ break;
+
+ //
+ // Read ...
+ case XApiKeyScope.Read:
+ //
+ alls = [XApiKeyScope.Read];
+
+ //
+ result = alls.Contains(dest);
+ break;
+ }
+
+ //
+ return result;
+ }
+
+ ///
+ /// Check Allowed Scopes passed Based on Required Scopes ...
+ ///
+ ///
+ ///
+ ///
+ public static bool IsScopePassed(
+ this IEnumerable allowedScopes,
+ IEnumerable requiredScopes
+ )
+ {
+ //
+ // if Allowed Scopes is Empty, this means access to
+ // All Scopes are Valid ...
+ var result = !allowedScopes.HasChild();
+ if (result)
+ {
+ return result;
+ }
+
+ //
+ // Make Sure we Have at least one Required Scope for Validation ...
+ result = requiredScopes.HasChild();
+ if (!result)
+ {
+ return result;
+ }
+
+ //
+ // Converts each Allowed or Required Scopes to XApiKeyScope ...
+ var alloweds = allowedScopes.Select(asc =>
+ {
+ //
+ if (asc.ToNormalString() == XApiKeyScope.Admin.GetStringValue().ToNormalString())
+ {
+ return XApiKeyScope.Admin;
+ }
+ else if (asc.ToNormalString() == XApiKeyScope.Manage.GetStringValue().ToNormalString())
+ {
+ return XApiKeyScope.Manage;
+ }
+ else if (asc.ToNormalString() == XApiKeyScope.Write.GetStringValue().ToNormalString())
+ {
+ return XApiKeyScope.Write;
+ }
+ else
+ {
+ return XApiKeyScope.Read;
+ }
+ });
+ var requireds = requiredScopes.Select(asc =>
+ {
+ //
+ if (asc.ToNormalString() == XApiKeyScope.Admin.GetStringValue().ToNormalString())
+ {
+ return XApiKeyScope.Admin;
+ }
+ else if (asc.ToNormalString() == XApiKeyScope.Manage.GetStringValue().ToNormalString())
+ {
+ return XApiKeyScope.Manage;
+ }
+ else if (asc.ToNormalString() == XApiKeyScope.Write.GetStringValue().ToNormalString())
+ {
+ return XApiKeyScope.Write;
+ }
+ else
+ {
+ return XApiKeyScope.Read;
+ }
+ });
+
+ //
+ // Retrieve top Allowed ApiKey Scope ...
+ var topAllowed = alloweds.GetTopXApiKeyScope();
+ var topRequired = requireds.GetTopXApiKeyScope();
+
+ //
+ result = topAllowed.HasAccess(topRequired);
+
+ //
+ return result;
+ }
+
///
/// Check Specified User Contains Specified UserSelectByParam or not ...
///
@@ -205,7 +417,7 @@ namespace xIdentityModels.Extensions
public static bool IsRevoked(this XApiKey source)
{
//
- var result =
+ var result =
!source.IsNullOrDefault() &&
source.RevokedAt.HasValue;