-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #292 from alborrajo/feature/chatrpc
Improvements to Chat RPC commands
- Loading branch information
Showing
12 changed files
with
190 additions
and
13 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
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
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,105 @@ | ||
#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 AuthMiddleware : IWebMiddleware | ||
{ | ||
private static readonly ILogger Logger = LogProvider.Logger<Logger>(typeof(AuthMiddleware)); | ||
|
||
private readonly IDatabase _database; | ||
private readonly Dictionary<string, AccountStateType> _routeAndRequiredMinimumState; | ||
|
||
public AuthMiddleware(IDatabase database) | ||
{ | ||
_database = database; | ||
_routeAndRequiredMinimumState = new Dictionary<string, AccountStateType>(); | ||
} | ||
|
||
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) | ||
{ | ||
Logger.Error("Attempted to access auth protected route with no Authorization header"); | ||
WebResponse response = new WebResponse(); | ||
response.StatusCode = 401; | ||
await response.WriteAsync("Attempted to access auth protected route with no Authorization header"); | ||
return response; | ||
} | ||
|
||
if(!authHeader.StartsWith("Basic ")) | ||
{ | ||
Logger.Error("Attempted to access auth protected route with an invalid Authorization method. Only Basic auth is supported."); | ||
WebResponse response = new WebResponse(); | ||
response.StatusCode = 401; | ||
await response.WriteAsync("Attempted to access auth protected route with an invalid Authorization method. Only Basic auth is supported."); | ||
return response; | ||
} | ||
|
||
string encodedUserAndPassword = authHeader.Substring("Basic ".Length); | ||
Encoding encoding = Encoding.GetEncoding("iso-8859-1"); | ||
string[] usernameAndPassword = encoding.GetString(Convert.FromBase64String(encodedUserAndPassword)).Split(":"); | ||
if(usernameAndPassword.Length != 2) | ||
{ | ||
Logger.Error("Attempted to access auth protected route with an invalid Basic auth header."); | ||
WebResponse response = new WebResponse(); | ||
response.StatusCode = 401; | ||
await response.WriteAsync("Attempted to access auth protected route with an invalid Basic auth header."); | ||
return response; | ||
} | ||
|
||
string username = usernameAndPassword[0]; | ||
string password = usernameAndPassword[1]; | ||
|
||
Account account = _database.SelectAccountByName(username); | ||
if (account == null) | ||
{ | ||
Logger.Error($"Attempted to authenticate as a nonexistant user {username}."); | ||
WebResponse response = new WebResponse(); | ||
response.StatusCode = 401; | ||
await response.WriteAsync($"Failed to authenticate as {username}."); | ||
return response; | ||
} | ||
|
||
if (!PasswordHash.Verify(password, account.Hash)) | ||
{ | ||
Logger.Error($"Attempted to authenticate as {username} with an incorrect password."); | ||
WebResponse response = new WebResponse(); | ||
response.StatusCode = 401; | ||
await response.WriteAsync($"Failed to authenticate as {username}."); | ||
return response; | ||
} | ||
|
||
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 {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 await next(request); | ||
} | ||
} |
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
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
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
22 changes: 22 additions & 0 deletions
22
Arrowgene.Ddon.Test/GameServer/Chat/Log/ChatMessageLogEntryTest.cs
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,22 @@ | ||
using System.Text.Json; | ||
using Arrowgene.Ddon.GameServer.Chat; | ||
using Arrowgene.Ddon.GameServer.Chat.Log; | ||
using Arrowgene.Ddon.Shared.Model; | ||
using Xunit; | ||
|
||
namespace Arrowgene.Ddon.Test.GameServer.Chat.Log; | ||
|
||
public class ChatMessageLogEntryTest | ||
{ | ||
[Fact] | ||
public void TestJsonSerialize() | ||
{ | ||
ChatMessageLogEntry obj = new ChatMessageLogEntry(); | ||
obj.ChatMessage = new ChatMessage(); | ||
obj.ChatMessage.Type = LobbyChatMsgType.Party; | ||
string json = JsonSerializer.Serialize(obj); | ||
ChatMessageLogEntry res = JsonSerializer.Deserialize<ChatMessageLogEntry>(json); | ||
Assert.NotNull(res); | ||
Assert.Equal(obj.ChatMessage.Type, res.ChatMessage.Type); | ||
} | ||
} |
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