Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Claims #31

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions Descope/Internal/Authentication/Authentication.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.IdentityModel.JsonWebTokens;
using Microsoft.IdentityModel.JsonWebTokens;
using Microsoft.IdentityModel.Tokens;
using System.Security.Cryptography;
using System.Text.Json.Serialization;
Expand Down Expand Up @@ -180,9 +180,9 @@ private static bool ValidateAgainstClaims(Token token, string claim, List<string
var claimItems = GetAuthorizationClaimItems(token, claim, tenant);
foreach (var value in values)
{
if (!claimItems.Contains(value)) return false;
if (claimItems.Contains(value)) return true;
}
return true;
return false;
}

private static List<string> GetMatchedClaimValues(Token token, string claim, List<string> values, string? tenant)
Expand Down Expand Up @@ -285,6 +285,4 @@ public RSAParameters ToRsaParameters()
return rsaParameters;
}
}


}
26 changes: 20 additions & 6 deletions Descope/Types/Types.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.IdentityModel.JsonWebTokens;

Expand Down Expand Up @@ -156,19 +156,33 @@ public Token(JsonWebToken jsonWebToken)
}
}

internal List<string> GetTenants()
public List<string> GetTenants()
{
return new List<string>(GetTenantsClaim().Keys);
}

internal object? GetTenantValue(string tenant, string key)
public List<string> GetTenantValue(string tenant, string key)
{
return (GetTenantsClaim()[tenant] is Dictionary<string, object> info) ? info[key] : null;
var tenantsClaimJson = GetTenantsClaim()[tenant].ToString();
if (string.IsNullOrEmpty(tenantsClaimJson))
return new List<string>();
var tenantsClaim = JsonSerializer.Deserialize<Dictionary<string, object>>(tenantsClaimJson)
?? new Dictionary<string, object>();
if (!tenantsClaim.TryGetValue(key, out var tenants))
return new List<string>();

var tenatnsJson = tenants.ToString() ?? string.Empty;
return JsonSerializer.Deserialize<List<string>>(tenatnsJson) ?? new List<string>();
}

private Dictionary<string, object> GetTenantsClaim()
public Dictionary<string, object> GetTenantsClaim()
{
return Claims["tenants"] as Dictionary<string, object> ?? new Dictionary<string, object>();
if (!Claims.TryGetValue("tenants", out var value))
return new Dictionary<string, object>();
var tenants = value.ToString();
if (string.IsNullOrEmpty(tenants))
return new Dictionary<string, object>();
return JsonSerializer.Deserialize<Dictionary<string, object>>(tenants) ?? new Dictionary<string, object>();
}
}

Expand Down