65 lines
1.6 KiB
C#
65 lines
1.6 KiB
C#
using Core.Enumerations;
|
|
using DomainResults.Mvc;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using WeatherForecast.Models.Requests;
|
|
using WeatherForecast.Policies;
|
|
using WeatherForecast.Services;
|
|
|
|
namespace WeatherForecast.Controllers;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class AuthenticationController : ControllerBase {
|
|
|
|
private readonly IAuthorizationService _authorizationService;
|
|
private readonly IUserService _userService;
|
|
|
|
private readonly WebapiControllers _webapiController = WebapiControllers.Authentication;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="authenticationService"></param>
|
|
public AuthenticationController(
|
|
IAuthorizationService authorizationService,
|
|
IUserService authenticationService
|
|
|
|
) {
|
|
_authorizationService = authorizationService;
|
|
_userService = authenticationService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// By providing username and password user obtains jwt token
|
|
/// </summary>
|
|
/// <param name="requestData"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public IActionResult Post([FromBody] AuthenticationRequestModel requestData) {
|
|
var result = _userService.CreateToken(requestData);
|
|
return result.ToActionResult();
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public async Task<IActionResult> Get() {
|
|
if ((await _authorizationService.AuthorizeAsync(User, null, new CrudActionRequirement(_webapiController, CrudActions.Read))).Succeeded) {
|
|
|
|
return Ok();
|
|
}
|
|
else {
|
|
return Unauthorized();
|
|
}
|
|
|
|
}
|
|
}
|
|
|