Skip to content

Commit 7c66609

Browse files
authored
Feature/api auth (#182)
* Add API key authentication - Adds support for API key authentication (disabled by default) for REST API - Code adopted from example provided by @joseftw * Add protection to all APIs by default - Add protection to all APIs by default, a few APIs is made public. * Remove annotations for API methods
1 parent 7e093f6 commit 7c66609

39 files changed

Lines changed: 453 additions & 7 deletions

File tree

src/Blockcore/Controllers/ConnectionManagerController.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Blockcore.Utilities;
88
using Blockcore.Utilities.Extensions;
99
using Blockcore.Utilities.JsonErrors;
10+
using Microsoft.AspNetCore.Authorization;
1011
using Microsoft.AspNetCore.Mvc;
1112
using Microsoft.Extensions.Logging;
1213

@@ -15,6 +16,7 @@ namespace Blockcore.Controllers
1516
/// <summary>
1617
/// A <see cref="FeatureController"/> that implements API and RPC methods for the connection manager.
1718
/// </summary>
19+
[Authorize]
1820
[ApiController]
1921
[ApiVersion("1")]
2022
[Route("api/[controller]")]
@@ -65,6 +67,7 @@ public IActionResult AddNode([FromQuery] string endpoint, string command)
6567
/// <see cref="https://github.com/bitcoin/bitcoin/blob/0.14/src/rpc/net.cpp"/>
6668
/// <remarks>This is an API implementation of an RPC call.</remarks>
6769
/// <returns>Json formatted <see cref="List{T}<see cref="PeerNodeModel"/>"/> of connected nodes. Returns <see cref="IActionResult"/> formatted error if fails.</returns>
70+
[AllowAnonymous]
6871
[Route("getpeerinfo")]
6972
[HttpGet]
7073
public IActionResult GetPeerInfo()

src/Blockcore/Controllers/DashboardController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
using Blockcore.AsyncWork;
2+
using Microsoft.AspNetCore.Authorization;
23
using Microsoft.AspNetCore.Mvc;
34

45
namespace Blockcore.Controllers
56
{
67
/// <summary>
78
/// Controller providing HTML Dashboard
89
/// </summary>
10+
[Authorize]
911
[ApiController]
1012
[ApiVersion("1")]
1113
[Route("api/[controller]")]

src/Blockcore/Controllers/NetworkController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Blockcore.P2P.Peer;
99
using Blockcore.Utilities.Extensions;
1010
using Blockcore.Utilities.JsonErrors;
11+
using Microsoft.AspNetCore.Authorization;
1112
using Microsoft.AspNetCore.Mvc;
1213
using Microsoft.Extensions.Logging;
1314
using NBitcoin;
@@ -17,6 +18,7 @@ namespace Blockcore.Controllers
1718
/// <summary>
1819
/// Provides methods that interact with the network elements of the full node.
1920
/// </summary>
21+
[Authorize]
2022
[ApiController]
2123
[ApiVersion("1")]
2224
[Route("api/[controller]")]

src/Blockcore/Controllers/NodeController.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using Blockcore.Utilities;
1818
using Blockcore.Utilities.JsonErrors;
1919
using Blockcore.Utilities.ModelStateErrors;
20+
using Microsoft.AspNetCore.Authorization;
2021
using Microsoft.AspNetCore.Mvc;
2122
using Microsoft.Extensions.Logging;
2223
using NBitcoin;
@@ -33,6 +34,7 @@ namespace Blockcore.Controllers
3334
/// <summary>
3435
/// Provides methods that interact with the full node.
3536
/// </summary>
37+
[Authorize]
3638
[ApiController]
3739
[ApiVersion("1")]
3840
[Route("api/[controller]")]
@@ -439,6 +441,7 @@ public async Task<IActionResult> GetTxOutAsync([FromQuery] string trxid, uint vo
439441
/// <seealso cref="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#Simple_requests"/>
440442
/// </remarks>
441443
/// <returns><see cref="OkResult"/></returns>
444+
[Authorize(Policy = "OnlyAdmins")]
442445
[HttpPost]
443446
[Route("shutdown")]
444447
[Route("stop")]
@@ -456,6 +459,7 @@ public IActionResult Shutdown([FromBody] bool corsProtection = true)
456459
/// </summary>
457460
/// <param name="request">The request containing the loggers to modify.</param>
458461
/// <returns><see cref="OkResult"/></returns>
462+
[Authorize(Policy = "OnlyAdmins")]
459463
[HttpPut]
460464
[Route("loglevels")]
461465
public IActionResult UpdateLogLevel([FromBody] LogRulesRequest request)

src/Features/Blockcore.Features.BlockStore/Api/Controllers/BlockStoreController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Blockcore.Utilities;
1010
using Blockcore.Utilities.JsonErrors;
1111
using Blockcore.Utilities.ModelStateErrors;
12+
using Microsoft.AspNetCore.Authorization;
1213
using Microsoft.AspNetCore.Mvc;
1314
using Microsoft.Extensions.Logging;
1415
using NBitcoin;
@@ -17,6 +18,7 @@ namespace Blockcore.Features.BlockStore.Api.Contollers
1718
{
1819

1920
/// <summary>Controller providing operations on a blockstore.</summary>
21+
[Authorize]
2022
[ApiController]
2123
[ApiVersion("1")]
2224
[Route("api/[controller]")]

src/Features/Blockcore.Features.ColdStaking/Api/Controllers/ColdStakingController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Blockcore.Utilities;
88
using Blockcore.Utilities.JsonErrors;
99
using Blockcore.Utilities.ModelStateErrors;
10+
using Microsoft.AspNetCore.Authorization;
1011
using Microsoft.AspNetCore.Mvc;
1112
using Microsoft.Extensions.Logging;
1213
using NBitcoin;
@@ -16,6 +17,7 @@ namespace Blockcore.Features.ColdStaking.Api.Controllers
1617
/// <summary>
1718
/// Controller providing operations for cold staking.
1819
/// </summary>
20+
[Authorize]
1921
[ApiController]
2022
[ApiVersion("1")]
2123
[Route("api/[controller]")]

src/Features/Blockcore.Features.Consensus/ConsensusController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Blockcore.Controllers;
99
using Blockcore.Utilities;
1010
using Blockcore.Utilities.JsonErrors;
11+
using Microsoft.AspNetCore.Authorization;
1112
using Microsoft.AspNetCore.Mvc;
1213
using Microsoft.Extensions.Logging;
1314
using NBitcoin;
@@ -17,6 +18,7 @@ namespace Blockcore.Features.Consensus
1718
/// <summary>
1819
/// A <see cref="FeatureController"/> that provides API and RPC methods from the consensus loop.
1920
/// </summary>
21+
[Authorize]
2022
[ApiController]
2123
[ApiVersion("1")]
2224
[Route("api/[controller]")]

src/Features/Blockcore.Features.Diagnostic/Controllers/DiagnosticController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
using Blockcore.Features.Diagnostic.Utils;
1111
using Blockcore.P2P.Peer;
1212
using Blockcore.Utilities.JsonErrors;
13+
using Microsoft.AspNetCore.Authorization;
1314
using Microsoft.AspNetCore.Mvc;
1415

1516
namespace Blockcore.Features.Diagnostic.Controllers
1617
{
1718
/// <summary>
1819
/// Controller providing diagnostic operations on fullnode.
1920
/// </summary>
21+
[Authorize]
2022
[ApiController]
2123
[ApiVersion("1")]
2224
[Route("api/[controller]/[action]")]

src/Features/Blockcore.Features.MemoryPool/MempoolController.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Blockcore.Controllers;
66
using Blockcore.Utilities;
77
using Blockcore.Utilities.JsonErrors;
8+
using Microsoft.AspNetCore.Authorization;
89
using Microsoft.AspNetCore.Mvc;
910
using Microsoft.Extensions.Logging;
1011
using NBitcoin;
@@ -14,6 +15,7 @@ namespace Blockcore.Features.MemoryPool
1415
/// <summary>
1516
/// Controller providing operations on the Mempool.
1617
/// </summary>
18+
[Authorize]
1719
[ApiVersion("1")]
1820
public class MempoolController : FeatureController
1921
{
@@ -42,6 +44,7 @@ public Task<List<uint256>> GetRawMempool()
4244
///
4345
/// </summary>
4446
/// <returns>Json formatted <see cref="List{T}<see cref="uint256"/>"/> containing the memory pool contents. Returns <see cref="IActionResult"/> formatted error if fails.</returns>
47+
[AllowAnonymous]
4548
[Route("api/[controller]/getrawmempool")]
4649
[HttpGet]
4750
public async Task<IActionResult> GetRawMempoolAsync()

src/Features/Blockcore.Features.Miner/Api/Controllers/MiningController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Blockcore.Features.Wallet.Types;
1111
using Blockcore.Utilities;
1212
using Blockcore.Utilities.JsonErrors;
13+
using Microsoft.AspNetCore.Authorization;
1314
using Microsoft.AspNetCore.Mvc;
1415
using Microsoft.Extensions.Logging;
1516
using NBitcoin;
@@ -20,6 +21,7 @@ namespace Blockcore.Features.Miner.Api.Controllers
2021
/// <summary>
2122
/// API controller for calls related to PoW mining and PoS minting.
2223
/// </summary>
24+
[Authorize]
2325
[ApiController]
2426
[ApiVersion("1")]
2527
[Route("api/[controller]")]

0 commit comments

Comments
 (0)