@@ -14,53 +14,63 @@ namespace KK.AspNetCore.EasyAuthAuthentication
1414 using Newtonsoft . Json ;
1515 using Newtonsoft . Json . Linq ;
1616
17+ /// <summary>
18+ /// Enables the handler in an Easy Auth context.
19+ /// </summary>
1720 public class EasyAuthAuthenticationHandler : AuthenticationHandler < EasyAuthAuthenticationOptions >
1821 {
22+ /// <summary>
23+ /// Initializes a new instance of the <see cref="EasyAuthAuthenticationHandler"/> class.
24+ /// </summary>
25+ /// <param name="options">An instance of <see cref="EasyAuthAuthenticationOptions"/>.</param>
26+ /// <param name="logger">An instance of <see cref="ILoggerFactory"/>.</param>
27+ /// <param name="encoder">An instance of <see cref="UrlEncoder"/>.</param>
28+ /// <param name="clock">An instance of <see cref="ISystemClock"/>.</param>
1929 public EasyAuthAuthenticationHandler (
2030 IOptionsMonitor < EasyAuthAuthenticationOptions > options ,
2131 ILoggerFactory logger ,
2232 UrlEncoder encoder ,
23- ISystemClock clock
24- ) : base ( options , logger , encoder , clock )
33+ ISystemClock clock ) : base ( options , logger , encoder , clock )
2534 {
26-
2735 }
2836
37+ /// <inheritdoc/>
2938 protected override async Task < AuthenticateResult > HandleAuthenticateAsync ( )
3039 {
31- Logger . LogInformation ( "starting authentication handler for app service authentication" ) ;
40+ this . Logger . LogInformation ( "starting authentication handler for app service authentication" ) ;
3241
3342 if (
3443 ( this . Context . User == null ||
3544 this . Context . User . Identity == null ||
36- this . Context . User . Identity . IsAuthenticated == false ) && this . Context . Request . Path != "/" + $ "{ Options . AuthEndpoint } ")
45+ this . Context . User . Identity . IsAuthenticated == false )
46+ && this . Context . Request . Path != "/" + $ "{ this . Options . AuthEndpoint } ")
3747 {
3848 var cookieContainer = new CookieContainer ( ) ;
39- HttpClientHandler handler = createHandler ( ref cookieContainer ) ;
40- HttpRequestMessage httpRequest = CreateAuthRequest ( ref cookieContainer ) ;
49+ var handler = this . CreateHandler ( ref cookieContainer ) ;
50+ var httpRequest = this . CreateAuthRequest ( ref cookieContainer ) ;
4151
4252 JArray payload = null ;
4353 try
4454 {
45- payload = await getAuthMe ( handler , httpRequest ) ;
55+ payload = await this . GetAuthMe ( handler , httpRequest ) ;
4656 }
4757 catch ( Exception ex )
4858 {
4959 return AuthenticateResult . Fail ( ex . Message ) ;
5060 }
5161
5262 // build up identity from json...
53- AuthenticationTicket ticket = BuildIdentityFromJsonPayload ( ( JObject ) payload [ 0 ] ) ;
63+ var ticket = this . BuildIdentityFromJsonPayload ( ( JObject ) payload [ 0 ] ) ;
5464
55- Logger . LogInformation ( "Set identity to user context object." ) ;
65+ this . Logger . LogInformation ( "Set identity to user context object." ) ;
5666 this . Context . User = ticket . Principal ;
5767
58- Logger . LogInformation ( "identity build was a success, returning ticket" ) ;
68+ this . Logger . LogInformation ( "identity build was a success, returning ticket" ) ;
5969 return AuthenticateResult . Success ( ticket ) ;
6070 }
6171 else
6272 {
63- Logger . LogInformation ( "identity already set, skipping middleware" ) ;
73+ this . Logger . LogInformation ( "identity already set, skipping middleware" ) ;
6474 return AuthenticateResult . NoResult ( ) ;
6575 }
6676 }
@@ -71,53 +81,52 @@ private AuthenticationTicket BuildIdentityFromJsonPayload(JObject payload)
7181 var idToken = payload [ "id_token" ] . Value < string > ( ) ;
7282 var providerName = payload [ "provider_name" ] . Value < string > ( ) ;
7383
74- Logger . LogDebug ( "payload was fetched from endpoint. id: {0}" , id ) ;
84+ this . Logger . LogDebug ( "payload was fetched from endpoint. id: {0}" , id ) ;
7585
7686 var identity = new GenericIdentity ( id ) ;
7787
78- Logger . LogInformation ( "building claims from payload..." ) ;
88+ this . Logger . LogInformation ( "building claims from payload..." ) ;
7989
80- List < Claim > claims = new List < Claim > ( ) ;
90+ var claims = new List < Claim > ( ) ;
8191 foreach ( var claim in payload [ "user_claims" ] )
8292 {
8393 claims . Add ( new Claim ( claim [ "typ" ] . ToString ( ) , claim [ "val" ] . ToString ( ) ) ) ;
8494 }
8595
86- Logger . LogInformation ( "Add claims to new identity" ) ;
96+ this . Logger . LogInformation ( "Add claims to new identity" ) ;
8797
8898 identity . AddClaims ( claims ) ;
8999 identity . AddClaim ( new Claim ( "id_token" , idToken ) ) ;
90100 identity . AddClaim ( new Claim ( "provider_name" , providerName ) ) ;
91101 var p = new GenericPrincipal ( identity , null ) ;
92102 return new AuthenticationTicket (
93103 p ,
94- EasyAuthAuthenticationDefaults . AuthenticationScheme
95- ) ;
104+ EasyAuthAuthenticationDefaults . AuthenticationScheme ) ;
96105 }
97106
98107 private HttpRequestMessage CreateAuthRequest ( ref CookieContainer cookieContainer )
99108 {
100- Logger . LogInformation ( $ "identity not found, attempting to fetch from auth endpoint '/{ Options . AuthEndpoint } '") ;
109+ this . Logger . LogInformation ( $ "identity not found, attempting to fetch from auth endpoint '/{ this . Options . AuthEndpoint } '") ;
101110
102- var uriString = $ "{ Context . Request . Scheme } ://{ Context . Request . Host } ";
111+ var uriString = $ "{ this . Context . Request . Scheme } ://{ this . Context . Request . Host } ";
103112
104- Logger . LogDebug ( "host uri: {0}" , uriString ) ;
113+ this . Logger . LogDebug ( "host uri: {0}" , uriString ) ;
105114
106- foreach ( var c in Context . Request . Cookies )
115+ foreach ( var c in this . Context . Request . Cookies )
107116 {
108117 cookieContainer . Add ( new Uri ( uriString ) , new Cookie ( c . Key , c . Value ) ) ;
109118 }
110119
111- Logger . LogDebug ( "found {0} cookies in request" , cookieContainer . Count ) ;
120+ this . Logger . LogDebug ( "found {0} cookies in request" , cookieContainer . Count ) ;
112121
113- foreach ( var cookie in Context . Request . Cookies )
122+ foreach ( var cookie in this . Context . Request . Cookies )
114123 {
115- Logger . LogDebug ( cookie . Key ) ;
124+ this . Logger . LogDebug ( cookie . Key ) ;
116125 }
117126
118127 // fetch value from endpoint
119- var request = new HttpRequestMessage ( HttpMethod . Get , $ "{ uriString } /{ Options . AuthEndpoint } ") ;
120- foreach ( var header in Context . Request . Headers )
128+ var request = new HttpRequestMessage ( HttpMethod . Get , $ "{ uriString } /{ this . Options . AuthEndpoint } ") ;
129+ foreach ( var header in this . Context . Request . Headers )
121130 {
122131 if ( header . Key . StartsWith ( "X-ZUMO-" ) )
123132 {
@@ -128,7 +137,7 @@ private HttpRequestMessage CreateAuthRequest(ref CookieContainer cookieContainer
128137 return request ;
129138 }
130139
131- private static HttpClientHandler createHandler ( ref CookieContainer container )
140+ private HttpClientHandler CreateHandler ( ref CookieContainer container )
132141 {
133142 var handler = new HttpClientHandler ( )
134143 {
@@ -137,15 +146,15 @@ private static HttpClientHandler createHandler(ref CookieContainer container)
137146 return handler ;
138147 }
139148
140- private async Task < JArray > getAuthMe ( HttpClientHandler handler , HttpRequestMessage httpRequest )
149+ private async Task < JArray > GetAuthMe ( HttpClientHandler handler , HttpRequestMessage httpRequest )
141150 {
142151 JArray payload = null ;
143- using ( HttpClient client = new HttpClient ( handler ) )
152+ using ( var client = new HttpClient ( handler ) )
144153 {
145154 var response = await client . SendAsync ( httpRequest ) ;
146155 if ( ! response . IsSuccessStatusCode )
147156 {
148- Logger . LogDebug ( "auth endpoint was not sucessful. Status code: {0}, reason {1}" , response . StatusCode , response . ReasonPhrase ) ;
157+ this . Logger . LogDebug ( "auth endpoint was not sucessful. Status code: {0}, reason {1}" , response . StatusCode , response . ReasonPhrase ) ;
149158 throw new WebException ( "Unable to fetch user information from auth endpoint." ) ;
150159 }
151160
@@ -163,4 +172,4 @@ private async Task<JArray> getAuthMe(HttpClientHandler handler, HttpRequestMessa
163172 return payload ;
164173 }
165174 }
166- }
175+ }
0 commit comments