2021-04-20 07:06:32 +02:00
using System.Threading.Tasks ;
using BTCPayServer.Abstractions.Extensions ;
using BTCPayServer.Abstractions.Models ;
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 ;
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")]
[Authorize]
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 ;
2022-01-14 10:34:30 +01:00
public UIFido2Controller ( UserManager < ApplicationUser > userManager , Fido2Service fido2Service )
2021-04-20 07:06:32 +02:00
{
_userManager = userManager ;
_fido2Service = fido2Service ;
}
[HttpGet("{id}/delete")]
public IActionResult Remove ( string id )
2021-12-31 08:59:02 +01:00
{
2021-09-13 03:16:52 +02:00
return View ( "Confirm" , new ConfirmModel ( "Remove security device" , "Your account will no longer have this security device as an option for two-factor authentication." , "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 ,
2021-09-13 03:16:52 +02:00
Html = "The security device was removed successfully."
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 ,
2021-09-13 03:16:52 +02:00
Html = "The security device could not be registered."
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 ,
2021-09-13 03:16:52 +02:00
Html = "The security device was registered successfully."
2021-04-20 07:06:32 +02:00
} ) ;
}
else
{
TempData . SetStatusMessageModel ( new StatusMessageModel
{
Severity = StatusMessageModel . StatusSeverity . Error ,
2021-09-13 03:16:52 +02:00
Html = "The security device could not be registered."
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
}
}