375 lines
14 KiB
C#
375 lines
14 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Security.Claims;
|
|
using Kreta.BusinessLogic.Security;
|
|
using Kreta.Framework.Security;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace Kreta.Web.Security
|
|
{
|
|
/// <summary>
|
|
/// Extension methods for ClaimsIdentity
|
|
/// </summary>
|
|
public static class ClaimsIdentityExtensions
|
|
{
|
|
/// <summary>
|
|
/// Checks whether a given claim exists
|
|
/// </summary>
|
|
/// <param name="identity">The identity.</param>
|
|
/// <param name="predicate">The search predicate.</param>
|
|
/// <returns>true/false</returns>
|
|
public static bool ClaimExists(this ClaimsIdentity identity, Predicate<Claim> predicate)
|
|
{
|
|
|
|
Claim claim = identity.FindClaims(predicate).FirstOrDefault();
|
|
return claim != null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks whether a given claim exists
|
|
/// </summary>
|
|
/// <param name="identity">The identity.</param>
|
|
/// <param name="claimType">Type of the claim.</param>
|
|
/// <returns>true/false</returns>
|
|
public static bool ClaimExists(this ClaimsIdentity identity, string claimType)
|
|
{
|
|
return identity.ClaimExists(c =>
|
|
c.Type.Equals(claimType, StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks whether a given claim exists
|
|
/// </summary>
|
|
/// <param name="identity">The identity.</param>
|
|
/// <param name="claimType">Type of the claim.</param>
|
|
/// <param name="value">The value.</param>
|
|
/// <returns>true/false</returns>
|
|
public static bool ClaimExists(this ClaimsIdentity identity, string claimType, string value)
|
|
{
|
|
return identity.ClaimExists(c =>
|
|
c.Type.Equals(claimType, StringComparison.OrdinalIgnoreCase) &&
|
|
c.Value.Equals(value, StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks whether a given claim exists
|
|
/// </summary>
|
|
/// <param name="identity">The identity.</param>
|
|
/// <param name="claimType">Type of the claim.</param>
|
|
/// <param name="value">The value.</param>
|
|
/// <param name="issuer">The issuer.</param>
|
|
/// <returns>true/false</returns>
|
|
public static bool ClaimExists(this ClaimsIdentity identity, string claimType, string value, string issuer)
|
|
{
|
|
|
|
return identity.ClaimExists(c =>
|
|
c.Type.Equals(claimType, StringComparison.OrdinalIgnoreCase) &&
|
|
c.Value.Equals(value, StringComparison.OrdinalIgnoreCase) &&
|
|
c.Issuer.Equals(issuer, StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Demands a specific claim.
|
|
/// </summary>
|
|
/// <param name="identity">The principal.</param>
|
|
/// <param name="predicate">The search predicate.</param>
|
|
public static void DemandClaim(this ClaimsIdentity identity, Predicate<Claim> predicate)
|
|
{
|
|
if (!identity.ClaimExists(predicate))
|
|
{
|
|
throw new SecurityException();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Demands a specific claim.
|
|
/// </summary>
|
|
/// <param name="identity">The identity.</param>
|
|
/// <param name="claimType">Type of the claim.</param>
|
|
public static void DemandClaim(this ClaimsIdentity identity, string claimType)
|
|
{
|
|
try
|
|
{
|
|
identity.DemandClaim(claim =>
|
|
claim.Type.Equals(claimType, StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
catch (SecurityException)
|
|
{
|
|
throw new SecurityException();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Demands a specific claim.
|
|
/// </summary>
|
|
/// <param name="identity">The identity.</param>
|
|
/// <param name="claimType">Type of the claim.</param>
|
|
/// <param name="value">The value.</param>
|
|
public static void DemandClaim(this ClaimsIdentity identity, string claimType, string value)
|
|
{
|
|
|
|
try
|
|
{
|
|
identity.DemandClaim(claim =>
|
|
claim.Type.Equals(claimType, StringComparison.OrdinalIgnoreCase) &&
|
|
claim.Value.Equals(value, StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
catch (SecurityException)
|
|
{
|
|
throw new SecurityException();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Demands a specific claim.
|
|
/// </summary>
|
|
/// <param name="identity">The identity.</param>
|
|
/// <param name="claimType">Type of the claim.</param>
|
|
/// <param name="value">The value.</param>
|
|
/// <param name="issuer">The issuer.</param>
|
|
public static void DemandClaim(this ClaimsIdentity identity, string claimType, string value, string issuer)
|
|
{
|
|
try
|
|
{
|
|
identity.DemandClaim(claim =>
|
|
claim.Type.Equals(claimType, StringComparison.OrdinalIgnoreCase) &&
|
|
claim.Value.Equals(value, StringComparison.OrdinalIgnoreCase) &&
|
|
claim.Issuer.Equals(issuer, StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
catch (SecurityException)
|
|
{
|
|
throw new SecurityException();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Denies a specific claim.
|
|
/// </summary>
|
|
/// <param name="principal">The identity.</param>
|
|
/// <param name="predicate">The search predicate.</param>
|
|
public static void DenyClaim(this ClaimsIdentity identity, Predicate<Claim> predicate)
|
|
{
|
|
foreach (Claim claim in identity.FindClaims(predicate))
|
|
{
|
|
throw new SecurityException();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Denies a specific claim.
|
|
/// </summary>
|
|
/// <param name="principal">The identity.</param>
|
|
/// <param name="claimType">Type of the claim.</param>
|
|
public static void DenyClaim(this ClaimsIdentity identity, string claimType)
|
|
{
|
|
try
|
|
{
|
|
identity.DenyClaim(claim =>
|
|
claim.Type.Equals(claimType, StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
catch (SecurityException)
|
|
{
|
|
throw new SecurityException();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Denies a specific claim.
|
|
/// </summary>
|
|
/// <param name="principal">The identity.</param>
|
|
/// <param name="claimType">Type of the claim.</param>
|
|
/// <param name="value">The value.</param>
|
|
public static void DenyClaim(this ClaimsIdentity identity, string claimType, string value)
|
|
{
|
|
try
|
|
{
|
|
identity.DenyClaim(claim =>
|
|
claim.Type.Equals(claimType, StringComparison.OrdinalIgnoreCase) &&
|
|
claim.Value.Equals(value, StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
catch (SecurityException)
|
|
{
|
|
throw new SecurityException();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Denies a specific claim.
|
|
/// </summary>
|
|
/// <param name="principal">The identity.</param>
|
|
/// <param name="claimType">Type of the claim.</param>
|
|
/// <param name="value">The value.</param>
|
|
/// <param name="issuer">The issuer.</param>
|
|
public static void DenyClaim(this ClaimsIdentity identity, string claimType, string value, string issuer)
|
|
{
|
|
try
|
|
{
|
|
identity.DenyClaim(claim =>
|
|
claim.Type.Equals(claimType, StringComparison.OrdinalIgnoreCase) &&
|
|
claim.Value.Equals(value, StringComparison.OrdinalIgnoreCase) &&
|
|
claim.Issuer.Equals(issuer, StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
catch (SecurityException)
|
|
{
|
|
throw new SecurityException();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Finds all instances of the specified claim.
|
|
/// </summary>
|
|
/// <param name="identity">The identity.</param>
|
|
/// <param name="predicate">The search predicate.</param>
|
|
/// <returns>List of claims that match the search criteria</returns>
|
|
public static IEnumerable<Claim> FindClaims(this ClaimsIdentity identity, Predicate<Claim> predicate)
|
|
{
|
|
return from claim in identity.Claims
|
|
where predicate(claim)
|
|
select claim;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Finds all instances of the specified claim.
|
|
/// </summary>
|
|
/// <param name="identity">The identity.</param>
|
|
/// <param name="claimType">Type of the claim.</param>
|
|
/// <returns>List of claims that match the search criteria</returns>
|
|
public static IEnumerable<Claim> FindClaims(this ClaimsIdentity identity, string claimType)
|
|
{
|
|
return identity.FindClaims(c =>
|
|
c.Type.Equals(claimType, StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Finds all instances of the specified claim.
|
|
/// </summary>
|
|
/// <param name="identity">The identity.</param>
|
|
/// <param name="claimType">Type of the claim.</param>
|
|
/// <param name="issuer">The issuer.</param>
|
|
/// <returns>List of claims that match the search criteria</returns>
|
|
public static IEnumerable<Claim> FindClaims(this ClaimsIdentity identity, string claimType, string issuer)
|
|
{
|
|
return identity.FindClaims(c =>
|
|
c.Type.Equals(claimType, StringComparison.OrdinalIgnoreCase) &&
|
|
c.Issuer.Equals(issuer, StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Finds all instances of the specified claim.
|
|
/// </summary>
|
|
/// <param name="identity">The identity.</param>
|
|
/// <param name="claimType">Type of the claim.</param>
|
|
/// <param name="issuer">The issuer.</param>
|
|
/// <param name="value">The value.</param>
|
|
/// <returns>List of claims that match the search criteria</returns>
|
|
public static IEnumerable<Claim> FindClaims(this ClaimsIdentity identity, string claimType, string issuer, string value)
|
|
{
|
|
return identity.FindClaims(c =>
|
|
c.Type.Equals(claimType, StringComparison.OrdinalIgnoreCase) &&
|
|
c.Value.Equals(value, StringComparison.OrdinalIgnoreCase) &&
|
|
c.Issuer.Equals(issuer, StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Finds all instances of the specified claim.
|
|
/// </summary>
|
|
/// <param name="identity">The identity.</param>
|
|
/// <param name="claim">Search claim.</param>
|
|
/// <returns>List of claims that match the search criteria</returns>
|
|
public static IEnumerable<Claim> FindClaims(this ClaimsIdentity identity, Claim claim)
|
|
{
|
|
return identity.FindClaims(c =>
|
|
c.Type.Equals(claim.Type, StringComparison.OrdinalIgnoreCase) &&
|
|
c.Value.Equals(claim.Value, StringComparison.OrdinalIgnoreCase) &&
|
|
c.Issuer.Equals(claim.Issuer, StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieves the value of a claim.
|
|
/// </summary>
|
|
/// <param name="identity">The identity.</param>
|
|
/// <param name="claimType">Type of the claim.</param>
|
|
/// <returns>The value</returns>
|
|
public static string GetClaimValue(this ClaimsIdentity identity, string claimType)
|
|
{
|
|
string value = null;
|
|
if (identity.TryGetClaimValue(claimType, out value))
|
|
{
|
|
return value;
|
|
}
|
|
|
|
throw new Exception("Claim not found exception");// ClaimNotFoundException(string.Format(CultureInfo.CurrentCulture, Messages.ClaimNotFound, claimType));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieves the value of a claim.
|
|
/// </summary>
|
|
/// <param name="identity">The identity.</param>
|
|
/// <param name="claimType">Type of the claim.</param>
|
|
/// <param name="issuer">The issuer.</param>
|
|
/// <returns>The value</returns>
|
|
public static string GetClaimValue(this ClaimsIdentity identity, string claimType, string issuer)
|
|
{
|
|
string value = null;
|
|
if (identity.TryGetClaimValue(claimType, issuer, out value))
|
|
{
|
|
return value;
|
|
}
|
|
|
|
throw new Exception("Claim not found exception"); //throw new ClaimNotFoundException(string.Format(CultureInfo.CurrentCulture, Messages.ClaimNotFound, claimType));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tries to retrieve the value of a claim.
|
|
/// </summary>
|
|
/// <param name="identity">The identity.</param>
|
|
/// <param name="claimType">Type of the claim.</param>
|
|
/// <param name="claimValue">The claim value.</param>
|
|
/// <returns>The value</returns>
|
|
public static bool TryGetClaimValue(this ClaimsIdentity identity, string claimType, out string claimValue)
|
|
{
|
|
|
|
claimValue = null;
|
|
Claim claim = identity.FindClaims(claimType).FirstOrDefault();
|
|
|
|
if (claim != null)
|
|
{
|
|
claimValue = claim.Value;
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tries to retrieve the value of a claim.
|
|
/// </summary>
|
|
/// <param name="identity">The identity.</param>
|
|
/// <param name="claimType">Type of the claim.</param>
|
|
/// <param name="issuer">The issuer.</param>
|
|
/// <param name="claimValue">The claim value.</param>
|
|
/// <returns>The value</returns>
|
|
public static bool TryGetClaimValue(this ClaimsIdentity identity, string claimType, string issuer, out string claimValue)
|
|
{
|
|
claimValue = null;
|
|
Claim claim = identity.FindClaims(claimType, issuer).FirstOrDefault();
|
|
|
|
if (claim != null)
|
|
{
|
|
claimValue = claim.Value;
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public static bool HasResource(this ClaimsIdentity identity, string resourceName)
|
|
{
|
|
var resourcesJSON = identity.GetClaimValue(KretaClaimTypes.Resource);
|
|
var resources = JsonConvert.DeserializeObject<List<string>>(resourcesJSON);
|
|
|
|
return resources.Contains(resourceName);
|
|
}
|
|
}
|
|
}
|