Skip to content

Commit

Permalink
Rpc Basic Auth (#14)
Browse files Browse the repository at this point in the history
* Rpc Basic Auth

* change plugin name to RpcSecurity
  • Loading branch information
belane authored and 陈志同 committed Oct 13, 2020
1 parent 79d3d80 commit 15d1894
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 21 deletions.
18 changes: 0 additions & 18 deletions RpcDisabled/RpcDisabled.cs

This file was deleted.

46 changes: 46 additions & 0 deletions RpcSecurity/RpcSecurity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Microsoft.AspNetCore.Http;
using Neo.IO.Json;
using Neo.Network.RPC;
using System;
using System.Linq;
using System.Text;

namespace Neo.Plugins
{
public class RpcSecurity : Plugin, IRpcPlugin
{
public JObject OnProcess(HttpContext context, string method, JArray _params)
{
if (!CheckAuth(context) || Settings.Default.DisabledMethods.Contains(method))
throw new RpcException(-400, "Access denied");
return null;
}

private bool CheckAuth(HttpContext context)
{
if (string.IsNullOrEmpty(Settings.Default.RpcUser))
{
return true;
}

context.Response.Headers["WWW-Authenticate"] = "Basic realm=\"Restricted\"";

string reqauth = context.Request.Headers["Authorization"];
string authstring = null;
try
{
authstring = Encoding.UTF8.GetString(Convert.FromBase64String(reqauth.Replace("Basic ", "").Trim()));
}
catch
{
return false;
}

string[] authvalues = authstring.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
if (authvalues.Length < 2)
return false;

return authvalues[0] == Settings.Default.RpcUser && authvalues[1] == Settings.Default.RpcPass;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<None Update="RpcDisabled\config.json">
<None Update="RpcSecurity\config.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</None>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"PluginConfiguration": {
"RpcUser": "",
"RpcPass": "",
"DisabledMethods": []
}
}
4 changes: 4 additions & 0 deletions RpcDisabled/Settings.cs → RpcSecurity/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace Neo.Plugins
{
internal class Settings
{
public string RpcUser { get; }
public string RpcPass { get; }
public string[] DisabledMethods { get; }

public static Settings Default { get; }
Expand All @@ -17,6 +19,8 @@ static Settings()

public Settings(IConfigurationSection section)
{
this.RpcUser = section.GetSection("RpcUser").Value;
this.RpcPass = section.GetSection("RpcPass").Value;
this.DisabledMethods = section.GetSection("DisabledMethods").GetChildren().Select(p => p.Value).ToArray();
}
}
Expand Down
4 changes: 2 additions & 2 deletions neo-plugins.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimplePolicy", "SimplePolic
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ApplicationLogs", "ApplicationLogs\ApplicationLogs.csproj", "{84DA8EA6-EF60-4FCD-B1C6-65C1A8323B3F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RpcDisabled", "RpcDisabled\RpcDisabled.csproj", "{6800D782-8EC0-49E9-98C4-195C8F781A1F}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RpcSecurity", "RpcSecurity\RpcSecurity.csproj", "{6800D782-8EC0-49E9-98C4-195C8F781A1F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StatesDumper", "StatesDumper\StatesDumper.csproj", "{86531DB1-A231-46C4-823F-BE60972F7523}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ImportBlocks", "ImportBlocks\ImportBlocks.csproj", "{B7A42984-57BB-4F8D-967B-23B0E841B726}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ImportBlocks", "ImportBlocks\ImportBlocks.csproj", "{B7A42984-57BB-4F8D-967B-23B0E841B726}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down

0 comments on commit 15d1894

Please sign in to comment.