This repository has been archived on 2023-02-13. You can view files and clone it, but cannot push or open issues or pull requests.
gswi-server/gswi/Controllers/AuthController.cs

129 lines
4.2 KiB
C#
Raw Normal View History

2022-02-20 19:04:11 +01:00
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using gswi.Interface.Services;
using gswi.Model.DTOs;
using gswi.Share.Common;
2022-02-21 18:27:33 +01:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
2022-02-20 19:04:11 +01:00
2022-02-21 18:27:33 +01:00
namespace gswi.Controllers {
2022-02-20 19:04:11 +01:00
[Route("api/auth")]
[ApiController]
2022-02-21 18:27:33 +01:00
public class AuthController : ControllerBase {
2022-02-20 19:04:11 +01:00
private readonly IAuthService _authService;
public AuthController(
IAuthService authServce
2022-02-21 18:27:33 +01:00
) {
2022-02-20 19:04:11 +01:00
_authService = authServce;
}
/* Data requests */
// Get /api/auth/users
[HttpGet("users")]
[Authorize]
2022-02-21 18:27:33 +01:00
public async Task<List<AuthUserDTO>> GetAllAuthUsers() {
2022-02-20 19:04:11 +01:00
return await _authService.GetAllAuthUsersAsync();
}
2022-02-21 18:27:33 +01:00
2022-02-20 19:04:11 +01:00
// POST /api/auth/users/get/filtered
[HttpPost("users/get/filtered")]
[Authorize]
2022-02-21 18:27:33 +01:00
public async Task<GetFilteredAuthUsersResultDTO> GetFilteredAuthUsers(AuthUserSelectCriterion selectCriterion) {
2022-02-20 19:04:11 +01:00
return await _authService.GetFilteredAuthUsersAsync(selectCriterion);
}
// Get /api/auth/users/get/<mail>
[HttpGet("users/get/{email}")]
[Authorize]
2022-02-21 18:27:33 +01:00
public async Task<AuthUserDTO> GetUserFromEMail(string email) {
2022-02-20 19:04:11 +01:00
return await _authService.GetAuthUserByEMailAsync(email);
}
// Get /api/auth/users/find/<mail>
[HttpGet("users/find/{email}")]
[Authorize]
2022-02-21 18:27:33 +01:00
public async Task<AuthUserDTO> FindUserFromEMail(string email) {
2022-02-20 19:04:11 +01:00
return await _authService.FindAuthUserByEMailAsync(email);
}
/* Auth requests */
// POST /api/auth/register
[HttpPost("register")]
2022-02-21 18:27:33 +01:00
public async Task Register(AuthUserDTO userDTO) {
2022-02-20 19:04:11 +01:00
await _authService.AddAuthUserAsync(userDTO);
}
// POST /api/auth/register/<id>
[HttpPost("register/{id}")]
2022-02-21 18:27:33 +01:00
public async Task<bool> ConfirmEMail(string id) {
2022-02-20 19:04:11 +01:00
return await _authService.ConfirmEMail(id);
}
// POST /api/auth/login
[HttpPost("login")]
2022-02-21 18:27:33 +01:00
public async Task<TokenDTO> Login(AuthUserDTO userDTO) {
2022-02-20 19:04:11 +01:00
return await _authService.Login(userDTO);
}
// POST /api/auth/forgot-password
[HttpPost("forgot-password")]
2022-02-21 18:27:33 +01:00
public async Task ForgotPassword([FromBody] string email) {
2022-02-20 19:04:11 +01:00
await _authService.ForgotPassword(email);
}
// POST /api/auth/confirm-forgot-password
[HttpPost("confirm-forgot-password")]
2022-02-21 18:27:33 +01:00
public async Task<EMailStringDTO> ConfirmForgotPassword([FromBody] string id) {
2022-02-20 19:04:11 +01:00
return await _authService.ConfirmForgotPassword(id);
}
// POST /api/auth/reset-password
[HttpPost("reset-password")]
2022-02-21 18:27:33 +01:00
public async Task ResetPassword(ResetPasswordDTO rpDTO) {
2022-02-20 19:04:11 +01:00
await _authService.ResetPassword(rpDTO);
}
// POST /api/auth/update-user
[HttpPost("update-user")]
2022-02-21 18:27:33 +01:00
public async Task UpdateUser(UpdateUserDTO updateUserDTO) {
2022-02-20 19:04:11 +01:00
await _authService.UpdateUser(updateUserDTO);
}
// POST /api/auth/update-user-as-admin
[HttpPost("update-user-as-admin")]
[Authorize]
2022-02-21 18:27:33 +01:00
public async Task UpdateUserAsAdmin(AdminUpdateUserDTO updateUserDTO) {
2022-02-20 19:04:11 +01:00
await _authService.UpdateUserAsAdmin(updateUserDTO);
}
// POST /api/auth/refresh
[HttpPost("refresh")]
2022-02-21 18:27:33 +01:00
public async Task<TokenDTO> Refresh(TokenDTO tokenDTO) {
2022-02-20 19:04:11 +01:00
return await _authService.Refresh(tokenDTO);
}
// POST /api/auth/revoke
[HttpPost("revoke")]
2022-02-21 18:27:33 +01:00
public async Task Revoke(TokenDTO tokenDTO) {
2022-02-20 19:04:11 +01:00
await _authService.Revoke(tokenDTO);
}
// POST /api/auth/delete-user
[HttpPost("delete-user")]
2022-02-21 18:27:33 +01:00
public async Task DeleteAuthUserAsync(AuthUserDTO userDTO) {
2022-02-20 19:04:11 +01:00
await _authService.DeleteAuthUserAsync(userDTO);
}
// POST /api/auth/delete-user
[HttpPost("delete-user-by-mail/{mail}")]
2022-02-21 18:27:33 +01:00
public async Task DeleteAuthUserByEMailAsync(string mail) {
2022-02-20 19:04:11 +01:00
await _authService.DeleteAuthUserByEMailAsync(mail);
}
}
}