Added authentication
This commit is contained in:
@@ -2,6 +2,9 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SampleApi.Models;
|
||||
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/license for full license details.
|
||||
|
||||
namespace SampleApi.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SampleApi.Models;
|
||||
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/license for full license details.
|
||||
|
||||
namespace SampleApi.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
@@ -35,6 +38,7 @@ namespace SampleApi.Controllers
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[DoxaApiAuth("bearer")]
|
||||
[DoxaApiExample("""
|
||||
{
|
||||
"name": "Grace Hopper",
|
||||
@@ -59,6 +63,7 @@ namespace SampleApi.Controllers
|
||||
}
|
||||
|
||||
[HttpPatch("{id}")]
|
||||
[DoxaApiAuth("bearer")]
|
||||
public ActionResult<User> Update(Guid id, [FromBody] UpdateUserRequest request)
|
||||
{
|
||||
var user = _users.FirstOrDefault(u => u.Id == id);
|
||||
@@ -86,6 +91,7 @@ namespace SampleApi.Controllers
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[DoxaApiAuth("apiKey")]
|
||||
[Obsolete("Use POST /api/users/{id}/archive instead.")]
|
||||
public IActionResult Delete(Guid id)
|
||||
{
|
||||
@@ -99,4 +105,4 @@ namespace SampleApi.Controllers
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace SampleApi.Models
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/license for full license details.
|
||||
|
||||
public class Address
|
||||
{
|
||||
public string Street { get; set; } = "";
|
||||
public string City { get; set; } = "";
|
||||
public string PostalCode { get; set; } = "";
|
||||
public string Country { get; set; } = "";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace SampleApi.Models
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/license for full license details.
|
||||
|
||||
public class CreateOrderRequest
|
||||
{
|
||||
public Guid UserId { get; set; }
|
||||
public List<OrderLine> Lines { get; set; } = new();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace SampleApi.Models
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/license for full license details.
|
||||
|
||||
public class CreateUserRequest
|
||||
{
|
||||
public string Name { get; set; } = "";
|
||||
|
||||
public string Email { get; set; } = "";
|
||||
|
||||
public UserRole Role { get; set; } = UserRole.Member;
|
||||
|
||||
public Address? Address { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
namespace SampleApi.Models
|
||||
{
|
||||
public enum UserRole
|
||||
{
|
||||
Admin,
|
||||
Member,
|
||||
Guest
|
||||
}
|
||||
|
||||
public class User
|
||||
{
|
||||
|
||||
public Guid Id { get; set; }
|
||||
|
||||
public string Name { get; set; } = "";
|
||||
|
||||
public string Email { get; set; } = "";
|
||||
|
||||
public UserRole Role { get; set; }
|
||||
|
||||
public DateTime CreatedAt { get; set; }
|
||||
|
||||
public Address? Address { get; set; }
|
||||
|
||||
public List<string> Tags { get; set; } = new();
|
||||
}
|
||||
|
||||
public class Address
|
||||
{
|
||||
public string Street { get; set; } = "";
|
||||
public string City { get; set; } = "";
|
||||
public string PostalCode { get; set; } = "";
|
||||
public string Country { get; set; } = "";
|
||||
}
|
||||
|
||||
public class CreateUserRequest
|
||||
{
|
||||
|
||||
public string Name { get; set; } = "";
|
||||
|
||||
public string Email { get; set; } = "";
|
||||
|
||||
public UserRole Role { get; set; } = UserRole.Member;
|
||||
|
||||
public Address? Address { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateUserRequest
|
||||
{
|
||||
public string? Name { get; set; }
|
||||
public string? Email { get; set; }
|
||||
public UserRole? Role { get; set; }
|
||||
}
|
||||
|
||||
public class Order
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public Guid UserId { get; set; }
|
||||
public List<OrderLine> Lines { get; set; } = new();
|
||||
public decimal Total { get; set; }
|
||||
public OrderStatus Status { get; set; }
|
||||
public DateTime PlacedAt { get; set; }
|
||||
}
|
||||
|
||||
public class OrderLine
|
||||
{
|
||||
public string Sku { get; set; } = "";
|
||||
public int Quantity { get; set; }
|
||||
public decimal UnitPrice { get; set; }
|
||||
}
|
||||
|
||||
public enum OrderStatus
|
||||
{
|
||||
Pending,
|
||||
Paid,
|
||||
Shipped,
|
||||
Delivered,
|
||||
Cancelled
|
||||
}
|
||||
|
||||
public class CreateOrderRequest
|
||||
{
|
||||
public Guid UserId { get; set; }
|
||||
public List<OrderLine> Lines { get; set; } = new();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
namespace SampleApi.Models
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/license for full license details.
|
||||
|
||||
public class Order
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public Guid UserId { get; set; }
|
||||
public List<OrderLine> Lines { get; set; } = new();
|
||||
public decimal Total { get; set; }
|
||||
public OrderStatus Status { get; set; }
|
||||
public DateTime PlacedAt { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace SampleApi.Models
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/license for full license details.
|
||||
|
||||
public class OrderLine
|
||||
{
|
||||
public string Sku { get; set; } = "";
|
||||
public int Quantity { get; set; }
|
||||
public decimal UnitPrice { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace SampleApi.Models
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/license for full license details.
|
||||
|
||||
public enum OrderStatus
|
||||
{
|
||||
Pending,
|
||||
Paid,
|
||||
Shipped,
|
||||
Delivered,
|
||||
Cancelled
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace SampleApi.Models
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/license for full license details.
|
||||
|
||||
public class UpdateUserRequest
|
||||
{
|
||||
public string? Name { get; set; }
|
||||
public string? Email { get; set; }
|
||||
public UserRole? Role { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
namespace SampleApi.Models
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/license for full license details.
|
||||
|
||||
public class User
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
|
||||
public string Name { get; set; } = "";
|
||||
|
||||
public string Email { get; set; } = "";
|
||||
|
||||
public UserRole Role { get; set; }
|
||||
|
||||
public DateTime CreatedAt { get; set; }
|
||||
|
||||
public Address? Address { get; set; }
|
||||
|
||||
public List<string> Tags { get; set; } = new();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace SampleApi.Models
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/license for full license details.
|
||||
|
||||
public enum UserRole
|
||||
{
|
||||
Admin,
|
||||
Member,
|
||||
Guest
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,9 @@ using EonaCat.DoxaApi.Middleware;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/license for full license details.
|
||||
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddDoxaApi(options =>
|
||||
{
|
||||
@@ -10,6 +13,12 @@ builder.Services.AddDoxaApi(options =>
|
||||
options.Description = "A demo service showing off the DoxaApi UI - users and orders.";
|
||||
options.Version = "v1";
|
||||
options.AccentColor = "#6366f1";
|
||||
|
||||
// Declare how clients authenticate; DoxaApi auto-detects which endpoints
|
||||
// require it via [Authorize] / [DoxaApiAuth] and renders an Authorize dialog.
|
||||
options.AddBearerAuth(description: "Paste a JWT issued by /api/auth/login.");
|
||||
options.AddApiKeyAuth("apiKey", "X-API-Key", description: "A static key for service-to-service calls.");
|
||||
options.DefaultSecurityScheme = "bearer";
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
@@ -23,4 +32,4 @@ app.UseDoxaApi(options =>
|
||||
|
||||
app.MapControllers();
|
||||
app.MapGet("/", () => Results.Redirect("/doxa"));
|
||||
app.Run();
|
||||
app.Run();
|
||||
Reference in New Issue
Block a user