11namespace KK . AspNetCore . EasyAuthAuthentication
22{
33 using System ;
4- using System . Collections . Generic ;
5- using System . IdentityModel . Tokens . Jwt ;
6- using System . Linq ; // required by Children<JObject>.FirstOrDefault requires using System.Linq;
7- using System . Net ;
8- using System . Net . Http ;
94 using System . Security . Claims ;
10- using System . Security . Principal ;
11- using System . Text ;
125 using System . Text . Encodings . Web ;
136 using System . Threading . Tasks ;
147 using KK . AspNetCore . EasyAuthAuthentication . Services ;
158 using Microsoft . AspNetCore . Authentication ;
169 using Microsoft . AspNetCore . Http ;
1710 using Microsoft . Extensions . Logging ;
1811 using Microsoft . Extensions . Options ;
19- using Newtonsoft . Json ;
20- using Newtonsoft . Json . Linq ;
2112
2213 /// <summary>
2314 /// Enables the handler in an Easy Auth context.
2415 /// </summary>
2516 public class EasyAuthAuthenticationHandler : AuthenticationHandler < EasyAuthAuthenticationOptions >
2617 {
18+ private static readonly Func < ClaimsPrincipal , bool > IsContextUserNotAuthenticated =
19+ user => user == null || user . Identity == null || user . Identity . IsAuthenticated == false ;
20+
21+ private static readonly Func < IHeaderDictionary , string , bool > IsHeaderSet =
22+ ( headers , headerName ) => ! string . IsNullOrEmpty ( headers [ headerName ] . ToString ( ) ) ;
23+
24+ private static readonly Func < IHeaderDictionary , ClaimsPrincipal , HttpRequest , string , bool > CanUseEasyAuthJson =
25+ ( headers , user , request , authEndpoint ) =>
26+ IsContextUserNotAuthenticated ( user )
27+ && ! IsHeaderSet ( headers , AuthTokenHeaderNames . AADIdToken )
28+ && request . Path != "/" + $ "{ authEndpoint } ";
29+
30+ private readonly Func < IHeaderDictionary , ClaimsPrincipal , bool > canUseHeaderAuth =
31+ ( headers , user ) => IsContextUserNotAuthenticated ( user ) &&
32+ IsHeaderSet ( headers , AuthTokenHeaderNames . AADIdToken ) ;
33+
2734 /// <summary>
2835 /// Initializes a new instance of the <see cref="EasyAuthAuthenticationHandler"/> class.
2936 /// </summary>
@@ -39,42 +46,30 @@ public EasyAuthAuthenticationHandler(
3946 {
4047 }
4148
42- private static Func < ClaimsPrincipal , bool > isContextUserNotAuthenticated =
43- user => ( user == null || user . Identity == null || user . Identity . IsAuthenticated == false ) ;
44- private static Func < IHeaderDictionary , string , bool > isHeaderSet =
45- ( headers , headerName ) => ! string . IsNullOrEmpty ( headers [ headerName ] . ToString ( ) ) ;
46- private Func < IHeaderDictionary , ClaimsPrincipal , bool > canUseHeaderAuth =
47- ( headers , user ) => isContextUserNotAuthenticated ( user ) &&
48- isHeaderSet ( headers , AuthTokenHeaderNames . AADIdToken ) ;
49- private static Func < IHeaderDictionary , ClaimsPrincipal , HttpRequest , string , bool > canUseEasyAuthJson =
50- ( headers , user , request , authEndpoint ) =>
51- isContextUserNotAuthenticated ( user )
52- && ! isHeaderSet ( headers , AuthTokenHeaderNames . AADIdToken )
53- && request . Path != "/" + $ "{ authEndpoint } ";
54-
5549 /// <inheritdoc/>
5650 protected override async Task < AuthenticateResult > HandleAuthenticateAsync ( )
5751 {
5852 this . Logger . LogInformation ( "starting authentication handler for app service authentication" ) ;
5953
60- if ( canUseHeaderAuth ( this . Context . Request . Headers , this . Context . User ) )
54+ if ( this . canUseHeaderAuth ( this . Context . Request . Headers , this . Context . User ) )
6155 {
6256 return EasyAuthWithHeaderService . AuthUser ( this . Logger , this . Context ) ;
6357 }
64- else if ( canUseEasyAuthJson ( this . Context . Request . Headers , this . Context . User , this . Context . Request , this . Options . AuthEndpoint ) )
58+ else if ( CanUseEasyAuthJson ( this . Context . Request . Headers , this . Context . User , this . Context . Request , this . Options . AuthEndpoint ) )
6559 {
6660 return await EasyAuthWithAuthMeService . AuthUser ( this . Logger , this . Context , this . Options . AuthEndpoint ) ;
6761 }
6862 else
6963 {
70- if ( isContextUserNotAuthenticated ( this . Context . User ) )
64+ if ( IsContextUserNotAuthenticated ( this . Context . User ) )
7165 {
7266 this . Logger . LogInformation ( "The identity isn't set by easy auth." ) ;
7367 }
7468 else
7569 {
7670 this . Logger . LogInformation ( "identity already set, skipping middleware" ) ;
7771 }
72+
7873 return AuthenticateResult . NoResult ( ) ;
7974 }
8075 }
0 commit comments