Skip to content

Commit

Permalink
Use middleware for authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
alborrajo committed May 10, 2024
1 parent dd88bd2 commit 46b9073
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 79 deletions.
9 changes: 0 additions & 9 deletions Arrowgene.Ddon.Rpc.Web/Interceptor/IInterceptor.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,29 +1,42 @@
#nullable enable

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Arrowgene.Ddon.Database;
using Arrowgene.Ddon.Database.Model;
using Arrowgene.Ddon.Shared.Crypto;
using Arrowgene.Logging;
using Arrowgene.WebServer;
using Arrowgene.WebServer.Middleware;

public class AuthInterceptor : IInterceptor
public class AuthMiddleware : IWebMiddleware
{
private static readonly ILogger Logger = LogProvider.Logger<Logger>(typeof(AuthInterceptor));
private static readonly ILogger Logger = LogProvider.Logger<Logger>(typeof(AuthMiddleware));

private readonly IDatabase _database;
private readonly AccountStateType _minimumState;
private readonly Dictionary<string, AccountStateType> _routeAndRequiredMinimumState;

public AuthInterceptor(IDatabase database, AccountStateType minimumState)
public AuthMiddleware(IDatabase database)
{
_database = database;
_minimumState = minimumState;
_routeAndRequiredMinimumState = new Dictionary<string, AccountStateType>();
}

public async Task<WebResponse?> InterceptRequest(WebRequest request)
public void Require(AccountStateType minimumState, string route)
{
_routeAndRequiredMinimumState.Add(route, minimumState);
}

public async Task<WebResponse> Handle(WebRequest request, WebMiddlewareDelegate next)
{
if(!_routeAndRequiredMinimumState.ContainsKey(request.Path))
{
// Don't intercept request if the request path isn't registered in the middleware
return await next(request);
}

string authHeader = request.Header.Get("authorization");
if(authHeader == null)
{
Expand Down Expand Up @@ -77,15 +90,16 @@ public AuthInterceptor(IDatabase database, AccountStateType minimumState)
return response;
}

if(account.State < _minimumState)
AccountStateType minimumRequiredAccountStateType = _routeAndRequiredMinimumState[request.Path];
if(account.State < minimumRequiredAccountStateType)
{
Logger.Error($"Attempted to access auth protected route as {username} without enough permissions (Account has {account.State}, minimum required {_minimumState}).");
Logger.Error($"Attempted to access auth protected route as {username} without enough permissions (Account has {account.State}, minimum required {minimumRequiredAccountStateType}).");
WebResponse response = new WebResponse();
response.StatusCode = 403;
await response.WriteAsync($"Attempted to access auth protected route as {username} without enough permissions.");
return response;
}

return null;
return await next(request);
}
}
59 changes: 0 additions & 59 deletions Arrowgene.Ddon.Rpc.Web/Route/InterceptedRpcWebRoute.cs

This file was deleted.

8 changes: 7 additions & 1 deletion Arrowgene.Ddon.Rpc.Web/RpcWebServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ public void Init()
{
_webServer.AddRoute(new SpawnRoute(this));
_webServer.AddRoute(new InfoRoute(this));
_webServer.AddRoute(new InterceptedRpcWebRoute(this, new ChatRoute(this), new AuthInterceptor(_gameServer.Database, AccountStateType.GameMaster)));

ChatRoute chatRoute = new ChatRoute(this);
_webServer.AddRoute(chatRoute);

AuthMiddleware authMiddleware = new AuthMiddleware(_gameServer.Database);
authMiddleware.Require(AccountStateType.GameMaster, chatRoute.Route);
_webServer.AddMiddleware(authMiddleware);
}
}
}
12 changes: 11 additions & 1 deletion Arrowgene.Ddon.WebServer/DdonWebServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Arrowgene.Ddon.Database;
using Arrowgene.Logging;
using Arrowgene.WebServer;
using Arrowgene.WebServer.Middleware;
using Arrowgene.WebServer.Route;
using Arrowgene.WebServer.Server;
using Arrowgene.WebServer.Server.Kestrel;
Expand Down Expand Up @@ -33,12 +34,21 @@ public DdonWebServer(WebServerSetting setting, IDatabase database)
Logger.Info(servingFile);
}

_webService.AddMiddleware(staticFile);
AddMiddleware(staticFile);

AddRoute(new IndexRoute());
AddRoute(new AccountRoute(database));
}

public void AddMiddleware(IWebMiddleware middleware)
{
_webService.AddMiddleware(middleware);
if (_running)
{
Logger.Info($"Registered new middleware `{middleware.GetType().Name}`");
}
}

public void AddRoute(IWebRoute route)
{
_webService.AddRoute(route);
Expand Down

0 comments on commit 46b9073

Please sign in to comment.