Skip to content

Commit 2dd1062

Browse files
authored
Merge pull request #17 from DeeJayTC/5-auth
Add Authorization & JWT Handling
2 parents 1505017 + d7889ff commit 2dd1062

29 files changed

+887
-351
lines changed

TCDev.APIGenerator.sln

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1414
EndProject
1515
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TCDev.APIGenerator.Caching", "src\TCDev.APIGenerator.Caching\TCDev.APIGenerator.Caching.csproj", "{0C8E23AD-AC5D-41D4-9F67-0ECF3D1C4BE1}"
1616
EndProject
17-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TCDev.APIGenerator.GraphQL", "src\TCDev.APIGenerator.GraphQL\TCDev.APIGenerator.GraphQL.csproj", "{EDEA4DF4-49DF-4205-9B8E-61D76F26BA8D}"
18-
EndProject
1917
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TCDev.APIGenerator.Schema", "src\TCDev.APIGenerator.Schema\TCDev.APIGenerator.Schema.csproj", "{94E59385-D259-40A1-A373-1FBD0A42CD63}"
2018
EndProject
2119
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ApiGenerator", "ApiGenerator", "{4189D7E0-F171-4267-AC64-C9A83BB1B559}"
@@ -59,11 +57,6 @@ Global
5957
{0C8E23AD-AC5D-41D4-9F67-0ECF3D1C4BE1}.Release|Any CPU.ActiveCfg = Release|Any CPU
6058
{0C8E23AD-AC5D-41D4-9F67-0ECF3D1C4BE1}.SampleAppJson|Any CPU.ActiveCfg = SampleAppJson|Any CPU
6159
{0C8E23AD-AC5D-41D4-9F67-0ECF3D1C4BE1}.SampleAppNuget|Any CPU.ActiveCfg = SampleAppNuget|Any CPU
62-
{EDEA4DF4-49DF-4205-9B8E-61D76F26BA8D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
63-
{EDEA4DF4-49DF-4205-9B8E-61D76F26BA8D}.DebugWithSampleApp|Any CPU.ActiveCfg = DebugWithSampleApp|Any CPU
64-
{EDEA4DF4-49DF-4205-9B8E-61D76F26BA8D}.Release|Any CPU.ActiveCfg = Release|Any CPU
65-
{EDEA4DF4-49DF-4205-9B8E-61D76F26BA8D}.SampleAppJson|Any CPU.ActiveCfg = SampleAppJson|Any CPU
66-
{EDEA4DF4-49DF-4205-9B8E-61D76F26BA8D}.SampleAppNuget|Any CPU.ActiveCfg = SampleAppNuget|Any CPU
6760
{94E59385-D259-40A1-A373-1FBD0A42CD63}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
6861
{94E59385-D259-40A1-A373-1FBD0A42CD63}.Debug|Any CPU.Build.0 = Debug|Any CPU
6962
{94E59385-D259-40A1-A373-1FBD0A42CD63}.DebugWithSampleApp|Any CPU.ActiveCfg = DebugWithSampleApp|Any CPU
@@ -106,7 +99,6 @@ Global
10699
{FE869C02-6C9A-4D9B-BBE2-56F1B21B2A55} = {4189D7E0-F171-4267-AC64-C9A83BB1B559}
107100
{303BF897-594C-4911-91CF-3887A8B8E839} = {8CC9B68F-E1C2-45B3-8814-B9FF4E1B2AB8}
108101
{0C8E23AD-AC5D-41D4-9F67-0ECF3D1C4BE1} = {4189D7E0-F171-4267-AC64-C9A83BB1B559}
109-
{EDEA4DF4-49DF-4205-9B8E-61D76F26BA8D} = {4189D7E0-F171-4267-AC64-C9A83BB1B559}
110102
{94E59385-D259-40A1-A373-1FBD0A42CD63} = {4189D7E0-F171-4267-AC64-C9A83BB1B559}
111103
{BA9E04E6-4B66-4369-9B2F-C6CEC9499851} = {8CC9B68F-E1C2-45B3-8814-B9FF4E1B2AB8}
112104
{7F3574D1-7421-4824-A0BB-522F3BC9BAC4} = {4189D7E0-F171-4267-AC64-C9A83BB1B559}

sample/ApiGeneratorSampleApp/ApiDefinition.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
"name": "MakeJSON",
55
"route": "/MakeJSON",
66
"idType": "int",
7+
"authorize": true,
8+
"scopesRead": [ "all.read" ],
9+
"scopesWrite": [ "all.write" ],
710
"Fields": [
811
{
912
"name": "Name",

sample/ApiGeneratorSampleApp/Model/Car.cs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
namespace ApiGeneratorSampleApI.Model
88
{
99

10-
[Api("/car")]
11-
public class Car : IObjectBase<Guid>
10+
[Api("/car",
11+
authorize: true,
12+
requiredReadScopes: new string[] { "car.read" },
13+
requiredWriteScopes: new string[] { "car.write" })]
14+
public class Car : IObjectBase<Guid>
1215
{
1316
[Key]
1417
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
@@ -24,11 +27,16 @@ public class Car : IObjectBase<Guid>
2427
public string Color { get; set; }
2528

2629
public Make? Make { get; set; }
30+
31+
public Model? Model { get; set; }
2732
}
2833

2934

30-
[Api("/carMakes")]
31-
public class Make : IObjectBase<Guid>
35+
[Api("/carMakes",
36+
authorize: true,
37+
requiredReadScopes: new string[] { "make.read" },
38+
requiredWriteScopes: new string[] { "make.write" })]
39+
public class Make : IObjectBase<Guid>
3240
{
3341
[Key]
3442
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
@@ -37,5 +45,26 @@ public class Make : IObjectBase<Guid>
3745
public string Name { get; set; }
3846

3947
public string Description { get; set; }
48+
49+
50+
public Model? Model { get; set; }
51+
}
52+
53+
54+
55+
[Api("/carModel",
56+
authorize: true,
57+
requiredReadScopes: new string[] { "model.read" },
58+
requiredWriteScopes: new string[] { "model.write" })]
59+
public class Model : IObjectBase<Guid>
60+
{
61+
[Key]
62+
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
63+
[SwaggerIgnore]
64+
public Guid Id { get; set; } = Guid.NewGuid();
65+
public string Name { get; set; }
66+
67+
public string Description { get; set; }
4068
}
69+
4170
}

sample/ApiGeneratorSampleApp/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
using Microsoft.AspNetCore.Builder;
33
using Microsoft.Extensions.DependencyInjection;
44
using TCDev.ApiGenerator.Extension;
5+
using TCDev.APIGenerator.Identity;
56

67
var builder = WebApplication.CreateBuilder(args);
78

89
// Add services to the container.
910

1011
builder.Services.AddControllers();
1112

12-
//builder.Services.AddApiGeneratorServices(builder.Configuration, JsonClassBuilder.CreateClass());
13+
builder.Services.AddApiGeneratorIdentity(builder.Configuration);
1314
builder.Services.AddApiGeneratorServices(builder.Configuration, Assembly.GetExecutingAssembly());
1415

1516
var app = builder.Build();
@@ -23,8 +24,7 @@
2324
app.UseStaticFiles();
2425
app.UseRouting();
2526

26-
app.UseAuthentication();
27-
app.UseAuthorization();
27+
app.UseApiGeneratorAuthentication();
2828

2929
app.UseEndpoints(endpoints =>
3030
{

sample/ApiGeneratorSampleApp/appsettings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ For more info see https://aka.ms/dotnet-template-ms-identity-platform
2727
"Api": {
2828
"Swagger": {
2929
"EnableProduction": "false", // Enable/Disable for production builds
30-
"Description": "Sample Swagger Config",
30+
"Description": "Smoower API Sample",
3131
"Version": "v1",
32-
"Title": "ssass Swagger Config Title",
32+
"Title": "Smoower sample config",
3333
"ContactMail": "Me@me.de",
3434
"ContactUri": "https://www.myuri.com"
3535
},

sample/SampleAppJson/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
var app = builder.Build();
1515

1616
// Configure the HTTP request pipeline.
17-
17+
app.UseAutomaticApiMigrations();
1818
app.UseHttpsRedirection();
1919

2020
app.UseAuthorization();
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.Security.Claims;
2+
using Microsoft.AspNetCore.Authentication.JwtBearer;
3+
using Microsoft.AspNetCore.Builder;
4+
using Microsoft.Extensions.Configuration;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.IdentityModel.Tokens;
7+
8+
namespace TCDev.APIGenerator.Identity
9+
{
10+
public static class ServiceExtension
11+
{
12+
public static IServiceCollection ConfigureIdentity(this IServiceCollection services, IConfiguration configuration)
13+
{
14+
string domain = $"https://{configuration["Auth0:Domain"]}/";
15+
services
16+
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
17+
.AddJwtBearer(options =>
18+
{
19+
options.Authority = domain;
20+
options.Audience = configuration["Auth0:Audience"];
21+
options.TokenValidationParameters = new TokenValidationParameters
22+
{
23+
NameClaimType = ClaimTypes.NameIdentifier
24+
};
25+
});
26+
27+
28+
return services;
29+
}
30+
31+
public static IApplicationBuilder UseApiGeneratorAuthentication(this IApplicationBuilder app)
32+
{
33+
app.UseAuthentication();
34+
app.UseAuthorization();
35+
36+
return app;
37+
}
38+
39+
40+
}
41+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.3" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,71 @@
1-
using Newtonsoft.Json;
1+
// TCDev.de 2022/04/07
2+
// TCDev.APIGenerator.Schema.JsonClassDefinition.cs
3+
// https://github.com/DeeJayTC/net-dynamic-api
4+
25
using System;
36
using System.Collections.Generic;
47
using System.Linq;
5-
using System.Runtime.CompilerServices;
6-
using System.Text;
7-
using System.Text.Json.Serialization;
8-
using System.Threading.Tasks;
8+
using Newtonsoft.Json;
99

1010
namespace TCDev.APIGenerator.Schema
1111
{
12-
[Flags]
13-
public enum Events
14-
{
15-
POST,
16-
PUT,
17-
DELETE,
18-
ALL = POST | PUT | DELETE
19-
}
12+
[Flags]
13+
public enum Events
14+
{
15+
POST,
16+
PUT,
17+
DELETE,
18+
ALL = POST | PUT | DELETE
19+
}
20+
21+
22+
public class JsonClassDefinition
23+
{
24+
public string Name { get; set; }
25+
26+
[JsonProperty("route")]
27+
public string RouteTemplate { get; set; } = "/";
28+
29+
[JsonProperty("caching")]
30+
public bool EnableCaching { get; set; }
31+
32+
[JsonProperty("idType")]
33+
public string IdType { get; set; } = "int";
2034

35+
public bool Authorize { get; set; } = false;
2136

22-
public class JsonClassDefinition
23-
{
24-
public string Name { get; set; }
37+
[JsonProperty("ScopesRead")]
38+
public List<string> ScopesReadList { get; set; } = new List<string>();
2539

26-
[JsonProperty("route")]
27-
public string RouteTemplate { get; set; } = "/";
40+
[JsonProperty("ScopesWrite")]
41+
public List<string> ScopesWriteList { get; set; } = new List<string>();
2842

29-
[JsonProperty("caching")]
30-
public bool EnableCaching { get; set; } = false;
3143

32-
[JsonProperty("idType")]
33-
public string IdType { get; set; } = "int";
44+
[JsonIgnore]
45+
public string ScopesRead {
46+
get
47+
{
48+
return ScopesReadList.Any() ? string.Join(",", ScopesReadList.Select(p => $"\"{p}\"").ToList()) : string.Empty;
49+
}
50+
}
51+
[JsonIgnore]
52+
public string ScopesWrite
53+
{
54+
get
55+
{
56+
return ScopesWriteList.Any() ? string.Join(",", ScopesWriteList.Select(p => $"\"{p}\"").ToList()) : string.Empty;
57+
}
58+
}
3459

35-
public List<Field> Fields { get; set; }
36-
}
60+
public List<Field> Fields { get; set; }
61+
}
3762

3863

39-
public class Field
40-
{
41-
public string Name { get; set; }
42-
public string Type { get; set; }
43-
public bool Nullable { get; set; }
44-
}
64+
public class Field
65+
{
66+
public string Name { get; set; }
67+
public string Type { get; set; }
68+
public bool Nullable { get; set; }
69+
public string MaxLength { get; set; }
70+
}
4571
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// TCDev.de 2022/04/10
2+
// TCDev.APIGenerator.ApiAttribute.cs
3+
// https://github.com/DeeJayTC/net-dynamic-api
4+
5+
using System;
6+
using System.Linq;
7+
using Microsoft.AspNetCore.Authorization;
8+
using Microsoft.AspNetCore.Mvc;
9+
using Microsoft.AspNetCore.Mvc.Filters;
10+
11+
namespace TCDev.ApiGenerator.Attributes
12+
{
13+
[AttributeUsage(AttributeTargets.Class)]
14+
public class ApiAttribute : Attribute
15+
{
16+
/// <summary>
17+
/// Attribute defining auto generated controller for the class
18+
/// </summary>
19+
/// <param name="route">The full base route for the class ie /myclass/ </param>
20+
/// <param name="requiredReadScopes"></param>
21+
/// <param name="requiredWriteScopes"></param>
22+
/// <param name="fireEvents"></param>
23+
/// <param name="authorize"></param>
24+
/// <param name="cache"></param>
25+
/// <param name="cacheDuration"></param>
26+
/// <param name="methods">The methods to generate for this endpoint</param>
27+
public ApiAttribute(
28+
string route,
29+
ApiMethodsToGenerate methods = ApiMethodsToGenerate.All,
30+
string[] requiredReadScopes = null,
31+
string[] requiredWriteScopes = null,
32+
bool fireEvents = false,
33+
bool authorize = true,
34+
bool cache = false,
35+
int cacheDuration = 50000)
36+
{
37+
this.Route = route;
38+
this.Options = new ApiAttributeAttributeOptions
39+
{
40+
RequiredReadScopes = requiredReadScopes,
41+
RequiredWriteScopes = requiredWriteScopes,
42+
Authorize = authorize,
43+
Cache = cache,
44+
CacheDuration = cacheDuration,
45+
FireEvents = fireEvents,
46+
Methods = methods
47+
};
48+
}
49+
50+
public string Route { get; set; }
51+
public ApiAttributeAttributeOptions Options { get; set; }
52+
53+
}
54+
}

0 commit comments

Comments
 (0)