forked from neo-project/neo-modules
-
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.
* Rpc Basic Auth * change plugin name to RpcSecurity
- Loading branch information
Showing
6 changed files
with
55 additions
and
21 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,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; | ||
} | ||
} | ||
} |
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
2 changes: 2 additions & 0 deletions
2
RpcDisabled/RpcDisabled/config.json → RpcSecurity/RpcSecurity/config.json
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
{ | ||
"PluginConfiguration": { | ||
"RpcUser": "", | ||
"RpcPass": "", | ||
"DisabledMethods": [] | ||
} | ||
} |
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