diff --git a/Api/Controllers/ProductController.cs b/Api/Controllers/ProductController.cs index 628f617..8b49afe 100644 --- a/Api/Controllers/ProductController.cs +++ b/Api/Controllers/ProductController.cs @@ -1,10 +1,12 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Api.Models; +using Microsoft.AspNetCore.Authorization; namespace Api.Controllers { [ApiController] + [Authorize] [Route("api/[controller]")] public class ProductController : ControllerBase { diff --git a/Api/Program.cs b/Api/Program.cs index caf2cd8..4827860 100644 --- a/Api/Program.cs +++ b/Api/Program.cs @@ -15,6 +15,28 @@ namespace Api var builder = WebApplication.CreateBuilder(args); // Add services to the container. + builder.Services.AddAuthentication(options => + { + options.DefaultScheme = "Cookies"; + options.DefaultChallengeScheme = "oidc"; + }) + .AddCookie("Cookies") + .AddOpenIdConnect("oidc", options => + { + var pocketId = builder.Configuration.GetSection("Authentication:PocketId"); + options.Authority = pocketId["Authority"]; + options.ClientId = pocketId["ClientId"]; + options.ClientSecret = pocketId["ClientSecret"]; + options.CallbackPath = pocketId["CallbackPath"]; + options.ResponseType = "code"; + options.SaveTokens = true; + options.Scope.Clear(); + var scopes = pocketId["Scopes"] ?? "openid"; + foreach (var scope in scopes.Split(' ')) + { + options.Scope.Add(scope); + } + }); builder.Services.AddControllers(); // Add DbContext with SQL Server builder.Services.AddDbContext(options => @@ -34,6 +56,9 @@ namespace Api app.UseHttpsRedirection(); } + app.UseAuthentication(); + app.UseAuthorization(); + app.MapControllers(); app.Run(); diff --git a/Api/appsettings.Development.json b/Api/appsettings.Development.json index 55ad44f..d17ed84 100644 --- a/Api/appsettings.Development.json +++ b/Api/appsettings.Development.json @@ -8,4 +8,14 @@ "ConnectionStrings": { "DefaultConnection": "Server=localhost,1433;Database=CentrumDb;User=sa;Password=P@ssw0rd;TrustServerCertificate=True;" } + , + "Authentication": { + "PocketId": { + "Authority": "https://identity.lesko.me", + "ClientId": "21131567-fea1-42a2-8907-21abd874eff8", + "ClientSecret": "a633GE6G3JoY8WopnsxhSXQpmsTuXa63", + "CallbackPath": "/signin-pocketid", + "Scopes": "openid profile email" + } + } }