2023-05-09 12:39:33 -04:00
|
|
|
|
using Bit.Api.Auth.Models.Response;
|
2022-09-26 13:21:13 -04:00
|
|
|
|
using Bit.Api.Models.Response;
|
2023-08-17 16:03:06 -04:00
|
|
|
|
using Bit.Core.Auth.Enums;
|
2023-05-09 12:39:33 -04:00
|
|
|
|
using Bit.Core.Auth.Models.Api.Request.AuthRequest;
|
|
|
|
|
|
using Bit.Core.Auth.Services;
|
2022-09-26 13:21:13 -04:00
|
|
|
|
using Bit.Core.Exceptions;
|
|
|
|
|
|
using Bit.Core.Repositories;
|
|
|
|
|
|
using Bit.Core.Services;
|
|
|
|
|
|
using Bit.Core.Settings;
|
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
2023-04-14 13:25:56 -04:00
|
|
|
|
namespace Bit.Api.Auth.Controllers;
|
2022-09-26 13:21:13 -04:00
|
|
|
|
|
|
|
|
|
|
[Route("auth-requests")]
|
|
|
|
|
|
[Authorize("Application")]
|
|
|
|
|
|
public class AuthRequestsController : Controller
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IUserService _userService;
|
|
|
|
|
|
private readonly IAuthRequestRepository _authRequestRepository;
|
|
|
|
|
|
private readonly IGlobalSettings _globalSettings;
|
2023-05-09 12:39:33 -04:00
|
|
|
|
private readonly IAuthRequestService _authRequestService;
|
2022-09-26 13:21:13 -04:00
|
|
|
|
|
|
|
|
|
|
public AuthRequestsController(
|
|
|
|
|
|
IUserService userService,
|
|
|
|
|
|
IAuthRequestRepository authRequestRepository,
|
2023-05-09 12:39:33 -04:00
|
|
|
|
IGlobalSettings globalSettings,
|
|
|
|
|
|
IAuthRequestService authRequestService)
|
2022-09-26 13:21:13 -04:00
|
|
|
|
{
|
|
|
|
|
|
_userService = userService;
|
|
|
|
|
|
_authRequestRepository = authRequestRepository;
|
|
|
|
|
|
_globalSettings = globalSettings;
|
2023-05-09 12:39:33 -04:00
|
|
|
|
_authRequestService = authRequestService;
|
2022-09-26 13:21:13 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet("")]
|
|
|
|
|
|
public async Task<ListResponseModel<AuthRequestResponseModel>> Get()
|
|
|
|
|
|
{
|
|
|
|
|
|
var userId = _userService.GetProperUserId(User).Value;
|
|
|
|
|
|
var authRequests = await _authRequestRepository.GetManyByUserIdAsync(userId);
|
2022-10-04 11:06:01 -04:00
|
|
|
|
var responses = authRequests.Select(a => new AuthRequestResponseModel(a, _globalSettings.BaseServiceUri.Vault)).ToList();
|
2022-09-26 13:21:13 -04:00
|
|
|
|
return new ListResponseModel<AuthRequestResponseModel>(responses);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet("{id}")]
|
2023-05-09 12:39:33 -04:00
|
|
|
|
public async Task<AuthRequestResponseModel> Get(Guid id)
|
2022-09-26 13:21:13 -04:00
|
|
|
|
{
|
|
|
|
|
|
var userId = _userService.GetProperUserId(User).Value;
|
2023-05-09 12:39:33 -04:00
|
|
|
|
var authRequest = await _authRequestService.GetAuthRequestAsync(id, userId);
|
|
|
|
|
|
|
|
|
|
|
|
if (authRequest == null)
|
2022-09-26 13:21:13 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-04 11:06:01 -04:00
|
|
|
|
return new AuthRequestResponseModel(authRequest, _globalSettings.BaseServiceUri.Vault);
|
2022-09-26 13:21:13 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet("{id}/response")]
|
|
|
|
|
|
[AllowAnonymous]
|
2023-05-09 12:39:33 -04:00
|
|
|
|
public async Task<AuthRequestResponseModel> GetResponse(Guid id, [FromQuery] string code)
|
2022-09-26 13:21:13 -04:00
|
|
|
|
{
|
2023-05-09 12:39:33 -04:00
|
|
|
|
var authRequest = await _authRequestService.GetValidatedAuthRequestAsync(id, code);
|
|
|
|
|
|
|
|
|
|
|
|
if (authRequest == null)
|
2022-09-26 13:21:13 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-04 11:06:01 -04:00
|
|
|
|
return new AuthRequestResponseModel(authRequest, _globalSettings.BaseServiceUri.Vault);
|
2022-09-26 13:21:13 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost("")]
|
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
|
public async Task<AuthRequestResponseModel> Post([FromBody] AuthRequestCreateRequestModel model)
|
2023-08-17 16:03:06 -04:00
|
|
|
|
{
|
|
|
|
|
|
if (model.Type == AuthRequestType.AdminApproval)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("You must be authenticated to create a request of that type.");
|
|
|
|
|
|
}
|
|
|
|
|
|
var authRequest = await _authRequestService.CreateAuthRequestAsync(model);
|
|
|
|
|
|
var r = new AuthRequestResponseModel(authRequest, _globalSettings.BaseServiceUri.Vault);
|
|
|
|
|
|
return r;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost("admin-request")]
|
|
|
|
|
|
public async Task<AuthRequestResponseModel> PostAdminRequest([FromBody] AuthRequestCreateRequestModel model)
|
2022-09-26 13:21:13 -04:00
|
|
|
|
{
|
2023-05-09 12:39:33 -04:00
|
|
|
|
var authRequest = await _authRequestService.CreateAuthRequestAsync(model);
|
2022-10-04 11:06:01 -04:00
|
|
|
|
var r = new AuthRequestResponseModel(authRequest, _globalSettings.BaseServiceUri.Vault);
|
2022-10-03 11:37:37 -04:00
|
|
|
|
return r;
|
2022-09-26 13:21:13 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPut("{id}")]
|
2023-05-09 12:39:33 -04:00
|
|
|
|
public async Task<AuthRequestResponseModel> Put(Guid id, [FromBody] AuthRequestUpdateRequestModel model)
|
2022-09-26 13:21:13 -04:00
|
|
|
|
{
|
|
|
|
|
|
var userId = _userService.GetProperUserId(User).Value;
|
2023-05-09 12:39:33 -04:00
|
|
|
|
var authRequest = await _authRequestService.UpdateAuthRequestAsync(id, userId, model);
|
2022-10-04 11:06:01 -04:00
|
|
|
|
return new AuthRequestResponseModel(authRequest, _globalSettings.BaseServiceUri.Vault);
|
2022-09-26 13:21:13 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|