Skip to content

Commit 9185dbe

Browse files
committed
Simplify authorization behavior usage by allowing to register a global template
1 parent d31819d commit 9185dbe

12 files changed

Lines changed: 398 additions & 19 deletions

File tree

src/Dibix.Sdk.CodeGeneration/Configuration/CodeGenerationUserConfigurationReader.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ private void CollectTemplates(JObject endpointConfiguration)
133133

134134
foreach (JProperty template in templates.Properties())
135135
{
136-
CollectTemplate(template.Name, (JObject)template.Value);
136+
if (template.Name == "Authorization")
137+
CollectAuthorizationTemplate((JObject)template.Value);
138+
else
139+
CollectTemplate(template.Name, (JObject)template.Value);
137140
}
138141
}
139142

@@ -149,5 +152,14 @@ private static void CollectTemplate(ConfigurationTemplate template, JObject temp
149152
JProperty actionTemplateProperty = templateJson.Property(propertyName)!;
150153
template.Action = (JObject)actionTemplateProperty.Value;
151154
}
155+
156+
private void CollectAuthorizationTemplate(JObject templates)
157+
{
158+
foreach (JProperty templateJson in templates.Properties())
159+
{
160+
ConfigurationAuthorizationTemplate template = new ConfigurationAuthorizationTemplate(templateJson.Name, (JObject)templateJson.Value);
161+
_configuration.ConfigurationTemplates.Authorization.Register(template);
162+
}
163+
}
152164
}
153165
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Newtonsoft.Json.Linq;
2+
3+
namespace Dibix.Sdk.CodeGeneration
4+
{
5+
public sealed class ConfigurationAuthorizationTemplate
6+
{
7+
public string Name { get; }
8+
public JObject Content { get; }
9+
10+
public ConfigurationAuthorizationTemplate(string name, JObject content)
11+
{
12+
Name = name;
13+
Content = content;
14+
}
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Collections.Generic;
2+
3+
namespace Dibix.Sdk.CodeGeneration
4+
{
5+
public sealed class ConfigurationAuthorizationTemplates
6+
{
7+
private readonly IDictionary<string, ConfigurationAuthorizationTemplate> _map = new Dictionary<string, ConfigurationAuthorizationTemplate>();
8+
9+
public void Register(ConfigurationAuthorizationTemplate template) => _map.Add(template.Name, template);
10+
11+
public bool TryGetTemplate(string name, out ConfigurationAuthorizationTemplate template) => _map.TryGetValue(name, out template);
12+
}
13+
}

src/Dibix.Sdk.CodeGeneration/Configuration/ConfigurationTemplates.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public sealed class ConfigurationTemplates
88
private readonly IDictionary<string, ConfigurationTemplate> _map = new Dictionary<string, ConfigurationTemplate>();
99

1010
public ConfigurationTemplate Default => _map.TryGetValue(DefaultTemplateKey, out ConfigurationTemplate template) ? template : new ConfigurationTemplate(DefaultTemplateKey);
11+
public ConfigurationAuthorizationTemplates Authorization { get; } = new ConfigurationAuthorizationTemplates();
1112

1213
public void Register(ConfigurationTemplate template) => _map.Add(template.Name, template);
1314
}

src/Dibix.Sdk.CodeGeneration/Registration/ControllerDefinitionProvider.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,14 @@ private void CollectAuthorization(JProperty property, JTokenType type, ActionDef
557557
switch (type)
558558
{
559559
case JTokenType.Object:
560-
JObject authorization = (JObject)property.Value;
560+
JObject authorizationValue = (JObject)property.Value;
561+
JProperty templateProperty = authorizationValue.Property("name");
562+
JObject authorization = authorizationValue;
563+
564+
// In case of error that has been previously logged
565+
if (templateProperty != null && (authorization = ApplyAuthorizationTemplate(templateProperty, filePath, authorizationValue)) == null)
566+
return;
567+
561568
IDictionary<string, ExplicitParameter> explicitParameters = new Dictionary<string, ExplicitParameter>();
562569
CollectActionParameters(authorization, filePath, explicitParameters, requestBody: null);
563570
IDictionary<string, PathParameter> pathParameters = new Dictionary<string, PathParameter>();
@@ -573,6 +580,35 @@ private void CollectAuthorization(JProperty property, JTokenType type, ActionDef
573580
}
574581
}
575582

583+
private JObject ApplyAuthorizationTemplate(JProperty templateNameProperty, string filePath, JObject authorizationTemplateReference)
584+
{
585+
string templateName = (string)templateNameProperty.Value;
586+
if (!_templates.Authorization.TryGetTemplate(templateName, out ConfigurationAuthorizationTemplate template))
587+
{
588+
IJsonLineInfo templateNameLineInfo = templateNameProperty.Value.GetLineInfo();
589+
Logger.LogError($"Unknown authorization template '{templateName}'", filePath, templateNameLineInfo.LineNumber, templateNameLineInfo.LinePosition);
590+
return null;
591+
}
592+
593+
templateNameProperty.Remove();
594+
595+
JObject resolvedAuthorization = new JObject();
596+
597+
if (authorizationTemplateReference.HasValues)
598+
{
599+
JObject @params = new JObject();
600+
resolvedAuthorization.Add(new JProperty("params", @params));
601+
602+
foreach (JProperty authorizationParameterProperty in authorizationTemplateReference.Properties())
603+
{
604+
@params.Add(authorizationParameterProperty);
605+
}
606+
}
607+
608+
JObject mergedAuthorization = ApplyTemplate(template.Content, resolvedAuthorization);
609+
return mergedAuthorization;
610+
}
611+
576612
private TypeReference ResolveType(JValue typeNameValue, string filePath)
577613
{
578614
string typeName = (string)typeNameValue;

src/Dibix.Sdk.CodeGeneration/Schema/dibix.endpoints.schema.json

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@
152152
}
153153
]
154154
},
155-
"Authorization": {
155+
"AuthorizationTarget": {
156156
"anyOf": [
157157
{
158158
"const": "none"
@@ -174,6 +174,23 @@
174174
}
175175
]
176176
},
177+
"AuthorizationTemplateReference": {
178+
"type": "object",
179+
"properties": {
180+
"name": {
181+
"$ref": "#/definitions/NameIdentifier"
182+
}
183+
},
184+
"patternProperties": {
185+
"^[a-z]+$": {
186+
"$ref": "#/definitions/ConstantSource"
187+
}
188+
},
189+
"required": [
190+
"name"
191+
],
192+
"additionalProperties": false
193+
},
177194
"FileResponse": {
178195
"properties": {
179196
"mediaType": {
@@ -273,7 +290,14 @@
273290
"$ref": "#/definitions/SecuritySchemes"
274291
},
275292
"authorization": {
276-
"$ref": "#/definitions/Authorization"
293+
"anyOf": [
294+
{
295+
"$ref": "#/definitions/AuthorizationTarget"
296+
},
297+
{
298+
"$ref": "#/definitions/AuthorizationTemplateReference"
299+
}
300+
]
277301
},
278302
"fileResponse": {
279303
"$ref": "#/definitions/FileResponse"

src/Dibix.Sdk/Schema/dibix.configuration.schema.json

Lines changed: 176 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,169 @@
11
{
22
"$schema": "http://json-schema.org/draft-04/schema#",
33
"definitions": {
4+
"NameIdentifier": {
5+
"type": "string",
6+
"pattern": "^[A-Z]([A-Za-z.]+)?(,[A-Z]([A-Za-z.]+)?)?$"
7+
},
8+
"ConstantSource": {
9+
"anyOf": [
10+
{
11+
"type": "boolean"
12+
},
13+
{
14+
"type": "number"
15+
},
16+
{
17+
"type": "string",
18+
"pattern": "^[^.]+$"
19+
},
20+
{
21+
"type": "null"
22+
}
23+
]
24+
},
25+
"ConstantSource": {
26+
"anyOf": [
27+
{
28+
"type": "boolean"
29+
},
30+
{
31+
"type": "number"
32+
},
33+
{
34+
"type": "string",
35+
"pattern": "^[^.]+$"
36+
},
37+
{
38+
"type": "null"
39+
}
40+
]
41+
},
42+
"PropertySource": {
43+
"anyOf": [
44+
{
45+
"type": "string",
46+
"pattern": "^[A-Z]+((\\.[A-Za-z-]+)|((\\.[A-Z][A-Za-z]+?){1,})|(\\.\\$[A-Z]+))$"
47+
},
48+
{
49+
"$ref": "#/definitions/ComplexPropertySource"
50+
}
51+
]
52+
},
53+
"ComplexPropertySource": {
54+
"type": "object",
55+
"minProperties": 2,
56+
"additionalProperties": false,
57+
"properties": {
58+
"source": {
59+
"$ref": "#/definitions/PropertySource"
60+
},
61+
"converter": {
62+
"type": "string",
63+
"pattern": "^[A-Z]+$"
64+
}
65+
},
66+
"required": [
67+
"source",
68+
"converter"
69+
]
70+
},
71+
"ItemsPropertySource": {
72+
"type": "object",
73+
"minProperties": 2,
74+
"additionalProperties": false,
75+
"properties": {
76+
"source": {
77+
"$ref": "#/definitions/PropertySource"
78+
},
79+
"items": {
80+
"type": "object",
81+
"patternProperties": {
82+
"^[a-z]+$": {
83+
"anyOf": [
84+
{
85+
"$ref": "#/definitions/Source"
86+
}
87+
]
88+
}
89+
}
90+
}
91+
},
92+
"required": [
93+
"source",
94+
"items"
95+
]
96+
},
97+
"BodySource": {
98+
"type": "object",
99+
"minProperties": 1,
100+
"additionalProperties": false,
101+
"properties": {
102+
"convertFromBody": {
103+
"$ref": "#/definitions/NameIdentifier"
104+
}
105+
}
106+
},
107+
"Source": {
108+
"anyOf": [
109+
{
110+
"$ref": "#/definitions/ConstantSource"
111+
},
112+
{
113+
"$ref": "#/definitions/PropertySource"
114+
}
115+
]
116+
},
117+
"ParameterMappings": {
118+
"type": "object",
119+
"patternProperties": {
120+
"^[a-z]+$": {
121+
"anyOf": [
122+
{
123+
"$ref": "#/definitions/Source"
124+
},
125+
{
126+
"$ref": "#/definitions/ItemsPropertySource"
127+
},
128+
{
129+
"$ref": "#/definitions/BodySource"
130+
}
131+
]
132+
}
133+
},
134+
"minProperties": 1,
135+
"additionalProperties": false
136+
},
137+
"SecuritySchemes": {
138+
"anyOf": [
139+
{
140+
"type": "string"
141+
},
142+
{
143+
"type": "array",
144+
"minItems": 1,
145+
"uniqueItems": true,
146+
"items": {
147+
"type": "string"
148+
}
149+
}
150+
]
151+
},
152+
"AuthorizationTarget": {
153+
"type": "object",
154+
"properties": {
155+
"target": {
156+
"$ref": "#/definitions/NameIdentifier"
157+
},
158+
"params": {
159+
"$ref": "#/definitions/ParameterMappings"
160+
}
161+
},
162+
"required": [
163+
"target"
164+
],
165+
"additionalProperties": false
166+
},
4167
"Configuration": {
5168
"type": "object",
6169
"properties": {
@@ -96,21 +259,6 @@
96259
"additionalProperties": false,
97260
"minProperties": 1
98261
},
99-
"SecuritySchemes": {
100-
"anyOf": [
101-
{
102-
"type": "string"
103-
},
104-
{
105-
"type": "array",
106-
"minItems": 1,
107-
"uniqueItems": true,
108-
"items": {
109-
"type": "string"
110-
}
111-
}
112-
]
113-
},
114262
"ActionTemplate": {
115263
"type": "object",
116264
"properties": {
@@ -131,11 +279,24 @@
131279
"additionalProperties": false,
132280
"minProperties": 1
133281
},
282+
"AuthorizationTemplate": {
283+
"type": "object",
284+
"patternProperties": {
285+
"^[A-Z]([A-Za-z]+)?$": {
286+
"$ref": "#/definitions/AuthorizationTarget"
287+
}
288+
},
289+
"additionalProperties": false,
290+
"minProperties": 1
291+
},
134292
"Templates": {
135293
"type": "object",
136294
"properties": {
137295
"Default": {
138296
"$ref": "#/definitions/Template"
297+
},
298+
"Authorization": {
299+
"$ref": "#/definitions/AuthorizationTemplate"
139300
}
140301
},
141302
"additionalProperties": false,

0 commit comments

Comments
 (0)