1818using System . Linq . Expressions ;
1919using System . Net ;
2020using System . Net . Http . Headers ;
21+ using System . Security . AccessControl ;
2122using System . Security . Claims ;
2223using System . Security . Cryptography ;
2324using System . Security . Cryptography . X509Certificates ;
@@ -88,12 +89,14 @@ public void parseAccessRuleFile()
8889 }
8990 }
9091
91- public List < AccessPermissionRule > ? GetAccessRules ( string accessRole , string neededRightsClaim , string ? httpRoute = null , List < Claim > ? tokenClaims = null )
92+ public List < AccessPermissionRule > ? GetAccessRules ( string accessRole , string neededRightsClaim , string ? httpRoute = null ,
93+ List < Claim > ? tokenClaims = null )
9294 {
9395 return GetAccessRulesStatic ( accessRole , neededRightsClaim , httpRoute , tokenClaims ) ;
9496 }
9597
96- public static List < AccessPermissionRule > ? GetAccessRulesStatic ( string accessRole , string neededRightsClaim , string ? httpRoute = null , List < Claim > ? tokenClaims = null )
98+ public static List < AccessPermissionRule > ? GetAccessRulesStatic ( string accessRole , string neededRightsClaim , string ? httpRoute = null ,
99+ List < Claim > ? tokenClaims = null )
97100 {
98101 if ( _accessRules != null )
99102 {
@@ -124,7 +127,7 @@ public void parseAccessRuleFile()
124127 a . ItemType == "CLAIM" &&
125128 (
126129 ( accessRole != null && a . Value == accessRole ) ||
127- ( tokenClaims != null && tokenClaims . Any ( t => t . ValueType == "token:" + a . Value ) )
130+ ( tokenClaims != null && tokenClaims . Any ( t => t . Type == a . Value ) )
128131 )
129132 ) &&
130133 (
@@ -147,7 +150,7 @@ public void parseAccessRuleFile()
147150 }
148151 public Dictionary < string , string > ? GetCondition ( string accessRole , string neededRightsClaim , string ? httpRoute = null , List < Claim > ? tokenClaims = null )
149152 {
150- var rules = GetAccessRules ( accessRole , neededRightsClaim ) ;
153+ var rules = GetAccessRules ( accessRole , neededRightsClaim , tokenClaims : tokenClaims ) ;
151154
152155 if ( rules == null )
153156 {
@@ -315,7 +318,7 @@ public void AddSecurityRule(string name, string access, string right, string obj
315318 }
316319
317320 var accessRole = GetAccessRole ( queries , headers , out var policy , out var policyRequestedResource , out var tokenClaims ) ;
318- if ( string . IsNullOrEmpty ( accessRole ) )
321+ if ( string . IsNullOrEmpty ( accessRole ) && tokenClaims . Count == 0 )
319322 {
320323 _logger . LogDebug ( $ "Access Role found null. Hence setting the access role as isNotAuthenticated.") ;
321324 accessRole = "isNotAuthenticated" ;
@@ -807,22 +810,24 @@ private string CheckUserPW(string userPW64, out string userName, out string pass
807810
808811 public bool AuthorizeRequest ( string accessRole , string httpRoute , AccessRights neededRights ,
809812 out string error , out bool withAllow , out string ? getPolicy , string objPath = null , string ? aasResourceType = null ,
810- IClass ? aasResource = null , string ? policy = null )
813+ IClass ? aasResource = null , string ? policy = null , List < Claim > ? tokenClaims = null )
811814 {
812- return CheckAccessRights ( accessRole , httpRoute , neededRights , out error , out withAllow , out getPolicy , objPath , aasResourceType , aasResource , policy : policy ) ;
815+ return CheckAccessRights ( accessRole , httpRoute , neededRights , out error , out withAllow , out getPolicy ,
816+ objPath , aasResourceType , aasResource , policy : policy , tokenClaims : tokenClaims ) ;
813817 }
814818
815819 private static bool CheckAccessRights ( string currentRole , string operation , AccessRights neededRights , out string error , out bool withAllow , out string ? getPolicy ,
816- string objPath = "" , string ? aasResourceType = null , IClass ? aasResource = null , bool testOnly = false , string ? policy = null )
820+ string objPath = "" , string ? aasResourceType = null , IClass ? aasResource = null , bool testOnly = false , string ? policy = null ,
821+ List < Claim > ? tokenClaims = null )
817822 {
818823 withAllow = false ;
819824 return CheckAccessRightsWithAllow ( currentRole , operation , neededRights , out error , out withAllow , out getPolicy ,
820- objPath , aasResourceType , aasResource , testOnly , policy ) ;
825+ objPath , aasResourceType , aasResource , testOnly , policy , tokenClaims ) ;
821826 }
822827
823828 private static bool CheckAccessRightsWithAllow ( string currentRole , string operation , AccessRights neededRights , out string error , out bool withAllow , out string ? getPolicy ,
824829 string objPath = "" , string ? aasResourceType = null , IClass ? aasResource = null , bool testOnly = false ,
825- string ? policy = null )
830+ string ? policy = null , List < Claim > ? tokenClaims = null )
826831 {
827832 error = "Access not allowed" ;
828833 withAllow = false ;
@@ -837,7 +842,7 @@ private static bool CheckAccessRightsWithAllow(string currentRole, string operat
837842 // TODO (jtikekar, 2023-09-04): uncomment
838843 if ( CheckAccessLevelWithError (
839844 out error , currentRole , operation , neededRights , out withAllow , out getPolicy ,
840- objPath , aasResourceType , aasResource , policy ) )
845+ objPath , aasResourceType , aasResource , policy , tokenClaims ) )
841846 return true ;
842847 }
843848
@@ -852,7 +857,8 @@ private static bool CheckAccessRightsWithAllow(string currentRole, string operat
852857 }
853858
854859 private static bool CheckAccessLevelWithError ( out string error , string currentRole , string operation , AccessRights neededRights , out bool withAllow , out string ? getPolicy ,
855- string objPath , string ? aasResourceType , IClass ? aasResource , string ? policy = null )
860+ string objPath , string ? aasResourceType , IClass ? aasResource , string ? policy = null ,
861+ List < Claim > ? tokenClaims = null )
856862 {
857863 withAllow = false ;
858864 getPolicy = "" ;
@@ -872,7 +878,7 @@ private static bool CheckAccessLevelWithError(out string error, string currentRo
872878 if ( aasResource == null )
873879 {
874880 //API security check
875- return CheckAccessLevelApi ( currentRole , operation , neededRights , out error , out getPolicy ) ;
881+ return CheckAccessLevelApi ( currentRole , operation , neededRights , out error , out getPolicy , tokenClaims ) ;
876882 }
877883
878884 if ( string . IsNullOrEmpty ( objPath ) )
@@ -889,12 +895,13 @@ private static bool CheckAccessLevelWithError(out string error, string currentRo
889895 return false ;
890896 }
891897
892- private static bool CheckAccessLevelApi ( string currentRole , string operation , AccessRights neededRights , out string error , out string ? getPolicy )
898+ private static bool CheckAccessLevelApi ( string currentRole , string operation , AccessRights neededRights , out string error ,
899+ out string ? getPolicy , List < Claim > ? tokenClaims = null )
893900 {
894901 error = string . Empty ;
895902 getPolicy = string . Empty ;
896903
897- var rules = GetAccessRulesStatic ( currentRole , neededRights . ToString ( ) , operation ) ;
904+ var rules = GetAccessRulesStatic ( currentRole , neededRights . ToString ( ) , operation , tokenClaims ) ;
898905
899906 if ( rules != null && rules . Count != 0 )
900907 {
0 commit comments