2021-04-20 07:06:32 +02:00
using System.Threading.Tasks ;
2024-06-19 15:23:10 +02:00
using BTCPayServer.Abstractions.Constants ;
2021-04-20 07:06:32 +02:00
using BTCPayServer.Abstractions.Extensions ;
using BTCPayServer.Abstractions.Models ;
2024-06-19 15:23:10 +02:00
using BTCPayServer.Client ;
2021-04-20 07:06:32 +02:00
using BTCPayServer.Data ;
2021-04-28 06:14:15 +02:00
using BTCPayServer.Fido2.Models ;
2021-04-20 07:06:32 +02:00
using BTCPayServer.Models ;
using Fido2NetLib ;
using Microsoft.AspNetCore.Authorization ;
using Microsoft.AspNetCore.Identity ;
using Microsoft.AspNetCore.Mvc ;
2024-10-17 15:51:40 +02:00
using Microsoft.Extensions.Localization ;
2021-04-20 07:06:32 +02:00
using Newtonsoft.Json.Linq ;
2021-04-28 06:14:15 +02:00
namespace BTCPayServer.Fido2
2021-04-20 07:06:32 +02:00
{
[Route("fido2")]
2024-06-19 15:23:10 +02:00
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie, Policy = Policies.CanViewProfile)]
2022-01-14 10:34:30 +01:00
public class UIFido2Controller : Controller
2021-04-20 07:06:32 +02:00
{
private readonly UserManager < ApplicationUser > _userManager ;
private readonly Fido2Service _fido2Service ;
2024-10-17 15:51:40 +02:00
private IStringLocalizer StringLocalizer { get ; }
2021-04-20 07:06:32 +02:00
2024-10-17 15:51:40 +02:00
public UIFido2Controller (
UserManager < ApplicationUser > userManager ,
Fido2Service fido2Service ,
IStringLocalizer stringLocalizer )
2021-04-20 07:06:32 +02:00
{
_userManager = userManager ;
_fido2Service = fido2Service ;
2024-10-17 15:51:40 +02:00
StringLocalizer = stringLocalizer ;
2021-04-20 07:06:32 +02:00
}
[HttpGet("{id}/delete")]
public IActionResult Remove ( string id )
2021-12-31 08:59:02 +01:00
{
2024-10-17 15:51:40 +02:00
return View ( "Confirm" , new ConfirmModel ( StringLocalizer [ "Remove security device" ] , StringLocalizer [ "Your account will no longer have this security device as an option for two-factor authentication." ] , StringLocalizer [ "Remove" ] ) ) ;
2021-04-20 07:06:32 +02:00
}
[HttpPost("{id}/delete")]
public async Task < IActionResult > RemoveP ( string id )
{
await _fido2Service . Remove ( id , _userManager . GetUserId ( User ) ) ;
2021-09-13 03:16:52 +02:00
2021-04-20 07:06:32 +02:00
TempData . SetStatusMessageModel ( new StatusMessageModel
{
Severity = StatusMessageModel . StatusSeverity . Success ,
2024-10-17 15:51:40 +02:00
Html = StringLocalizer [ "The security device was removed successfully." ] . Value
2021-04-20 07:06:32 +02:00
} ) ;
2021-12-31 08:59:02 +01:00
2021-09-13 03:16:52 +02:00
return RedirectToList ( ) ;
2021-04-20 07:06:32 +02:00
}
[HttpGet("register")]
public async Task < IActionResult > Create ( AddFido2CredentialViewModel viewModel )
{
var options = await _fido2Service . RequestCreation ( _userManager . GetUserId ( User ) ) ;
if ( options is null )
{
TempData . SetStatusMessageModel ( new StatusMessageModel
{
Severity = StatusMessageModel . StatusSeverity . Error ,
2024-10-17 15:51:40 +02:00
Html = StringLocalizer [ "The security device could not be registered." ] . Value
2021-04-20 07:06:32 +02:00
} ) ;
2021-12-31 08:59:02 +01:00
2021-09-13 03:16:52 +02:00
return RedirectToList ( ) ;
2021-04-20 07:06:32 +02:00
}
ViewData [ "CredentialName" ] = viewModel . Name ? ? "" ;
return View ( options ) ;
}
[HttpPost("register")]
public async Task < IActionResult > CreateResponse ( [ FromForm ] string data , [ FromForm ] string name )
{
2021-04-28 06:14:15 +02:00
if ( await _fido2Service . CompleteCreation ( _userManager . GetUserId ( User ) , name , data ) )
2021-04-20 07:06:32 +02:00
{
TempData . SetStatusMessageModel ( new StatusMessageModel
{
Severity = StatusMessageModel . StatusSeverity . Success ,
2024-10-17 15:51:40 +02:00
Html = StringLocalizer [ "The security device was registered successfully." ] . Value
2021-04-20 07:06:32 +02:00
} ) ;
}
else
{
TempData . SetStatusMessageModel ( new StatusMessageModel
{
Severity = StatusMessageModel . StatusSeverity . Error ,
2024-10-17 15:51:40 +02:00
Html = StringLocalizer [ "The security device could not be registered." ] . Value
2021-04-20 07:06:32 +02:00
} ) ;
}
2021-09-13 03:16:52 +02:00
return RedirectToList ( ) ;
2021-04-20 07:06:32 +02:00
}
2021-09-13 03:16:52 +02:00
private ActionResult RedirectToList ( )
{
2022-01-07 04:32:00 +01:00
return RedirectToAction ( "TwoFactorAuthentication" , "UIManage" ) ;
2021-09-13 03:16:52 +02:00
}
2021-04-20 07:06:32 +02:00
}
}