mirror of
https://github.com/hubHarmony/Csharp-API-Template.git
synced 2024-11-17 21:40:31 +00:00
Added simple tests and role filters
This commit is contained in:
parent
3218b4ba6a
commit
130943a052
@ -10,12 +10,16 @@ using Microsoft.IdentityModel.Tokens;
|
|||||||
namespace Simple_API.Controllers
|
namespace Simple_API.Controllers
|
||||||
{
|
{
|
||||||
|
|
||||||
[Route("auth/")]
|
[Route("Auth/")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class Default(IConfiguration configuration) : ControllerBase
|
public class Default(IConfiguration configuration) : ControllerBase
|
||||||
{
|
{
|
||||||
|
public static class UserRoles
|
||||||
|
{
|
||||||
|
public const string User = "User";
|
||||||
|
public const string Admin = "Admin";
|
||||||
|
}
|
||||||
|
|
||||||
public class AuthPayload
|
public class AuthPayload
|
||||||
{
|
{
|
||||||
[DataType(DataType.EmailAddress)]
|
[DataType(DataType.EmailAddress)]
|
||||||
@ -32,13 +36,13 @@ namespace Simple_API.Controllers
|
|||||||
public string? Password { get; init; } = string.Empty;
|
public string? Password { get; init; } = string.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPut("register")]
|
[HttpPut("Register")]
|
||||||
public IActionResult Register([FromBody] AuthPayload authPayload)
|
public IActionResult Register([FromBody] AuthPayload authPayload)
|
||||||
{
|
{
|
||||||
return Ok();
|
return Ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("login")]
|
[HttpPost("Login")]
|
||||||
public IActionResult Login([FromBody] AuthPayload authPayload)
|
public IActionResult Login([FromBody] AuthPayload authPayload)
|
||||||
{
|
{
|
||||||
// Here, you would typically validate the user's credentials against a database.
|
// Here, you would typically validate the user's credentials against a database.
|
||||||
@ -47,7 +51,7 @@ namespace Simple_API.Controllers
|
|||||||
var claims = new[]
|
var claims = new[]
|
||||||
{
|
{
|
||||||
new Claim(ClaimTypes.Email, authPayload.Email),
|
new Claim(ClaimTypes.Email, authPayload.Email),
|
||||||
new Claim(ClaimTypes.Role, "Admin"),
|
new Claim(ClaimTypes.Role, UserRoles.User),
|
||||||
new Claim(ClaimTypes.GivenName, "Test_ID"),
|
new Claim(ClaimTypes.GivenName, "Test_ID"),
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -75,7 +79,7 @@ namespace Simple_API.Controllers
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Route("test/")]
|
[Route("Test/")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class Test : ControllerBase
|
public class Test : ControllerBase
|
||||||
{
|
{
|
||||||
@ -88,32 +92,53 @@ namespace Simple_API.Controllers
|
|||||||
private const string ProtocolOk = "Protocol tested successfully.";
|
private const string ProtocolOk = "Protocol tested successfully.";
|
||||||
|
|
||||||
// GET: test/get
|
// GET: test/get
|
||||||
[Authorize]
|
[HttpGet("Get")]
|
||||||
[HttpGet("get")]
|
|
||||||
public IActionResult TestGet()
|
public IActionResult TestGet()
|
||||||
{
|
{
|
||||||
return Ok($"GET: {ProtocolOk}");
|
return Ok($"GET: {ProtocolOk}");
|
||||||
}
|
}
|
||||||
|
|
||||||
// POST: test/post
|
// POST: test/post
|
||||||
[HttpPost("post")]
|
[HttpPost("Post")]
|
||||||
public IActionResult TestPost([FromBody] TestPayload testPayload)
|
public IActionResult TestPost([FromBody] TestPayload testPayload)
|
||||||
{
|
{
|
||||||
return Ok($"POST: {ProtocolOk} Received: {testPayload.Data}");
|
return Ok($"POST: {ProtocolOk} Received: {testPayload.Data}");
|
||||||
}
|
}
|
||||||
|
|
||||||
// PUT: test/put
|
// PUT: test/put
|
||||||
[HttpPut("put")]
|
[HttpPut("Put")]
|
||||||
public IActionResult TestPut([FromBody] TestPayload testPayload)
|
public IActionResult TestPut([FromBody] TestPayload testPayload)
|
||||||
{
|
{
|
||||||
return Ok($"PUT: {ProtocolOk} Updated: {testPayload.Data}");
|
return Ok($"PUT: {ProtocolOk} Updated: {testPayload.Data}");
|
||||||
}
|
}
|
||||||
|
|
||||||
// DELETE: test/delete
|
// DELETE: test/delete
|
||||||
[HttpDelete("delete")]
|
[Authorize]
|
||||||
|
[HttpDelete("Delete")]
|
||||||
public IActionResult TestDelete([FromBody] TestPayload testPayload)
|
public IActionResult TestDelete([FromBody] TestPayload testPayload)
|
||||||
{
|
{
|
||||||
return Ok($"DELETE: {ProtocolOk} Deleted: {testPayload.Data}");
|
return Ok($"DELETE: {ProtocolOk} Deleted: {testPayload.Data}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Authorize]
|
||||||
|
[HttpGet("Protected")]
|
||||||
|
public IActionResult Protected()
|
||||||
|
{
|
||||||
|
return Ok("Successfully executed secured request.");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Authorize(Roles = Default.UserRoles.User)]
|
||||||
|
[HttpGet("ProtectedUserOnly")]
|
||||||
|
public IActionResult ProtectedUserOnly()
|
||||||
|
{
|
||||||
|
return Ok("Successfully executed secured request. (User)");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Authorize(Roles = Default.UserRoles.Admin)]
|
||||||
|
[HttpGet("ProtectedAdminOnly")]
|
||||||
|
public IActionResult ProtectedAdminOnly()
|
||||||
|
{
|
||||||
|
return Ok("Successfully executed secured request. (Admin)");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -5,6 +5,6 @@ Accept: application/json
|
|||||||
|
|
||||||
###
|
###
|
||||||
|
|
||||||
GET {{Simple_API_HostAddress}}/test/get
|
PUT {{Simple_API_HostAddress}}/test/put
|
||||||
Authorization: Bearer 1
|
Authorization: Bearer
|
||||||
###
|
###
|
Loading…
Reference in New Issue
Block a user