-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from DeeJayTC/5-auth
Add Authorization & JWT Handling
- Loading branch information
Showing
29 changed files
with
887 additions
and
351 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System.Security.Claims; | ||
using Microsoft.AspNetCore.Authentication.JwtBearer; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.IdentityModel.Tokens; | ||
|
||
namespace TCDev.APIGenerator.Identity | ||
{ | ||
public static class ServiceExtension | ||
{ | ||
public static IServiceCollection ConfigureIdentity(this IServiceCollection services, IConfiguration configuration) | ||
{ | ||
string domain = $"https://{configuration["Auth0:Domain"]}/"; | ||
services | ||
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) | ||
.AddJwtBearer(options => | ||
{ | ||
options.Authority = domain; | ||
options.Audience = configuration["Auth0:Audience"]; | ||
options.TokenValidationParameters = new TokenValidationParameters | ||
{ | ||
NameClaimType = ClaimTypes.NameIdentifier | ||
}; | ||
}); | ||
|
||
|
||
return services; | ||
} | ||
|
||
public static IApplicationBuilder UseApiGeneratorAuthentication(this IApplicationBuilder app) | ||
{ | ||
app.UseAuthentication(); | ||
app.UseAuthorization(); | ||
|
||
return app; | ||
} | ||
|
||
|
||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/TCDev.APIGenerator.Identity/TCDev.APIGenerator.Identity.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.3" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,71 @@ | ||
using Newtonsoft.Json; | ||
// TCDev.de 2022/04/07 | ||
// TCDev.APIGenerator.Schema.JsonClassDefinition.cs | ||
// https://github.com/DeeJayTC/net-dynamic-api | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Runtime.CompilerServices; | ||
using System.Text; | ||
using System.Text.Json.Serialization; | ||
using System.Threading.Tasks; | ||
using Newtonsoft.Json; | ||
|
||
namespace TCDev.APIGenerator.Schema | ||
{ | ||
[Flags] | ||
public enum Events | ||
{ | ||
POST, | ||
PUT, | ||
DELETE, | ||
ALL = POST | PUT | DELETE | ||
} | ||
[Flags] | ||
public enum Events | ||
{ | ||
POST, | ||
PUT, | ||
DELETE, | ||
ALL = POST | PUT | DELETE | ||
} | ||
|
||
|
||
public class JsonClassDefinition | ||
{ | ||
public string Name { get; set; } | ||
|
||
[JsonProperty("route")] | ||
public string RouteTemplate { get; set; } = "/"; | ||
|
||
[JsonProperty("caching")] | ||
public bool EnableCaching { get; set; } | ||
|
||
[JsonProperty("idType")] | ||
public string IdType { get; set; } = "int"; | ||
|
||
public bool Authorize { get; set; } = false; | ||
|
||
public class JsonClassDefinition | ||
{ | ||
public string Name { get; set; } | ||
[JsonProperty("ScopesRead")] | ||
public List<string> ScopesReadList { get; set; } = new List<string>(); | ||
|
||
[JsonProperty("route")] | ||
public string RouteTemplate { get; set; } = "/"; | ||
[JsonProperty("ScopesWrite")] | ||
public List<string> ScopesWriteList { get; set; } = new List<string>(); | ||
|
||
[JsonProperty("caching")] | ||
public bool EnableCaching { get; set; } = false; | ||
|
||
[JsonProperty("idType")] | ||
public string IdType { get; set; } = "int"; | ||
[JsonIgnore] | ||
public string ScopesRead { | ||
get | ||
{ | ||
return ScopesReadList.Any() ? string.Join(",", ScopesReadList.Select(p => $"\"{p}\"").ToList()) : string.Empty; | ||
} | ||
} | ||
[JsonIgnore] | ||
public string ScopesWrite | ||
{ | ||
get | ||
{ | ||
return ScopesWriteList.Any() ? string.Join(",", ScopesWriteList.Select(p => $"\"{p}\"").ToList()) : string.Empty; | ||
} | ||
} | ||
|
||
public List<Field> Fields { get; set; } | ||
} | ||
public List<Field> Fields { get; set; } | ||
} | ||
|
||
|
||
public class Field | ||
{ | ||
public string Name { get; set; } | ||
public string Type { get; set; } | ||
public bool Nullable { get; set; } | ||
} | ||
public class Field | ||
{ | ||
public string Name { get; set; } | ||
public string Type { get; set; } | ||
public bool Nullable { get; set; } | ||
public string MaxLength { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// TCDev.de 2022/04/10 | ||
// TCDev.APIGenerator.ApiAttribute.cs | ||
// https://github.com/DeeJayTC/net-dynamic-api | ||
|
||
using System; | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
|
||
namespace TCDev.ApiGenerator.Attributes | ||
{ | ||
[AttributeUsage(AttributeTargets.Class)] | ||
public class ApiAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// Attribute defining auto generated controller for the class | ||
/// </summary> | ||
/// <param name="route">The full base route for the class ie /myclass/ </param> | ||
/// <param name="requiredReadScopes"></param> | ||
/// <param name="requiredWriteScopes"></param> | ||
/// <param name="fireEvents"></param> | ||
/// <param name="authorize"></param> | ||
/// <param name="cache"></param> | ||
/// <param name="cacheDuration"></param> | ||
/// <param name="methods">The methods to generate for this endpoint</param> | ||
public ApiAttribute( | ||
string route, | ||
ApiMethodsToGenerate methods = ApiMethodsToGenerate.All, | ||
string[] requiredReadScopes = null, | ||
string[] requiredWriteScopes = null, | ||
bool fireEvents = false, | ||
bool authorize = true, | ||
bool cache = false, | ||
int cacheDuration = 50000) | ||
{ | ||
this.Route = route; | ||
this.Options = new ApiAttributeAttributeOptions | ||
{ | ||
RequiredReadScopes = requiredReadScopes, | ||
RequiredWriteScopes = requiredWriteScopes, | ||
Authorize = authorize, | ||
Cache = cache, | ||
CacheDuration = cacheDuration, | ||
FireEvents = fireEvents, | ||
Methods = methods | ||
}; | ||
} | ||
|
||
public string Route { get; set; } | ||
public ApiAttributeAttributeOptions Options { get; set; } | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.