Skip to content

Commit

Permalink
Add path-based filtering support for API keys (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
sondreb authored Aug 16, 2020
1 parent 2a1d8f4 commit f4b3c46
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ public class ApiKey
//public DateTime ValidTo { get; set; } // TODO: Add support for time-activated API keys.

public IReadOnlyCollection<string> Roles { get; set; }

public IReadOnlyCollection<string> Paths { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()

if (existingApiKey != null)
{
// First verify the path access is enabled, if so we'll perform a validation here.
if (this.Request.Path.HasValue && existingApiKey.Paths != null && existingApiKey.Paths.Count > 0)
{
string path = this.Request.Path.Value;
bool hasAccess = existingApiKey.Paths.Any(p => path.StartsWith(p));

if (!hasAccess)
{
// Return NoResult and return standard 401 Unauthorized result.
return AuthenticateResult.NoResult();
}
}

var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, existingApiKey.Owner)
Expand Down
3 changes: 3 additions & 0 deletions src/Features/Blockcore.Features.NodeHost/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public void ConfigureServices(IServiceCollection services)
{
NodeHostSettings hostSettings = fullNode.Services.ServiceProvider.GetService<NodeHostSettings>();

// Make the configuration available to custom features.
services.AddSingleton(this.Configuration);

services.AddLogging(loggingBuilder =>
{
loggingBuilder.AddConfiguration(this.Configuration.GetSection("Logging"));
Expand Down
13 changes: 0 additions & 13 deletions src/Features/Blockcore.Features.NodeHost/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,5 @@
"System": "Information",
"Microsoft": "Information"
}
},
"Blockcore": {
"API": {
"Keys": [
{
"Id": 1,
"Enabled": false,
"Owner": "Admin",
"Key": "1ca8f906-a23e-48b2-8b83-e95290986d0e",
"Roles": [ "User", "Admin" ]
}
]
}
}
}
31 changes: 31 additions & 0 deletions src/Node/Blockcore.Node/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Information",
"System": "Information",
"Microsoft": "Information"
}
},
"Blockcore": {
"API": {
"Keys": [
{
"Id": 1,
"Enabled": false,
"Owner": "Admin",
"Key": "1ca8f906-a23e-48b2-8b83-e95290986d0e",
"Roles": [ "User", "Admin" ]
},
{
"Id": 2,
"Enabled": false,
"Owner": "Registry",
"Key": "132525f1-46d2-45eb-bfe5-8a354b63ce36",
"Roles": [ "User" ],
"Paths": [ "/api/identity", "/api/storage", "/.well-known" ]
}
]
}
}
}

0 comments on commit f4b3c46

Please sign in to comment.