-
-
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.
feat: basic auth for all api endpoints (#22)
- Loading branch information
Showing
5 changed files
with
154 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace v_rising_discord_bot_companion; | ||
|
||
public readonly record struct PluginConfig( | ||
List<BasicAuthUser> BasicAuthUsers | ||
); | ||
|
||
public readonly record struct BasicAuthUser( | ||
string Username, | ||
string Password | ||
); |
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,64 @@ | ||
using System.Linq; | ||
using HarmonyLib; | ||
using Il2CppSystem.Net; | ||
using Il2CppSystem.Security.Principal; | ||
using ProjectM.Network; | ||
|
||
namespace v_rising_discord_bot_companion.command; | ||
|
||
[HarmonyPatch(typeof(HttpServiceReceiveThread))] | ||
public class HttpReceiveServicePatches { | ||
|
||
[HarmonyPrefix] | ||
[HarmonyPatch("IsAllowed")] | ||
public static bool IsAllowed(HttpListenerContext context, ref bool __result) { | ||
|
||
var pluginConfig = Plugin.Instance.GetPluginConfig(); | ||
|
||
if (pluginConfig.BasicAuthUsers.Count <= 0) { | ||
return true; | ||
} | ||
|
||
var currentBasicAuthUser = ParseBasicAuthUser(context); | ||
if (!currentBasicAuthUser.HasValue) { | ||
__result = false; | ||
return false; | ||
} | ||
|
||
__result = IsAuthorized((BasicAuthUser) currentBasicAuthUser); | ||
return __result; | ||
} | ||
|
||
private static BasicAuthUser? ParseBasicAuthUser(HttpListenerContext context) { | ||
|
||
context.ParseAuthentication(AuthenticationSchemes.Basic); | ||
|
||
if (context.user == null) { | ||
return null; | ||
} | ||
|
||
var principal = context.user.TryCast<GenericPrincipal>(); | ||
var identity = principal?.m_identity.TryCast<HttpListenerBasicIdentity>(); | ||
|
||
if (identity == null) { | ||
return null; | ||
} | ||
|
||
var username = identity.Name; | ||
var password = identity.password; | ||
|
||
if (username == null || password == null) { | ||
return null; | ||
} | ||
|
||
return new BasicAuthUser( | ||
Username: username, | ||
Password: password | ||
); | ||
} | ||
|
||
private static bool IsAuthorized(BasicAuthUser currentBasicAuthUser) { | ||
return Plugin.Instance.GetPluginConfig().BasicAuthUsers | ||
.Count(it => it.Username.Equals(currentBasicAuthUser.Username) && it.Password.Equals(currentBasicAuthUser.Password)) == 1; | ||
} | ||
} |
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