Skip to content

Commit

Permalink
Begin implementation of API permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
webprofusion-chrisc committed Jan 7, 2025
1 parent b2ed48a commit 786fe97
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
14 changes: 9 additions & 5 deletions src/Certify.SourceGenerators/ApiMethods.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using SourceGenerator;
Expand Down Expand Up @@ -162,7 +162,8 @@ public static List<GeneratedAPI> GetApiDefinitions()
PublicAPIController = "ManagedChallenge",
PublicAPIRoute = "list",
ServiceAPIRoute = "managedchallenge",
ReturnType = "ICollection<ManagedChallenge>"
ReturnType = "ICollection<ManagedChallenge>",
RequiredPermissions = [new ("managedchallenge", "managedchallenge_list")]
},

new GeneratedAPI {
Expand All @@ -176,7 +177,8 @@ public static List<GeneratedAPI> GetApiDefinitions()
ReturnType = "Models.Config.ActionResult",
Params = new Dictionary<string, string>{
{ "update", "Certify.Models.Hub.ManagedChallenge" }
}
},
RequiredPermissions = [new ("managedchallenge", "managedchallenge_update")]
},

new GeneratedAPI {
Expand All @@ -190,7 +192,8 @@ public static List<GeneratedAPI> GetApiDefinitions()
ReturnType = "Models.Config.ActionResult",
Params = new Dictionary<string, string>{
{ "id", "string" }
}
},
RequiredPermissions = [new ("managedchallenge", "managedchallenge_delete")]
},
new GeneratedAPI {

Expand All @@ -202,7 +205,8 @@ public static List<GeneratedAPI> GetApiDefinitions()
ReturnType = "Models.Config.ActionResult",
Params = new Dictionary<string, string>{
{ "request", "Certify.Models.Hub.ManagedChallengeRequest" }
}
},
RequiredPermissions = [new ("managedchallenge", "managedchallenge_request")]
},
new GeneratedAPI {

Expand Down
46 changes: 42 additions & 4 deletions src/Certify.SourceGenerators/PublicAPISourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Certify.SourceGenerators;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
Expand All @@ -17,12 +16,24 @@ public class GeneratedAPI
public string PublicAPIController { get; set; } = string.Empty;

public string PublicAPIRoute { get; set; } = string.Empty;
public List<PermissionSpec> RequiredPermissions { get; set; } = [];
public bool UseManagementAPI { get; set; } = false;
public string ManagementHubCommandType { get; set; } = string.Empty;
public string ServiceAPIRoute { get; set; } = string.Empty;
public string ReturnType { get; set; } = string.Empty;
public Dictionary<string, string> Params { get; set; } = new Dictionary<string, string>();
}

public class PermissionSpec
{
public string ResourceType { get; set; }
public string Action { get; set; }
public PermissionSpec(string resourceType, string action)
{
ResourceType = resourceType;
Action = action;
}
}
[Generator]
public class PublicAPISourceGenerator : ISourceGenerator
{
Expand Down Expand Up @@ -86,7 +97,7 @@ public partial class AppModel

private static void ImplementPublicAPI(GeneratorExecutionContext context, GeneratedAPI config, string apiParamDeclWithoutAuthContext, string apiParamDecl, string apiParamCall)
{
context.AddSource($"{config.PublicAPIController}Controller.{config.OperationName}.g.cs", SourceText.From($@"
var publicApiSrc = $@"
using Certify.Client;
using Certify.Server.Api.Public.Controllers;
Expand Down Expand Up @@ -115,12 +126,39 @@ public partial class {config.PublicAPIController}Controller
[Route(""""""{config.PublicAPIRoute}"""""")]
public async Task<IActionResult> {config.OperationName}({apiParamDeclWithoutAuthContext})
{{
[RequiredPermissions]
var result = await {(config.UseManagementAPI ? "_mgmtAPI" : "_client")}.{config.OperationName}({apiParamCall.Replace("authContext", "CurrentAuthContext")});
return new OkObjectResult(result);
}}
}}
}}
", Encoding.UTF8));
}};
";

if (config.RequiredPermissions.Any())
{
var fragment = "";
foreach (var perm in config.RequiredPermissions)
{
fragment += $@"
if (!await IsAuthorized(_client, ""{perm.ResourceType}"" , ""{perm.Action}""))
{{
{{
return Unauthorized();
}}
}}
";
}

publicApiSrc = publicApiSrc.Replace("[RequiredPermissions]", fragment);
}
else
{
publicApiSrc = publicApiSrc.Replace("[RequiredPermissions]", "");
}

context.AddSource($"{config.PublicAPIController}Controller.{config.OperationName}.g.cs", SourceText.From(publicApiSrc, Encoding.UTF8));

// Management API service

Expand Down

0 comments on commit 786fe97

Please sign in to comment.