Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions Server/API/GoogleDriveCallbackController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Remotely.Server.Services;
using Remotely.Shared.Entities;
using System.Threading.Tasks;

namespace Remotely.Server.API;

[Route("api/[controller]")]
[ApiController]
[Authorize]
public class GoogleDriveCallbackController : ControllerBase
{
private readonly IGoogleDriveService _googleDriveService;
private readonly UserManager<RemotelyUser> _userManager;

public GoogleDriveCallbackController(
IGoogleDriveService googleDriveService,
UserManager<RemotelyUser> userManager)
{
_googleDriveService = googleDriveService;
_userManager = userManager;
}

[HttpGet]
public async Task<IActionResult> Callback([FromQuery] string code, [FromQuery] string state)
{
if (string.IsNullOrEmpty(code))
{
return BadRequest("Authorization code is missing.");
}

if (string.IsNullOrEmpty(state))
{
return BadRequest("State parameter is missing.");
}

var user = await _userManager.GetUserAsync(User);
if (user is null)
{
return Unauthorized();
}

// Validate the OAuth state token to prevent CSRF attacks.
var validatedUserId = _googleDriveService.ValidateOAuthState(state);
if (validatedUserId is null || validatedUserId != user.Id)
{
return BadRequest("Invalid or expired OAuth state token.");
}

var redirectUri = $"{Request.Scheme}://{Request.Host}/api/googledrivecallback";

await _googleDriveService.ExchangeCodeForTokenAsync(user.Id, code, redirectUri);

return LocalRedirect("/backup");
}
}
5 changes: 5 additions & 0 deletions Server/Components/Layout/NavMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@
<span class="oi oi-pencil" aria-hidden="true"></span> User Options
</NavLink>
</li>
<li class="nav-item px-3">
<NavLink class="nav-link" href="backup">
<span class="oi oi-hard-drive" aria-hidden="true"></span> Backup
</NavLink>
</li>

</Authorized>
</AuthorizeView>
Expand Down
Loading
Loading