Skip to content

Commit ba468a4

Browse files
committed
Make HandleAuthenticateAsync always return a proper AuthenticateResult
1 parent 50d9cde commit ba468a4

1 file changed

Lines changed: 27 additions & 20 deletions

File tree

src/CZ.AspNetCore.EasyAuthAuthentication/EasyAuthAuthenticationHandler.cs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -79,32 +79,39 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
7979

8080
var authService = this.authenticationServices.FirstOrDefault(d => d.CanHandleAuthentification(this.Context));
8181
var enabledProviders = this.Options.ProviderOptions.Where(d => d.Enabled == true);
82-
if (authService != null && enabledProviders.Any(d => d.ProviderName == authService.GetType().Name))
82+
try
8383
{
84-
this.Logger.LogInformation($"use the {authService.GetType().Name} as auth handler.");
85-
return authService.AuthUser(this.Context, this.Options.ProviderOptions.FirstOrDefault(d => d.ProviderName == authService.GetType().Name));
86-
}
87-
else if (CanUseEasyAuthJson(this.Context.Request.Headers, this.Context.User, this.Context.Request, this.Options))
88-
{
89-
var service = new LocalAuthMeService(this.Logger,
90-
this.Context.Request.Scheme,
91-
this.Context.Request.Host.ToString(),
92-
this.Context.Request.Cookies,
93-
this.Context.Request.Headers);
94-
return await service.AuthUser(this.Context, this.Options.LocalProviderOption);
95-
}
96-
else
97-
{
98-
if (IsContextUserNotAuthenticated(this.Context.User))
84+
if (authService != null && enabledProviders.Any(d => d.ProviderName == authService.GetType().Name))
9985
{
100-
this.Logger.LogInformation("The identity isn't set by easy auth.");
86+
this.Logger.LogInformation($"use the {authService.GetType().Name} as auth handler.");
87+
return authService.AuthUser(this.Context, this.Options.ProviderOptions.FirstOrDefault(d => d.ProviderName == authService.GetType().Name));
10188
}
102-
else
89+
else if (CanUseEasyAuthJson(this.Context.Request.Headers, this.Context.User, this.Context.Request, this.Options))
10390
{
104-
this.Logger.LogInformation("identity already set, skipping middleware");
91+
var service = new LocalAuthMeService(this.Logger,
92+
this.Context.Request.Scheme,
93+
this.Context.Request.Host.ToString(),
94+
this.Context.Request.Cookies,
95+
this.Context.Request.Headers);
96+
return await service.AuthUser(this.Context, this.Options.LocalProviderOption);
10597
}
98+
else
99+
{
100+
if (IsContextUserNotAuthenticated(this.Context.User))
101+
{
102+
this.Logger.LogInformation("The identity isn't set by easy auth.");
103+
}
104+
else
105+
{
106+
this.Logger.LogInformation("identity already set, skipping middleware");
107+
}
106108

107-
return AuthenticateResult.NoResult();
109+
return AuthenticateResult.NoResult();
110+
}
111+
}
112+
catch (Exception ex)
113+
{
114+
return AuthenticateResult.Fail(ex);
108115
}
109116
}
110117
}

0 commit comments

Comments
 (0)