Skip to content

Commit

Permalink
Merge pull request #4 from hermesdj/dev
Browse files Browse the repository at this point in the history
Package improvements
  • Loading branch information
hermesdj authored Jun 14, 2023
2 parents 339bada + 2c63475 commit e9c36c7
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 42 deletions.
42 changes: 4 additions & 38 deletions ApiPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#nullable enable
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using HarmonyLib;
using Il2CppInterop.Runtime.Injection;
using UnityEngine;
using VRisingServerApiPlugin.command;
using VRisingServerApiPlugin.http.security;
using VRisingServerApiPlugin.query;

namespace VRisingServerApiPlugin;
Expand All @@ -25,15 +23,15 @@ public class ApiPlugin : BasePlugin
public static ApiPlugin Instance { get; private set; }
#nullable enable

private ConfigEntry<string> _authorizedUsers;

public ApiPlugin() : base()
{
Instance = this;
Logger = Log;

_authorizedUsers = Config.Bind("Authentication", "AuthorizedUsers", "",
var authorizedUsers = Config.Bind("Authentication", "AuthorizedUsers", "",
"A list of comma separated username:password entries that defines the accounts allowed to query the API");

HttpSecuritySingleton.GetInstance().Initialize(authorizedUsers);
}

public override void Load()
Expand Down Expand Up @@ -65,36 +63,4 @@ public override bool Unload()

return true;
}

public List<AuthorizedUser> GetAuthorizedUserList()
{
return AuthorizedUser.ParseConfig(this._authorizedUsers.Value);
}

public bool CheckAuthenticationOfUser(string username, string password)
{
return GetAuthorizedUserList()
.Count(user => user.Username.Equals(username) && user.Password.Equals(password)) == 1;
}

public class AuthorizedUser
{
public string Username { get; set; }
public string Password { get; set; }

private AuthorizedUser(string username, string password)
{
Username = username;
Password = password;
}

public static List<AuthorizedUser> ParseConfig(string authorizedUsers)
{
return (from user in authorizedUsers.Split(",")
select user.Split(':')
into parts
where parts.Length == 2
select new AuthorizedUser(parts[0].Trim(), parts[1].Trim())).ToList();
}
}
}
3 changes: 0 additions & 3 deletions VRisingServerApiPlugin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
</ItemGroup>

<ItemGroup>
<Content Include=".github\release.yaml" />
<Content Include=".github\workflows\build.yaml" />
<Content Include=".github\workflows\release.yaml" />
<None Include="README.md" Pack="true" PackagePath="\" />
</ItemGroup>

Expand Down
3 changes: 2 additions & 1 deletion http/HttpRequestParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Il2CppSystem.Net;
using Il2CppSystem.Security.Principal;
using VRisingServerApiPlugin.command;
using VRisingServerApiPlugin.http.security;

namespace VRisingServerApiPlugin.http;

Expand Down Expand Up @@ -64,7 +65,7 @@ public static HttpRequest ParseHttpRequest(HttpListenerContext context, Command
var username = identity.Name;
var password = identity.password;

var isAuthorized = ApiPlugin.Instance.CheckAuthenticationOfUser(username, password);
var isAuthorized = HttpSecuritySingleton.GetInstance().CheckAuthenticationOfUser(username, password);

return new AuthenticatedUser(Username: identity.Name, Password: identity.password,
IsAuthorized: isAuthorized);
Expand Down
25 changes: 25 additions & 0 deletions http/security/AuthorizedUser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Collections.Generic;
using System.Linq;

namespace VRisingServerApiPlugin.http.security;

public class AuthorizedUser
{
public string Username { get; set; }
public string Password { get; set; }

private AuthorizedUser(string username, string password)
{
Username = username;
Password = password;
}

public static IEnumerable<AuthorizedUser> ParseConfig(string authorizedUsers)
{
return (from user in authorizedUsers.Split(",")
select user.Split(':')
into parts
where parts.Length == 2
select new AuthorizedUser(parts[0].Trim(), parts[1].Trim())).ToList();
}
}
40 changes: 40 additions & 0 deletions http/security/HttpSecuritySingleton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Collections.Generic;
using System.Linq;
using BepInEx.Configuration;

namespace VRisingServerApiPlugin.http.security;

public class HttpSecuritySingleton
{
private static HttpSecuritySingleton _instance;

private IEnumerable<AuthorizedUser> _authorizedUsers;

public static HttpSecuritySingleton GetInstance()
{
return _instance ??= new HttpSecuritySingleton();
}

public void Initialize(ConfigEntry<string> authorizedUsers)
{
authorizedUsers.SettingChanged += (_, args) =>
{
var changedArgs = (SettingChangedEventArgs)args;
var changedSettings = (ConfigEntry<string>)changedArgs.ChangedSetting;

_authorizedUsers = GetAuthorizedUserList(changedSettings.Value);
};
_authorizedUsers = GetAuthorizedUserList(authorizedUsers.Value);
}

private static IEnumerable<AuthorizedUser> GetAuthorizedUserList(string authorizedUsers)
{
return AuthorizedUser.ParseConfig(authorizedUsers);
}

public bool CheckAuthenticationOfUser(string username, string password)
{
return _authorizedUsers
.Count(user => user.Username.Equals(username) && user.Password.Equals(password)) == 1;
}
}

0 comments on commit e9c36c7

Please sign in to comment.