-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SendBotPlugins websocket event added TokenFilter to only allow
- Loading branch information
Showing
8 changed files
with
130 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace MicrobotApi; | ||
|
||
public class ConnectionManager | ||
{ | ||
public readonly List<string> Connections = []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace MicrobotApi.Models; | ||
|
||
public interface IHubRequestModel | ||
{ | ||
public string Group { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace MicrobotApi.Models; | ||
|
||
public class PluginRequestModel | ||
{ | ||
public string Name { get; set; } | ||
public bool Active { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace MicrobotApi.Models; | ||
|
||
public class SendBotPluginRequestModel : IHubRequestModel | ||
{ | ||
public string Group { get; set; } | ||
public List<PluginRequestModel> Plugins { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using Microsoft.AspNetCore.SignalR; | ||
|
||
namespace MicrobotApi; | ||
|
||
public class TokenFilter : IHubFilter | ||
{ | ||
private readonly ConnectionManager _connectionManager; | ||
private const string invalidTokenMessage = "Invalid token!"; | ||
|
||
public TokenFilter(ConnectionManager connectionManager) | ||
{ | ||
_connectionManager = connectionManager; | ||
} | ||
public async ValueTask<object> InvokeMethodAsync( | ||
HubInvocationContext invocationContext, Func<HubInvocationContext, ValueTask<object>> next) | ||
{ | ||
|
||
var token = GetToken(invocationContext.Context); | ||
|
||
var exists = _connectionManager.Connections.Contains(token); | ||
if (!exists) | ||
throw new UnauthorizedAccessException(invalidTokenMessage); | ||
|
||
Console.WriteLine($"Calling hub method '{invocationContext.HubMethodName}'"); | ||
try | ||
{ | ||
return await next(invocationContext); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"Exception calling '{invocationContext.HubMethodName}': {ex}"); | ||
throw; | ||
} | ||
} | ||
|
||
// Optional method | ||
public Task OnConnectedAsync(HubLifetimeContext context, Func<HubLifetimeContext, Task> next) | ||
{ | ||
return next(context); | ||
} | ||
|
||
// Optional method | ||
public Task OnDisconnectedAsync( | ||
HubLifetimeContext context, Exception exception, Func<HubLifetimeContext, Exception, Task> next) | ||
{ | ||
return next(context, exception); | ||
} | ||
|
||
private string? GetToken(HubCallerContext context) | ||
{ | ||
var httpContext = context.GetHttpContext(); | ||
|
||
var validHttpRequest = httpContext is { Request.Headers: not null } && httpContext.Request.Headers.Any(); | ||
|
||
if (!validHttpRequest | ||
|| (string.IsNullOrWhiteSpace(httpContext?.Request.Headers?["token"]) | ||
&& string.IsNullOrWhiteSpace(httpContext?.Request.Query["token"].FirstOrDefault()))) | ||
{ | ||
return null; | ||
} | ||
|
||
var token = httpContext.Request.Headers?["token"].FirstOrDefault() | ||
?? httpContext.Request.Query["token"].FirstOrDefault(); | ||
|
||
return token; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace MicrobotApi; | ||
|
||
public class TokenRequestModel | ||
{ | ||
public String Token { get; set; } | ||
} |