Skip to content

Commit 786fe97

Browse files
Begin implementation of API permissions
1 parent b2ed48a commit 786fe97

File tree

2 files changed

+51
-9
lines changed

2 files changed

+51
-9
lines changed

src/Certify.SourceGenerators/ApiMethods.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Linq;
44
using SourceGenerator;
@@ -162,7 +162,8 @@ public static List<GeneratedAPI> GetApiDefinitions()
162162
PublicAPIController = "ManagedChallenge",
163163
PublicAPIRoute = "list",
164164
ServiceAPIRoute = "managedchallenge",
165-
ReturnType = "ICollection<ManagedChallenge>"
165+
ReturnType = "ICollection<ManagedChallenge>",
166+
RequiredPermissions = [new ("managedchallenge", "managedchallenge_list")]
166167
},
167168

168169
new GeneratedAPI {
@@ -176,7 +177,8 @@ public static List<GeneratedAPI> GetApiDefinitions()
176177
ReturnType = "Models.Config.ActionResult",
177178
Params = new Dictionary<string, string>{
178179
{ "update", "Certify.Models.Hub.ManagedChallenge" }
179-
}
180+
},
181+
RequiredPermissions = [new ("managedchallenge", "managedchallenge_update")]
180182
},
181183

182184
new GeneratedAPI {
@@ -190,7 +192,8 @@ public static List<GeneratedAPI> GetApiDefinitions()
190192
ReturnType = "Models.Config.ActionResult",
191193
Params = new Dictionary<string, string>{
192194
{ "id", "string" }
193-
}
195+
},
196+
RequiredPermissions = [new ("managedchallenge", "managedchallenge_delete")]
194197
},
195198
new GeneratedAPI {
196199

@@ -202,7 +205,8 @@ public static List<GeneratedAPI> GetApiDefinitions()
202205
ReturnType = "Models.Config.ActionResult",
203206
Params = new Dictionary<string, string>{
204207
{ "request", "Certify.Models.Hub.ManagedChallengeRequest" }
205-
}
208+
},
209+
RequiredPermissions = [new ("managedchallenge", "managedchallenge_request")]
206210
},
207211
new GeneratedAPI {
208212

src/Certify.SourceGenerators/PublicAPISourceGenerator.cs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Diagnostics;
33
using System.Linq;
44
using System.Text;
5-
using System.Threading.Tasks;
65
using Certify.SourceGenerators;
76
using Microsoft.CodeAnalysis;
87
using Microsoft.CodeAnalysis.Text;
@@ -17,12 +16,24 @@ public class GeneratedAPI
1716
public string PublicAPIController { get; set; } = string.Empty;
1817

1918
public string PublicAPIRoute { get; set; } = string.Empty;
19+
public List<PermissionSpec> RequiredPermissions { get; set; } = [];
2020
public bool UseManagementAPI { get; set; } = false;
2121
public string ManagementHubCommandType { get; set; } = string.Empty;
2222
public string ServiceAPIRoute { get; set; } = string.Empty;
2323
public string ReturnType { get; set; } = string.Empty;
2424
public Dictionary<string, string> Params { get; set; } = new Dictionary<string, string>();
2525
}
26+
27+
public class PermissionSpec
28+
{
29+
public string ResourceType { get; set; }
30+
public string Action { get; set; }
31+
public PermissionSpec(string resourceType, string action)
32+
{
33+
ResourceType = resourceType;
34+
Action = action;
35+
}
36+
}
2637
[Generator]
2738
public class PublicAPISourceGenerator : ISourceGenerator
2839
{
@@ -86,7 +97,7 @@ public partial class AppModel
8697

8798
private static void ImplementPublicAPI(GeneratorExecutionContext context, GeneratedAPI config, string apiParamDeclWithoutAuthContext, string apiParamDecl, string apiParamCall)
8899
{
89-
context.AddSource($"{config.PublicAPIController}Controller.{config.OperationName}.g.cs", SourceText.From($@"
100+
var publicApiSrc = $@"
90101
91102
using Certify.Client;
92103
using Certify.Server.Api.Public.Controllers;
@@ -115,12 +126,39 @@ public partial class {config.PublicAPIController}Controller
115126
[Route(""""""{config.PublicAPIRoute}"""""")]
116127
public async Task<IActionResult> {config.OperationName}({apiParamDeclWithoutAuthContext})
117128
{{
129+
130+
[RequiredPermissions]
131+
118132
var result = await {(config.UseManagementAPI ? "_mgmtAPI" : "_client")}.{config.OperationName}({apiParamCall.Replace("authContext", "CurrentAuthContext")});
119133
return new OkObjectResult(result);
120134
}}
121135
}}
122-
}}
123-
", Encoding.UTF8));
136+
}};
137+
";
138+
139+
if (config.RequiredPermissions.Any())
140+
{
141+
var fragment = "";
142+
foreach (var perm in config.RequiredPermissions)
143+
{
144+
fragment += $@"
145+
if (!await IsAuthorized(_client, ""{perm.ResourceType}"" , ""{perm.Action}""))
146+
{{
147+
{{
148+
return Unauthorized();
149+
}}
150+
}}
151+
";
152+
}
153+
154+
publicApiSrc = publicApiSrc.Replace("[RequiredPermissions]", fragment);
155+
}
156+
else
157+
{
158+
publicApiSrc = publicApiSrc.Replace("[RequiredPermissions]", "");
159+
}
160+
161+
context.AddSource($"{config.PublicAPIController}Controller.{config.OperationName}.g.cs", SourceText.From(publicApiSrc, Encoding.UTF8));
124162

125163
// Management API service
126164

0 commit comments

Comments
 (0)