Skip to content

Commit 9ece67f

Browse files
authored
Chore: Skip auth instead of fail (#12)
* Skip auth instead of Fail auth on missing slack headers * Update editorconfig (end-of-file, final newline)
1 parent 3cb319e commit 9ece67f

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

source/.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ tab_width = 4
1616

1717
# New line preferences
1818
end_of_line = crlf
19-
insert_final_newline = false
19+
insert_final_newline = true
2020

2121
#### .NET Coding Conventions ####
2222

source/src/Slackbot.Net.Endpoints/Authentication/SlackbotEventsAuthenticationAuthenticationHandler.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.AspNetCore.Http;
77
using Microsoft.Extensions.Logging;
88
using Microsoft.Extensions.Options;
9+
using Microsoft.Extensions.Primitives;
910

1011
namespace Slackbot.Net.Endpoints.Authentication;
1112

@@ -31,22 +32,28 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
3132

3233
string timestamp = headers[TimestampHeaderName].FirstOrDefault();
3334
string signature = headers[SignatureHeaderName].FirstOrDefault();
34-
35+
var failures = new StringBuilder();
3536
if (timestamp == null)
3637
{
37-
return HandleRequestResult.Fail($"Missing header {TimestampHeaderName}");
38+
failures.Append($"Missing header {TimestampHeaderName}");
3839
}
3940

4041
if (signature == null)
4142
{
42-
return HandleRequestResult.Fail($"Missing header {SignatureHeaderName}");
43+
failures.Append($"Missing header {TimestampHeaderName}");
44+
}
45+
46+
if (timestamp is null || signature == null)
47+
{
48+
Logger.LogDebug($"Skipping handler: {failures}");
49+
return HandleRequestResult.SkipHandler();
4350
}
4451

4552
bool isNumber = long.TryParse(timestamp, out long timestampAsLong);
4653

4754
if (!isNumber)
4855
{
49-
return HandleRequestResult.Fail($"Invalid header. Header {TimestampHeaderName} not a number");
56+
return HandleRequestResult.Fail($"Invalid formatted headers. {TimestampHeaderName} is not a number. ");
5057
}
5158

5259
Request.EnableBuffering();
@@ -59,7 +66,7 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
5966
return HandleRequestResult.Success(new AuthenticationTicket(new ClaimsPrincipal(), SlackbotEventsAuthenticationConstants.AuthenticationScheme));
6067
}
6168

62-
return HandleRequestResult.Fail("Verification of Slack request failed.");
69+
return HandleRequestResult.Fail("Slack request failed signature verification.");
6370

6471
}
6572

source/src/Slackbot.Net.Endpoints/Middlewares/SlackbotEventAuthMiddleware.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,23 @@ public SlackbotEventAuthMiddleware(RequestDelegate next)
1616

1717
public async Task Invoke(HttpContext ctx, ILogger<SlackbotEventAuthMiddleware> logger)
1818
{
19-
bool success = false;
19+
AuthenticateResult res;
2020
try
2121
{
22-
var res = await ctx.AuthenticateAsync(SlackbotEventsAuthenticationConstants.AuthenticationScheme);
23-
success = res.Succeeded;
22+
res = await ctx.AuthenticateAsync(SlackbotEventsAuthenticationConstants.AuthenticationScheme);
2423
}
2524
catch (InvalidOperationException ioe)
2625
{
2726
throw new InvalidOperationException("Did you forget to call services.AddAuthentication().AddSlackbotEvents()?", ioe);
2827
}
2928

30-
if (success)
29+
if (res.Succeeded)
3130
{
3231
await _next(ctx);
3332
}
3433
else
3534
{
35+
logger.LogWarning($"Unauthorized callback from Slack");
3636
ctx.Response.StatusCode = StatusCodes.Status401Unauthorized;
3737
await ctx.Response.WriteAsync("UNAUTHORIZED");
3838
}

0 commit comments

Comments
 (0)