45 lines
3.9 KiB
C#
45 lines
3.9 KiB
C#
using System.Security.Claims;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using PlotLine.Services.NativeCompanion;
|
|
|
|
namespace PlotLine.Controllers;
|
|
|
|
[RequestSizeLimit(4096)]
|
|
[Microsoft.AspNetCore.RateLimiting.EnableRateLimiting("native-pair")]
|
|
public sealed class NativeCompanionAuthController(DesktopPairing pairing,NativeInstallationService? installations=null):Controller
|
|
{
|
|
[AllowAnonymous,HttpPost("api/native-companion/pair")]
|
|
public IActionResult Start([FromBody] PairStart request)
|
|
{
|
|
Response.Headers.CacheControl="no-store";
|
|
if(request==null||(request.Persistent&&request.ConnectionId==Guid.Empty))return BadRequest();
|
|
try{return Ok(new{code=pairing.Start(request.Challenge??"",request.Persistent,request.ConnectionId,request.DeviceName??"Windows Companion")});}
|
|
catch(ArgumentException){return BadRequest();}catch(InvalidOperationException){return StatusCode(429);}
|
|
}
|
|
[Authorize,HttpGet("native-companion/authorize")]
|
|
public IActionResult Approve(string code)
|
|
{Response.Headers.CacheControl="no-store";Response.Headers["Referrer-Policy"]="no-referrer";ViewData["PersistentInstallation"]=pairing.Persistent(code??"");return !string.IsNullOrEmpty(code)&&pairing.Exists(code)?View("Approve",code):BadRequest("This request expired. Start Connect again in Word.");}
|
|
[Authorize,HttpPost("native-companion/authorize"),ValidateAntiForgeryToken]
|
|
public IActionResult ApproveGrant(string code,bool retainImport=false)
|
|
{
|
|
if(string.IsNullOrEmpty(code))return BadRequest();
|
|
if(!int.TryParse(User.FindFirstValue(ClaimTypes.NameIdentifier),out var user)||!Guid.TryParse(User.FindFirstValue("PlotLine.SecurityStamp"),out var stamp))return Forbid();
|
|
Response.Headers.CacheControl="no-store";
|
|
var persistent=pairing.Persistent(code);return pairing.Approve(code,user,stamp,retainImport)?Content(persistent?"Companion installation approved. You may close this tab. Access renews automatically; sign out or revoke this installation to remove its approval.":"Word Companion connected. You may close this tab. This older Companion uses a four-hour grant; update it for automatic reconnection."):BadRequest("This request expired or was already approved.");
|
|
}
|
|
[AllowAnonymous,HttpPost("api/native-companion/token")]
|
|
public async Task<IActionResult> Token([FromBody] PairRedeem request)
|
|
{
|
|
Response.Headers.CacheControl="no-store";if(request==null)return BadRequest();var approval=pairing.RedeemApproval(request.Code??"",request.Verifier??"");if(approval==null)return Accepted(new{status="pending-or-expired"});
|
|
if(approval.Persistent){if(installations==null||approval.ConnectionId==Guid.Empty)return StatusCode(503);return Ok(await installations.Create(approval));}
|
|
return Ok(new{accessToken=pairing.Issue(new(approval.UserId,approval.Stamp,DateTimeOffset.UtcNow.AddHours(4),approval.RetainImport))});
|
|
}
|
|
[AllowAnonymous,HttpPost("api/native-companion/renew")]
|
|
public async Task<IActionResult> Renew([FromBody] NativeDeviceRenew request){Response.Headers.CacheControl="no-store";if(request==null)return BadRequest();if(installations==null)return StatusCode(503);try{var tokens=await installations.Renew(request);return tokens==null?Unauthorized():Ok(tokens);}catch(ArgumentException){return BadRequest();}}
|
|
[AllowAnonymous,HttpPost("api/native-companion/revoke")]
|
|
public async Task<IActionResult> Revoke([FromBody] NativeDeviceRenew request){Response.Headers.CacheControl="no-store";if(request==null)return BadRequest();if(installations==null)return StatusCode(503);try{await installations.RevokeSecret(request);return NoContent();}catch(ArgumentException){return BadRequest();}}
|
|
public sealed record PairStart(string Challenge,bool Persistent=false,Guid ConnectionId=default,string? DeviceName=null);
|
|
public sealed record PairRedeem(string Code,string Verifier);
|
|
}
|