Set API authentication using Auth0. API endpoints require Authorized JWT

This commit is contained in:
Ruidy Nemausat 2020-02-19 22:47:41 +01:00
parent e59676d04a
commit 83dcab1c15
14 changed files with 44 additions and 48 deletions

View file

@ -1,15 +1,15 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using TicketManager.Data; using TicketManager.Data;
using TicketManager.Models; using TicketManager.Models;
using Microsoft.AspNetCore.Authorization;
namespace TicketManager.Controllers namespace TicketManager.Controllers
{ {
[Authorize]
[Route("api/v1/[controller]")] [Route("api/v1/[controller]")]
[ApiController] [ApiController]
public class UsersController : ControllerBase public class UsersController : ControllerBase

View file

@ -1,8 +1,7 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using TicketManager.Data; using TicketManager.Data;
@ -10,6 +9,7 @@ using TicketManager.Models;
namespace TicketManager.Controllers namespace TicketManager.Controllers
{ {
[Authorize]
[Route("api/[controller]")] [Route("api/[controller]")]
[ApiController] [ApiController]
public class AssignmentsController : ControllerBase public class AssignmentsController : ControllerBase

View file

@ -1,8 +1,7 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using TicketManager.Data; using TicketManager.Data;
@ -10,6 +9,7 @@ using TicketManager.Models;
namespace TicketManager.Controllers namespace TicketManager.Controllers
{ {
[Authorize]
[Route("api/v1/[controller]")] [Route("api/v1/[controller]")]
[ApiController] [ApiController]
public class FilesController : ControllerBase public class FilesController : ControllerBase

View file

@ -1,8 +1,7 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using TicketManager.Data; using TicketManager.Data;
@ -10,6 +9,7 @@ using TicketManager.Models;
namespace TicketManager.Controllers namespace TicketManager.Controllers
{ {
[Authorize]
[Route("api/v1/[controller]")] [Route("api/v1/[controller]")]
[ApiController] [ApiController]
public class HistoriesController : ControllerBase public class HistoriesController : ControllerBase

View file

@ -1,8 +1,7 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using TicketManager.Data; using TicketManager.Data;
@ -10,6 +9,7 @@ using TicketManager.Models;
namespace TicketManager.Controllers namespace TicketManager.Controllers
{ {
[Authorize]
[Route("api/v1/[controller]")] [Route("api/v1/[controller]")]
[ApiController] [ApiController]
public class NotesController : ControllerBase public class NotesController : ControllerBase

View file

@ -1,5 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@ -8,6 +9,8 @@ using TicketManager.Models;
namespace TicketManager.Controllers namespace TicketManager.Controllers
{ {
// [Authorize(Roles = "Admin")]
[Authorize]
[Produces("application/json")] [Produces("application/json")]
[Route("api/v1/[controller]")] [Route("api/v1/[controller]")]
[ApiController] [ApiController]

View file

@ -1,8 +1,6 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using TicketManager.Data; using TicketManager.Data;
@ -10,6 +8,7 @@ using TicketManager.Models;
namespace TicketManager.Controllers namespace TicketManager.Controllers
{ {
[Authorize]
[Route("api/v1/[controller]")] [Route("api/v1/[controller]")]
[ApiController] [ApiController]
public class TicketsController : ControllerBase public class TicketsController : ControllerBase

View file

@ -41,7 +41,5 @@
- Ensure Tickets Edits belong to Project Edits - Ensure Tickets Edits belong to Project Edits
- Ensure Tickets Files belong to Project Files - Ensure Tickets Files belong to Project Files
- Async model methods ? - Async model methods ?
- setMembers & removeMembers from project api not working
- Write a query class to refactor code and optimize perf on get queries (AsNoTracking)
- repository + strategy to decouple controllers from DbContext. Easier testing
- update assignments automatically from context - update assignments automatically from context
- use PATCH instead of PUT

View file

@ -1,5 +0,0 @@
rm -r Migrations
rm app.db
dotnet ef migrations add Migration1
dotnet ef database update
dotnet run

View file

@ -1,6 +0,0 @@
rm Controllers/AppUsersController.cs
rm Controllers/TicketsController.cs
rm Controllers/ProjectsController.cs
dotnet aspnet-codegenerator controller -name AppUsersController -async -api -m AppUser -dc AppDbContext -outDir Controllers
dotnet aspnet-codegenerator controller -name TicketsController -async -api -m Ticket -dc AppDbContext -outDir Controllers
dotnet aspnet-codegenerator controller -name ProjectsController -async -api -m Project -dc AppDbContext -outDir Controllers

View file

@ -19,6 +19,7 @@ using TicketManager.Data;
using TicketManager.Models; using TicketManager.Models;
using Microsoft.AspNetCore.Mvc.NewtonsoftJson; using Microsoft.AspNetCore.Mvc.NewtonsoftJson;
using Newtonsoft.Json; using Newtonsoft.Json;
using Microsoft.AspNetCore.Authentication.JwtBearer;
[assembly: ApiController] [assembly: ApiController]
namespace TicketManager namespace TicketManager
@ -32,7 +33,6 @@ namespace TicketManager
public IConfiguration Configuration { get; } public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
services.AddDbContext<AppDbContext>(options => services.AddDbContext<AppDbContext>(options =>
@ -40,12 +40,26 @@ namespace TicketManager
services.AddScoped<IProjectRepository, ProjectRepository>(); services.AddScoped<IProjectRepository, ProjectRepository>();
services.AddScoped<IAppUserRepository, AppUserRepository>(); services.AddScoped<IAppUserRepository, AppUserRepository>();
services.AddScoped<ITicketRepository, TicketRepository>(); services.AddScoped<ITicketRepository, TicketRepository>();
services.AddControllers()
.AddNewtonsoftJson(options => services.AddAuthentication(options =>
{ {
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; // avoid cycle ref errors options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.Authority = "https://dev-fyjrvohx.auth0.com/";
options.Audience = "https://localhost:5001/api/V1/";
//options.Authority = $"https://{Configuration["Auth0:Domain"]}/";
//options.Audience = Configuration["Auth0:Audience"];
}); });
services.AddControllers()
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; // avoid cycle ref errors
}
);
services.AddSpaStaticFiles(configuration => services.AddSpaStaticFiles(configuration =>
{ {
configuration.RootPath = "client/build"; configuration.RootPath = "client/build";
@ -72,17 +86,12 @@ namespace TicketManager
services.AddSwaggerGenNewtonsoftSupport(); // explicit opt-in - needs to be placed after AddSwaggerGen() services.AddSwaggerGenNewtonsoftSupport(); // explicit opt-in - needs to be placed after AddSwaggerGen()
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider serviceProvider) public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider serviceProvider)
{ {
if (env.IsDevelopment()) if (env.IsDevelopment())
{ {
app.UseDeveloperExceptionPage(); app.UseDeveloperExceptionPage();
var repository = serviceProvider.GetRequiredService<IProjectRepository>(); // var repository = serviceProvider.GetRequiredService<IProjectRepository>();
// InitializeDatabaseAsync(repository).Wait()
} }
else else
{ {
@ -92,7 +101,6 @@ namespace TicketManager
app.UseHttpsRedirection(); app.UseHttpsRedirection();
app.UseDefaultFiles(); app.UseDefaultFiles();
app.UseSwagger(); app.UseSwagger();
app.UseSwaggerUI(c => app.UseSwaggerUI(c =>
@ -100,11 +108,9 @@ namespace TicketManager
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Ticket Manager API v1"); c.SwaggerEndpoint("/swagger/v1/swagger.json", "Ticket Manager API v1");
}); });
app.UseSpaStaticFiles(); app.UseSpaStaticFiles();
app.UseRouting(); app.UseRouting();
app.UseAuthentication();
app.UseAuthorization(); app.UseAuthorization();
app.UseEndpoints(endpoints => app.UseEndpoints(endpoints =>
@ -123,9 +129,4 @@ namespace TicketManager
}); });
} }
} }
} }

View file

@ -16,6 +16,9 @@
<ItemGroup> <ItemGroup>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="3.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.1" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.1" />
<PackageReference Include="Microsoft.AspNetCore.SpaServices" Version="3.1.1" /> <PackageReference Include="Microsoft.AspNetCore.SpaServices" Version="3.1.1" />
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="3.1.1" /> <PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="3.1.1" />

View file

@ -10,5 +10,9 @@
"AllowedHosts": "*", "AllowedHosts": "*",
"ConnectionStrings": { "ConnectionStrings": {
"Sqlite": "Data Source=app.db" "Sqlite": "Data Source=app.db"
},
"Auth0": {
"Domain": "https://dev-fyjrvohx.auth0.com/",
"Audience": "https://localhost:5001/api/V1/"
} }
} }

View file

@ -1 +0,0 @@
/// <reference types="react-scripts" />