Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion source/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ tab_width = 4

# New line preferences
end_of_line = crlf
insert_final_newline = false
insert_final_newline = true

#### .NET Coding Conventions ####

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives;

namespace Slackbot.Net.Endpoints.Authentication;

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

string timestamp = headers[TimestampHeaderName].FirstOrDefault();
string signature = headers[SignatureHeaderName].FirstOrDefault();

var failures = new StringBuilder();
if (timestamp == null)
{
return HandleRequestResult.Fail($"Missing header {TimestampHeaderName}");
failures.Append($"Missing header {TimestampHeaderName}");
}

if (signature == null)
{
return HandleRequestResult.Fail($"Missing header {SignatureHeaderName}");
failures.Append($"Missing header {TimestampHeaderName}");
}

if (timestamp is null || signature == null)
{
Logger.LogDebug($"Skipping handler: {failures}");
return HandleRequestResult.SkipHandler();
}

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

if (!isNumber)
{
return HandleRequestResult.Fail($"Invalid header. Header {TimestampHeaderName} not a number");
return HandleRequestResult.Fail($"Invalid formatted headers. {TimestampHeaderName} is not a number. ");
}

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

return HandleRequestResult.Fail("Verification of Slack request failed.");
return HandleRequestResult.Fail("Slack request failed signature verification.");

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ public SlackbotEventAuthMiddleware(RequestDelegate next)

public async Task Invoke(HttpContext ctx, ILogger<SlackbotEventAuthMiddleware> logger)
{
bool success = false;
AuthenticateResult res;
try
{
var res = await ctx.AuthenticateAsync(SlackbotEventsAuthenticationConstants.AuthenticationScheme);
success = res.Succeeded;
res = await ctx.AuthenticateAsync(SlackbotEventsAuthenticationConstants.AuthenticationScheme);
}
catch (InvalidOperationException ioe)
{
throw new InvalidOperationException("Did you forget to call services.AddAuthentication().AddSlackbotEvents()?", ioe);
}

if (success)
if (res.Succeeded)
{
await _next(ctx);
}
else
{
logger.LogWarning($"Unauthorized callback from Slack");
ctx.Response.StatusCode = StatusCodes.Status401Unauthorized;
await ctx.Response.WriteAsync("UNAUTHORIZED");
}
Expand Down