2026-10-01 08:05:56 +02:00
|
|
|
using Microsoft.AspNetCore.Authentication;
|
2023-02-18 08:52:17 +01:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2026-10-01 11:02:21 +02:00
|
|
|
using Microsoft.AspNetCore.RateLimiting;
|
2023-02-18 08:52:17 +01:00
|
|
|
using Microsoft.Extensions.Localization;
|
2026-10-01 08:05:56 +02:00
|
|
|
using Microsoft.Extensions.Options;
|
2023-02-18 08:52:17 +01:00
|
|
|
|
2026-10-16 23:21:41 +02:00
|
|
|
using MongoDB.Entities;
|
|
|
|
|
|
2023-02-19 00:43:43 +01:00
|
|
|
using PrivaPub.ClientModels;
|
|
|
|
|
using PrivaPub.ClientModels.Resources;
|
2026-10-01 08:05:56 +02:00
|
|
|
using PrivaPub.ClientModels.Group;
|
2023-02-19 00:43:43 +01:00
|
|
|
using PrivaPub.ClientModels.User;
|
2026-10-01 08:05:56 +02:00
|
|
|
using PrivaPub.ClientModels.User.Avatar;
|
2023-02-19 00:43:43 +01:00
|
|
|
using PrivaPub.Extensions;
|
2026-10-01 10:58:39 +02:00
|
|
|
using PrivaPub.Federation.Actors;
|
2026-10-01 11:02:21 +02:00
|
|
|
using PrivaPub.Infrastructure;
|
2026-10-01 08:05:56 +02:00
|
|
|
using PrivaPub.Models;
|
2023-02-19 00:43:43 +01:00
|
|
|
using PrivaPub.Models.User;
|
|
|
|
|
using PrivaPub.Resources;
|
|
|
|
|
using PrivaPub.Services;
|
2026-10-01 08:05:56 +02:00
|
|
|
using PrivaPub.Services.ClientToServer.Private;
|
2023-02-19 00:43:43 +01:00
|
|
|
using PrivaPub.StaticServices;
|
2023-02-18 08:52:17 +01:00
|
|
|
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
|
2023-02-19 00:43:43 +01:00
|
|
|
namespace PrivaPub.Controllers.ClientToServer
|
2023-02-18 08:52:17 +01:00
|
|
|
{
|
|
|
|
|
[ApiController,
|
|
|
|
|
Route("clientapi/user")]
|
2026-11-10 01:24:43 +01:00
|
|
|
public class RootUserController(
|
|
|
|
|
IRootUsersService usersService,
|
|
|
|
|
IGroupUsersService groupUsersService,
|
|
|
|
|
IPrivateAvatarUsersService avatarUsersService,
|
|
|
|
|
ILocalActorService localActors,
|
|
|
|
|
AuthTokenManager authTokenManager,
|
|
|
|
|
IOptionsMonitor<AppConfiguration> appConfiguration,
|
|
|
|
|
IOptionsMonitor<RegistrationOptions> registrations,
|
|
|
|
|
IStringLocalizer<GenericRes> localizer,
|
|
|
|
|
ILogger<RootUserController> logger) : ControllerBase
|
2023-02-18 08:52:17 +01:00
|
|
|
{
|
|
|
|
|
#region User endpoints
|
|
|
|
|
|
2026-10-01 11:02:21 +02:00
|
|
|
[HttpPost, Route("/clientapi/user/signup"), EnableRateLimiting(RateLimiting.Accounts), Authorize(Policy = Policies.IsUser), AllowAnonymous]
|
2026-11-10 01:24:43 +01:00
|
|
|
public async Task<IActionResult> SignUp(LoginForm signUpForm, CancellationToken token = default)
|
2023-02-18 08:52:17 +01:00
|
|
|
{
|
|
|
|
|
if (User.Identity?.IsAuthenticated ?? false) return Redirect("/");
|
|
|
|
|
var result = new WebResult();
|
2026-11-10 01:24:43 +01:00
|
|
|
if (!registrations.CurrentValue.IsOpen)
|
|
|
|
|
return StatusCode(StatusCodes.Status403Forbidden, result.Invalidate(localizer[RegistrationOptions.ClosedMessage], StatusCodes.Status403Forbidden));
|
2023-02-18 08:52:17 +01:00
|
|
|
if (!ModelState.IsValid)
|
2026-11-10 01:24:43 +01:00
|
|
|
return BadRequest(result.Invalidate(localizer["Invalid model."]));
|
2023-02-18 08:52:17 +01:00
|
|
|
try
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
result = await usersService.SignUpAsync(signUpForm, token);
|
2023-02-18 08:52:17 +01:00
|
|
|
if (!result.IsValid)
|
|
|
|
|
return StatusCode(result.StatusCode, result);
|
|
|
|
|
|
2026-10-01 08:05:56 +02:00
|
|
|
(var user, var userSettings) = ((RootUser, ViewUserSettings))result.Data;
|
2026-11-10 01:24:43 +01:00
|
|
|
var jwtUser = authTokenManager.GenerateToken(user, userSettings);
|
2023-02-18 08:52:17 +01:00
|
|
|
return Ok(jwtUser);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
logger.LogError(ex, $"{nameof(User)}.{nameof(SignUp)}()");
|
|
|
|
|
return BadRequest(result.Invalidate(localizer["Something went wrong."]));
|
2023-02-18 08:52:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-01 11:02:21 +02:00
|
|
|
[HttpPost, Route("/clientapi/user/login"), EnableRateLimiting(RateLimiting.Accounts), Authorize(Policy = Policies.IsUser), AllowAnonymous]
|
2026-11-10 01:24:43 +01:00
|
|
|
public async Task<IActionResult> Login(LoginForm loginForm, CancellationToken token = default)
|
2023-02-18 08:52:17 +01:00
|
|
|
{
|
2026-10-17 02:56:57 +02:00
|
|
|
if (User.Identity?.IsAuthenticated ?? false) return Redirect("/");
|
2023-02-18 08:52:17 +01:00
|
|
|
var result = new WebResult();
|
|
|
|
|
if (!ModelState.IsValid)
|
2026-11-10 01:24:43 +01:00
|
|
|
return BadRequest(result.Invalidate(localizer["Invalid model."]));
|
2023-02-18 08:52:17 +01:00
|
|
|
try
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
result = await usersService.LoginAsync(loginForm, token);
|
2023-02-18 08:52:17 +01:00
|
|
|
if (!result.IsValid)
|
|
|
|
|
return StatusCode(result.StatusCode, result);
|
|
|
|
|
|
|
|
|
|
var (user, userSettings) =
|
2026-10-01 08:05:56 +02:00
|
|
|
((RootUser, ViewUserSettings))result.Data;
|
2026-11-10 01:24:43 +01:00
|
|
|
var jwtUser = authTokenManager.GenerateToken(user, userSettings);
|
2023-02-18 08:52:17 +01:00
|
|
|
|
|
|
|
|
return Ok(jwtUser);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
logger.LogError(ex, $"{nameof(User)}.{nameof(Login)}()");
|
|
|
|
|
return BadRequest(result.Invalidate(localizer["Something went wrong."]));
|
2023-02-18 08:52:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-01 11:02:21 +02:00
|
|
|
[HttpPost, Route("/clientapi/user/invitation/signup"), EnableRateLimiting(RateLimiting.Accounts), AllowAnonymous]
|
2026-10-01 08:05:56 +02:00
|
|
|
public async Task<IActionResult> InvitationSignUp(InvitationLoginForm signUpForm, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var result = new WebResult();
|
|
|
|
|
if (!ModelState.IsValid)
|
2026-11-10 01:24:43 +01:00
|
|
|
return BadRequest(result.Invalidate(localizer["Invalid model."]));
|
2026-10-01 08:05:56 +02:00
|
|
|
try
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
var invitation = await groupUsersService.GetInvitation(signUpForm.InvitationCode, signUpForm.InvitationPassword, token);
|
2026-10-01 08:05:56 +02:00
|
|
|
if (!invitation.IsValid)
|
|
|
|
|
return StatusCode(invitation.StatusCode, invitation);
|
2023-02-18 08:52:17 +01:00
|
|
|
|
2026-10-01 10:58:39 +02:00
|
|
|
var avatarUserName = signUpForm.AvatarUserName?.Trim().ToLowerInvariant();
|
|
|
|
|
var personaProblem = await PersonaProblem(avatarUserName, signUpForm.UserName, token);
|
|
|
|
|
if (personaProblem != default)
|
|
|
|
|
return BadRequest(result.Invalidate(personaProblem));
|
|
|
|
|
|
2026-11-10 01:24:43 +01:00
|
|
|
result = await usersService.SignUpAsync(signUpForm, token);
|
2026-10-01 08:05:56 +02:00
|
|
|
if (!result.IsValid)
|
|
|
|
|
return StatusCode(result.StatusCode, result);
|
|
|
|
|
var (user, userSettings) = ((RootUser, ViewUserSettings))result.Data;
|
|
|
|
|
|
2026-11-10 01:24:43 +01:00
|
|
|
var avatar = await InsertPersona(user.ID, avatarUserName, signUpForm.AvatarName, token);
|
2026-10-01 08:05:56 +02:00
|
|
|
if (!avatar.IsValid)
|
|
|
|
|
return StatusCode(avatar.StatusCode, avatar);
|
|
|
|
|
|
2026-11-10 01:24:43 +01:00
|
|
|
var joined = await groupUsersService.JoinGroup(user.ID, new JoinGroupForm
|
2026-10-01 08:05:56 +02:00
|
|
|
{
|
|
|
|
|
AvatarId = ((ViewAvatar)avatar.Data).Id,
|
|
|
|
|
InvitationCode = signUpForm.InvitationCode,
|
|
|
|
|
InvitationPassword = signUpForm.InvitationPassword
|
|
|
|
|
}, token);
|
|
|
|
|
if (!joined.IsValid)
|
|
|
|
|
return StatusCode(joined.StatusCode, joined);
|
2023-02-18 08:52:17 +01:00
|
|
|
|
2026-11-10 01:24:43 +01:00
|
|
|
return Ok(authTokenManager.GenerateToken(user, userSettings));
|
2026-10-01 08:05:56 +02:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
logger.LogError(ex, $"{nameof(User)}.{nameof(InvitationSignUp)}()");
|
|
|
|
|
return BadRequest(result.Invalidate(localizer["Something went wrong."]));
|
2026-10-01 08:05:56 +02:00
|
|
|
}
|
|
|
|
|
}
|
2023-02-18 08:52:17 +01:00
|
|
|
|
2026-10-01 11:02:21 +02:00
|
|
|
[HttpPost, Route("/clientapi/user/invitation/login"), EnableRateLimiting(RateLimiting.Accounts), AllowAnonymous]
|
2026-10-01 08:05:56 +02:00
|
|
|
public async Task<IActionResult> InvitationLogin(InvitationLoginForm loginForm, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var result = new WebResult();
|
|
|
|
|
if (!ModelState.IsValid)
|
2026-11-10 01:24:43 +01:00
|
|
|
return BadRequest(result.Invalidate(localizer["Invalid model."]));
|
2026-10-01 08:05:56 +02:00
|
|
|
try
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
var invitation = await groupUsersService.GetInvitation(loginForm.InvitationCode, loginForm.InvitationPassword, token);
|
2026-10-01 08:05:56 +02:00
|
|
|
if (!invitation.IsValid)
|
|
|
|
|
return StatusCode(invitation.StatusCode, invitation);
|
2023-02-18 08:52:17 +01:00
|
|
|
|
2026-11-10 01:24:43 +01:00
|
|
|
result = await usersService.LoginAsync(loginForm, token);
|
2026-10-01 08:05:56 +02:00
|
|
|
if (!result.IsValid)
|
|
|
|
|
return StatusCode(result.StatusCode, result);
|
|
|
|
|
var (user, userSettings) = ((RootUser, ViewUserSettings))result.Data;
|
|
|
|
|
|
2026-11-10 01:24:43 +01:00
|
|
|
var avatars = await avatarUsersService.GetRootAvatars(user.ID, token);
|
2026-10-01 10:58:39 +02:00
|
|
|
var avatarUserName = loginForm.AvatarUserName?.Trim().ToLowerInvariant();
|
|
|
|
|
var avatar = string.IsNullOrEmpty(avatarUserName)
|
|
|
|
|
? (avatars.Data as List<ViewAvatar>)?.FirstOrDefault()
|
|
|
|
|
: (avatars.Data as List<ViewAvatar>)?.FirstOrDefault(a => a.UserName == avatarUserName);
|
2026-10-01 08:05:56 +02:00
|
|
|
if (avatar == default)
|
|
|
|
|
{
|
2026-10-01 10:58:39 +02:00
|
|
|
var personaProblem = await PersonaProblem(avatarUserName, user.UserName, token);
|
|
|
|
|
if (personaProblem != default)
|
|
|
|
|
return BadRequest(new WebResult().Invalidate(personaProblem));
|
2026-11-10 01:24:43 +01:00
|
|
|
var inserted = await InsertPersona(user.ID, avatarUserName, loginForm.AvatarName, token);
|
2026-10-01 08:05:56 +02:00
|
|
|
if (!inserted.IsValid)
|
|
|
|
|
return StatusCode(inserted.StatusCode, inserted);
|
|
|
|
|
avatar = (ViewAvatar)inserted.Data;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-10 01:24:43 +01:00
|
|
|
var joined = await groupUsersService.JoinGroup(user.ID, new JoinGroupForm
|
2026-10-01 08:05:56 +02:00
|
|
|
{
|
|
|
|
|
AvatarId = avatar.Id,
|
|
|
|
|
InvitationCode = loginForm.InvitationCode,
|
|
|
|
|
InvitationPassword = loginForm.InvitationPassword
|
|
|
|
|
}, token);
|
|
|
|
|
if (!joined.IsValid)
|
|
|
|
|
return StatusCode(joined.StatusCode, joined);
|
2023-02-18 08:52:17 +01:00
|
|
|
|
2026-11-10 01:24:43 +01:00
|
|
|
return Ok(authTokenManager.GenerateToken(user, userSettings));
|
2026-10-01 08:05:56 +02:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
logger.LogError(ex, $"{nameof(User)}.{nameof(InvitationLogin)}()");
|
|
|
|
|
return BadRequest(result.Invalidate(localizer["Something went wrong."]));
|
2026-10-01 08:05:56 +02:00
|
|
|
}
|
|
|
|
|
}
|
2023-02-18 08:52:17 +01:00
|
|
|
|
2026-10-16 23:21:41 +02:00
|
|
|
[HttpGet, Route("/clientapi/user/setup"), AllowAnonymous]
|
|
|
|
|
public async Task<IActionResult> Setup(CancellationToken token) =>
|
|
|
|
|
Ok(new ViewServerSetup { IsPending = await ServerSetup.IsPending(token) });
|
|
|
|
|
|
|
|
|
|
// The server's first login, made while it has no administrator (owner decision 2026-10-16: first come, first served):
|
|
|
|
|
// it is made the administrator, with its first persona, and setup closes for good. Sign-up stays closed to everyone
|
2026-11-10 01:24:43 +01:00
|
|
|
// else (registrations), so NodeInfo and the instance API don't change.
|
2026-10-16 23:21:41 +02:00
|
|
|
[HttpPost, Route("/clientapi/user/setup"), EnableRateLimiting(RateLimiting.Accounts), AllowAnonymous]
|
|
|
|
|
public async Task<IActionResult> SetUp(SetupForm setupForm, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
if (User.Identity?.IsAuthenticated ?? false) return Redirect("/");
|
|
|
|
|
var result = new WebResult();
|
|
|
|
|
if (!ModelState.IsValid)
|
2026-11-10 01:24:43 +01:00
|
|
|
return BadRequest(result.Invalidate(localizer["Invalid model."]));
|
2026-10-16 23:21:41 +02:00
|
|
|
var claimed = false;
|
|
|
|
|
var adminId = default(string);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (!await ServerSetup.IsPending(token))
|
|
|
|
|
return SetUpAlready();
|
|
|
|
|
|
|
|
|
|
var avatarUserName = setupForm.AvatarUserName?.Trim().ToLowerInvariant();
|
|
|
|
|
var personaProblem = await PersonaProblem(avatarUserName, setupForm.UserName, token);
|
|
|
|
|
if (personaProblem != default)
|
|
|
|
|
return BadRequest(result.Invalidate(personaProblem));
|
|
|
|
|
|
|
|
|
|
claimed = await ServerSetup.Claim(token);
|
|
|
|
|
if (!claimed)
|
|
|
|
|
return SetUpAlready();
|
|
|
|
|
|
2026-11-10 01:24:43 +01:00
|
|
|
result = await usersService.SignUpAsync(setupForm, token);
|
2026-10-16 23:21:41 +02:00
|
|
|
if (!result.IsValid)
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
await ServerSetup.Release(CancellationToken.None);
|
2026-10-16 23:21:41 +02:00
|
|
|
return StatusCode(result.StatusCode, result);
|
|
|
|
|
}
|
|
|
|
|
var (user, userSettings) = ((RootUser, ViewUserSettings))result.Data;
|
|
|
|
|
user.SetAdmin(true);
|
|
|
|
|
await DB.Default.SaveAsync(user, CancellationToken.None);
|
|
|
|
|
adminId = user.ID;
|
|
|
|
|
await ServerSetup.Claimed(adminId, CancellationToken.None);
|
|
|
|
|
|
|
|
|
|
// set up from here, whatever happens to the persona: it can be made in the client's settings
|
2026-11-10 01:24:43 +01:00
|
|
|
var avatar = await InsertPersona(user.ID, avatarUserName, setupForm.AvatarName, token);
|
2026-10-16 23:21:41 +02:00
|
|
|
if (!avatar.IsValid)
|
|
|
|
|
return StatusCode(avatar.StatusCode, avatar);
|
|
|
|
|
|
2026-11-10 01:24:43 +01:00
|
|
|
return Ok(authTokenManager.GenerateToken(user, userSettings));
|
2026-10-16 23:21:41 +02:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
logger.LogError(ex, $"{nameof(User)}.{nameof(SetUp)}()");
|
2026-10-16 23:21:41 +02:00
|
|
|
if (claimed && adminId == default)
|
2026-11-10 01:24:43 +01:00
|
|
|
await ServerSetup.Release(CancellationToken.None);
|
|
|
|
|
return BadRequest(result.Invalidate(localizer["Something went wrong."]));
|
2026-10-16 23:21:41 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IActionResult SetUpAlready() =>
|
|
|
|
|
StatusCode(StatusCodes.Status403Forbidden,
|
2026-11-10 01:24:43 +01:00
|
|
|
new WebResult().Invalidate(localizer["This server is already set up: joining it takes a group invitation."], StatusCodes.Status403Forbidden));
|
2026-10-16 23:21:41 +02:00
|
|
|
|
2026-11-10 01:24:43 +01:00
|
|
|
Task<WebResult> InsertPersona(string rootId, string avatarUserName, string avatarName, CancellationToken token = default) =>
|
|
|
|
|
avatarUsersService.InsertAvatar(new InsertAvatarForm
|
2026-10-16 23:21:41 +02:00
|
|
|
{
|
|
|
|
|
RootId = rootId,
|
|
|
|
|
Name = string.IsNullOrWhiteSpace(avatarName) ? avatarUserName : avatarName.Trim(),
|
|
|
|
|
UserName = avatarUserName,
|
|
|
|
|
Biography = string.Empty
|
2026-11-10 01:24:43 +01:00
|
|
|
}, token);
|
2026-10-16 23:21:41 +02:00
|
|
|
|
2026-10-01 10:58:39 +02:00
|
|
|
async Task<string> PersonaProblem(string avatarUserName, string rootUserName, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(avatarUserName))
|
2026-11-10 01:24:43 +01:00
|
|
|
return localizer["Choose a username for your persona."];
|
2026-10-01 10:58:39 +02:00
|
|
|
if (string.Equals(avatarUserName, rootUserName?.Trim(), StringComparison.OrdinalIgnoreCase))
|
2026-11-10 01:24:43 +01:00
|
|
|
return localizer["Your persona's username must differ from your login."];
|
|
|
|
|
if (await localActors.IsUserNameTaken(avatarUserName, token))
|
|
|
|
|
return localizer["The username '{0}' is already taken.", avatarUserName];
|
2026-10-01 10:58:39 +02:00
|
|
|
return default;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-17 03:13:16 +02:00
|
|
|
// This device signs out: its session (every token it renewed) is refused from now on. A token from before sessions had
|
|
|
|
|
// ids can't be told apart from the others, so it signs every device out.
|
2023-02-18 08:52:17 +01:00
|
|
|
[HttpGet, Route("/clientapi/user/logout"), Authorize(Policy = Policies.IsUser)]
|
2026-10-17 03:13:16 +02:00
|
|
|
public async Task<IActionResult> Logout([FromServices] IRootSessions sessions, CancellationToken token)
|
2023-02-18 08:52:17 +01:00
|
|
|
{
|
|
|
|
|
var result = new WebResult();
|
|
|
|
|
try
|
|
|
|
|
{
|
2026-10-17 03:13:16 +02:00
|
|
|
var sessionId = User.FindFirst(AuthTokenManager.SessionId)?.Value;
|
|
|
|
|
if (string.IsNullOrEmpty(sessionId))
|
|
|
|
|
await sessions.Revoke(User.GetUserId(), token);
|
|
|
|
|
else
|
|
|
|
|
await sessions.End(User.GetUserId(), sessionId, token);
|
2023-02-18 08:52:17 +01:00
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
logger.LogError(ex, $"{nameof(User)}.{nameof(Logout)}()");
|
|
|
|
|
return BadRequest(result.Invalidate(localizer["Something went wrong."]));
|
2023-02-18 08:52:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-17 03:13:16 +02:00
|
|
|
[HttpPost, Route("/clientapi/user/logout/everywhere"), Authorize(Policy = Policies.IsUser)]
|
|
|
|
|
public async Task<IActionResult> LogoutEverywhere([FromServices] IRootSessions sessions, CancellationToken token)
|
2023-02-18 08:52:17 +01:00
|
|
|
{
|
|
|
|
|
var result = new WebResult();
|
|
|
|
|
try
|
|
|
|
|
{
|
2026-10-17 03:13:16 +02:00
|
|
|
await sessions.Revoke(User.GetUserId(), token);
|
2023-02-18 08:52:17 +01:00
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
logger.LogError(ex, $"{nameof(User)}.{nameof(LogoutEverywhere)}()");
|
|
|
|
|
return BadRequest(result.Invalidate(localizer["Something went wrong."]));
|
2023-02-18 08:52:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-17 03:13:16 +02:00
|
|
|
// The address is changed only with the password and a code sent to it (/clientapi/user/email); this older route, which
|
|
|
|
|
// took neither and said whether an address was taken, no longer changes anything.
|
|
|
|
|
[HttpPost, Route("/clientapi/user/update"), Authorize(Policy = Policies.IsUser)]
|
|
|
|
|
public IActionResult UpdateUser(UserForm userEmailForm) =>
|
|
|
|
|
StatusCode(StatusCodes.Status422UnprocessableEntity,
|
2026-11-10 01:24:43 +01:00
|
|
|
new WebResult().Invalidate(localizer["Changing the address takes your password and a code sent to it."], StatusCodes.Status422UnprocessableEntity));
|
2026-10-17 03:13:16 +02:00
|
|
|
|
|
|
|
|
[HttpGet, Route("/clientapi/user/account"), Authorize(Policy = Policies.IsUser)]
|
2026-11-10 01:24:43 +01:00
|
|
|
public async Task<IActionResult> Account(CancellationToken token = default)
|
2026-10-17 03:13:16 +02:00
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
var result = await usersService.AccountAsync(User.GetUserId(), token);
|
2026-10-17 03:13:16 +02:00
|
|
|
return result.IsValid ? Ok(result.Data) : StatusCode(result.StatusCode, result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost, Route("/clientapi/user/email"), EnableRateLimiting(RateLimiting.Accounts), Authorize(Policy = Policies.IsUser)]
|
2026-11-10 01:24:43 +01:00
|
|
|
public async Task<IActionResult> RequestEmailChange(EmailChangeForm form, CancellationToken token = default)
|
2026-10-17 03:13:16 +02:00
|
|
|
{
|
|
|
|
|
if (!ModelState.IsValid)
|
2026-11-10 01:24:43 +01:00
|
|
|
return BadRequest(new WebResult().Invalidate(localizer["Invalid model."]));
|
|
|
|
|
var result = await usersService.RequestEmailChangeAsync(User.GetUserId(), form, token);
|
2026-10-17 03:13:16 +02:00
|
|
|
return result.IsValid ? Ok(result.Data) : StatusCode(result.StatusCode, result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost, Route("/clientapi/user/email/verify"), EnableRateLimiting(RateLimiting.Accounts), Authorize(Policy = Policies.IsUser)]
|
2026-11-10 01:24:43 +01:00
|
|
|
public async Task<IActionResult> VerifyEmail(EmailCodeForm form, CancellationToken token = default)
|
2026-10-17 03:13:16 +02:00
|
|
|
{
|
|
|
|
|
if (!ModelState.IsValid)
|
2026-11-10 01:24:43 +01:00
|
|
|
return BadRequest(new WebResult().Invalidate(localizer["Invalid model."]));
|
|
|
|
|
var result = await usersService.VerifyEmailAsync(User.GetUserId(), form.Code, token);
|
2026-10-17 03:13:16 +02:00
|
|
|
return result.IsValid ? Ok(result.Data) : StatusCode(result.StatusCode, result);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-17 03:18:19 +02:00
|
|
|
[HttpPost, Route("/clientapi/user/totp/setup"), EnableRateLimiting(RateLimiting.Accounts), Authorize(Policy = Policies.IsUser)]
|
2026-11-10 01:24:43 +01:00
|
|
|
public async Task<IActionResult> TotpSetup(PasswordForm form, CancellationToken token = default)
|
2026-10-17 03:18:19 +02:00
|
|
|
{
|
|
|
|
|
if (!ModelState.IsValid)
|
2026-11-10 01:24:43 +01:00
|
|
|
return BadRequest(new WebResult().Invalidate(localizer["Invalid model."]));
|
|
|
|
|
var result = await usersService.TotpSetupAsync(User.GetUserId(), form.Password, token);
|
2026-10-17 03:18:19 +02:00
|
|
|
return result.IsValid ? Ok(result.Data) : StatusCode(result.StatusCode, result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// on with the app's first code; every other session ends, and this device goes on with a new one
|
|
|
|
|
[HttpPost, Route("/clientapi/user/totp/enable"), EnableRateLimiting(RateLimiting.Accounts), Authorize(Policy = Policies.IsUser)]
|
2026-11-10 01:24:43 +01:00
|
|
|
public async Task<IActionResult> TotpEnable(TotpCodeForm form, CancellationToken token = default)
|
2026-10-17 03:18:19 +02:00
|
|
|
{
|
|
|
|
|
if (!ModelState.IsValid)
|
2026-11-10 01:24:43 +01:00
|
|
|
return BadRequest(new WebResult().Invalidate(localizer["Invalid model."]));
|
|
|
|
|
var result = await usersService.TotpEnableAsync(User.GetUserId(), form.Code, token);
|
2026-10-17 03:18:19 +02:00
|
|
|
if (!result.IsValid)
|
|
|
|
|
return StatusCode(result.StatusCode, result);
|
|
|
|
|
var (user, userSettings, codes) = ((RootUser, ViewUserSettings, List<string>))result.Data;
|
2026-11-10 01:24:43 +01:00
|
|
|
return Ok(new ViewTotpEnabled { RecoveryCodes = codes, Session = authTokenManager.GenerateToken(user, userSettings) });
|
2026-10-17 03:18:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost, Route("/clientapi/user/totp/disable"), EnableRateLimiting(RateLimiting.Accounts), Authorize(Policy = Policies.IsUser)]
|
2026-11-10 01:24:43 +01:00
|
|
|
public async Task<IActionResult> TotpDisable(TotpDisableForm form, CancellationToken token = default)
|
2026-10-17 03:18:19 +02:00
|
|
|
{
|
|
|
|
|
if (!ModelState.IsValid)
|
2026-11-10 01:24:43 +01:00
|
|
|
return BadRequest(new WebResult().Invalidate(localizer["Invalid model."]));
|
|
|
|
|
var result = await usersService.TotpDisableAsync(User.GetUserId(), form.Password, form.Code, token);
|
2026-10-17 03:18:19 +02:00
|
|
|
return result.IsValid ? Ok(result.Data) : StatusCode(result.StatusCode, result);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-17 03:13:16 +02:00
|
|
|
[HttpPost, Route("/clientapi/user/email/remove"), EnableRateLimiting(RateLimiting.Accounts), Authorize(Policy = Policies.IsUser)]
|
2026-11-10 01:24:43 +01:00
|
|
|
public async Task<IActionResult> RemoveEmail(PasswordForm form, CancellationToken token = default)
|
2026-10-17 03:13:16 +02:00
|
|
|
{
|
|
|
|
|
if (!ModelState.IsValid)
|
2026-11-10 01:24:43 +01:00
|
|
|
return BadRequest(new WebResult().Invalidate(localizer["Invalid model."]));
|
|
|
|
|
var result = await usersService.RemoveEmailAsync(User.GetUserId(), form.Password, token);
|
2026-10-17 03:13:16 +02:00
|
|
|
return result.IsValid ? Ok(result.Data) : StatusCode(result.StatusCode, result);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-18 08:52:17 +01:00
|
|
|
[HttpPost, Route("/clientapi/user/update/settings"), Authorize(Policy = Policies.IsUser)]
|
2026-11-10 01:24:43 +01:00
|
|
|
public async Task<IActionResult> UpdateUserSettings(ViewUserSettings userSettings, CancellationToken token = default)
|
2023-02-18 08:52:17 +01:00
|
|
|
{
|
|
|
|
|
var result = new WebResult();
|
|
|
|
|
if (!ModelState.IsValid)
|
2026-11-10 01:24:43 +01:00
|
|
|
return BadRequest(result.Invalidate(localizer["Invalid model."]));
|
2023-02-18 08:52:17 +01:00
|
|
|
try
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
result = await usersService.UpdateUserSettingsAsync(userSettings, User.GetUserId(), token);
|
2023-02-18 08:52:17 +01:00
|
|
|
if (!result.IsValid)
|
|
|
|
|
return StatusCode(result.StatusCode, result);
|
|
|
|
|
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
logger.LogError(ex, $"{nameof(User)}.{nameof(UpdateUserSettings)}()");
|
|
|
|
|
return BadRequest(result.Invalidate(localizer["Something went wrong."]));
|
2023-02-18 08:52:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost, Route("/clientapi/user/update/password"), Authorize(Policy = Policies.IsUser)]
|
2026-11-10 01:24:43 +01:00
|
|
|
public async Task<IActionResult> UpdatePassword(UserPasswordForm userPasswordForm, CancellationToken token = default)
|
2023-02-18 08:52:17 +01:00
|
|
|
{
|
|
|
|
|
var result = new WebResult();
|
|
|
|
|
if (!ModelState.IsValid)
|
2026-11-10 01:24:43 +01:00
|
|
|
return BadRequest(result.Invalidate(localizer["Invalid model."]));
|
2023-02-18 08:52:17 +01:00
|
|
|
try
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
result = await usersService.UpdateUserPasswordAsync(userPasswordForm, User.GetUserId(), token);
|
2023-02-18 08:52:17 +01:00
|
|
|
if (!result.IsValid)
|
|
|
|
|
return StatusCode(result.StatusCode, result);
|
|
|
|
|
|
2026-10-17 03:13:16 +02:00
|
|
|
// every session ended with the change; this device goes on with a new one
|
|
|
|
|
var (user, userSettings) = ((RootUser, ViewUserSettings))result.Data;
|
2026-11-10 01:24:43 +01:00
|
|
|
return Ok(authTokenManager.GenerateToken(user, userSettings));
|
2023-02-18 08:52:17 +01:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
logger.LogError(ex, $"{nameof(User)}.{nameof(UpdatePassword)}()");
|
|
|
|
|
return BadRequest(result.Invalidate(localizer["Something went wrong."]));
|
2023-02-18 08:52:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-07 11:03:25 +02:00
|
|
|
// what the login's media take, every persona's, and its quota when the server sets one
|
|
|
|
|
[HttpGet, Route("/clientapi/user/storage"), Authorize(Policy = Policies.IsUser)]
|
|
|
|
|
public async Task<IActionResult> GetStorage([FromServices] Microsoft.Extensions.Options.IOptionsMonitor<Domain.Media.MediaOptions> media, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var (bytes, files) = await Domain.Privacy.Counted.MediaOfRoot(User.GetUserId(), token);
|
|
|
|
|
var quota = media.CurrentValue.QuotaBytesPerRoot;
|
|
|
|
|
return Ok(new PrivaPub.ClientModels.User.ViewStorage { MediaBytes = bytes, MediaFiles = files, QuotaBytes = quota > 0 ? quota : null });
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-18 08:52:17 +01:00
|
|
|
[HttpGet, Route("/clientapi/user/settings"), Authorize(Policy = Policies.IsUser)]
|
2026-11-10 01:24:43 +01:00
|
|
|
public async Task<IActionResult> GetUserSettings(CancellationToken token = default)
|
2023-02-18 08:52:17 +01:00
|
|
|
{
|
|
|
|
|
var result = new WebResult();
|
|
|
|
|
try
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
result = await usersService.GetUserSettingsAsync(User.GetUserId(), token: token);
|
2023-02-18 08:52:17 +01:00
|
|
|
if (!result.IsValid)
|
|
|
|
|
return StatusCode(result.StatusCode, result);
|
|
|
|
|
return Ok(result.Data);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
logger.LogError(ex, $"{nameof(User)}.{nameof(GetUserSettings)}()");
|
|
|
|
|
return BadRequest(result.Invalidate(localizer["Something went wrong."]));
|
2023-02-18 08:52:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-01 11:02:21 +02:00
|
|
|
[HttpPost, Route("/clientapi/user/recover/password"), EnableRateLimiting(RateLimiting.Accounts), AllowAnonymous]
|
2026-11-10 01:24:43 +01:00
|
|
|
public async Task<IActionResult> RecoverPassword(PasswordRecoveryForm passwordRecoveryForm, CancellationToken token = default)
|
2023-02-18 08:52:17 +01:00
|
|
|
{
|
|
|
|
|
var result = new WebResult();
|
|
|
|
|
if (!ModelState.IsValid)
|
2026-11-10 01:24:43 +01:00
|
|
|
return BadRequest(result.Invalidate(localizer["Invalid model."]));
|
2023-02-18 08:52:17 +01:00
|
|
|
try
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
var host = string.IsNullOrEmpty(appConfiguration.CurrentValue.FrontendBaseAddress)
|
2026-10-01 08:05:56 +02:00
|
|
|
? $"{Request.Scheme}://{Request.Host.Host}"
|
2026-11-10 01:24:43 +01:00
|
|
|
: appConfiguration.CurrentValue.FrontendBaseAddress.TrimEnd('/');
|
|
|
|
|
result = await usersService.SetupAndSendRecoveryEmail(passwordRecoveryForm, host, token);
|
2023-02-18 08:52:17 +01:00
|
|
|
if (!result.IsValid)
|
|
|
|
|
return StatusCode(result.StatusCode, result);
|
|
|
|
|
|
2026-10-04 03:16:59 +02:00
|
|
|
return Ok(result.Data);//the same sentence for everyone
|
2023-02-18 08:52:17 +01:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
logger.LogError(ex, $"{nameof(User)}.{nameof(RecoverPassword)}()");
|
|
|
|
|
return BadRequest(result.Invalidate(localizer["Something went wrong."]));
|
2023-02-18 08:52:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-01 11:02:21 +02:00
|
|
|
[HttpPost, Route("/clientapi/user/recover/valid"), EnableRateLimiting(RateLimiting.Accounts), AllowAnonymous]
|
2023-02-18 08:52:17 +01:00
|
|
|
public async Task<IActionResult> IsValidRecoveryCode(
|
|
|
|
|
[FromBody,
|
|
|
|
|
Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(ErrorsResource)),
|
|
|
|
|
Display(Name = "RecoveryCode", ResourceType = typeof(FieldsNameResource))]
|
2026-11-10 01:24:43 +01:00
|
|
|
string recoveryCode, CancellationToken token = default)
|
2023-02-18 08:52:17 +01:00
|
|
|
{
|
|
|
|
|
var result = new WebResult();
|
|
|
|
|
try
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
result = await usersService.IsValidRecoveryCode(recoveryCode, token);
|
2023-02-18 08:52:17 +01:00
|
|
|
if (!result.IsValid)
|
|
|
|
|
return StatusCode(result.StatusCode, result);
|
|
|
|
|
|
|
|
|
|
return Ok(result.Data);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
logger.LogError(ex, $"{nameof(User)}.{nameof(IsValidRecoveryCode)}()");
|
|
|
|
|
return BadRequest(result.Invalidate(localizer["Something went wrong."]));
|
2023-02-18 08:52:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-01 11:02:21 +02:00
|
|
|
[HttpPost, Route("/clientapi/user/recover/update/password"), EnableRateLimiting(RateLimiting.Accounts), AllowAnonymous]
|
2026-11-10 01:24:43 +01:00
|
|
|
public async Task<IActionResult> ChangePassword(NewPasswordForm newPasswordForm, CancellationToken token = default)
|
2023-02-18 08:52:17 +01:00
|
|
|
{
|
|
|
|
|
var result = new WebResult();
|
|
|
|
|
if (!ModelState.IsValid)
|
2026-11-10 01:24:43 +01:00
|
|
|
return BadRequest(result.Invalidate(localizer["Invalid model."]));
|
2023-02-18 08:52:17 +01:00
|
|
|
try
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
result = await usersService.ChangePassword(newPasswordForm, token);
|
2023-02-18 08:52:17 +01:00
|
|
|
if (!result.IsValid)
|
|
|
|
|
return StatusCode(result.StatusCode, result);
|
|
|
|
|
|
|
|
|
|
return Ok(result.Data);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
logger.LogError(ex, $"{nameof(User)}.{nameof(ChangePassword)}()");
|
|
|
|
|
return BadRequest(result.Invalidate(localizer["Something went wrong."]));
|
2023-02-18 08:52:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-04 03:16:59 +02:00
|
|
|
[HttpPost, Route("/clientapi/user/delete"), EnableRateLimiting(RateLimiting.Accounts), Authorize(Policy = Policies.IsUser)]
|
2026-11-10 01:24:43 +01:00
|
|
|
public async Task<IActionResult> RemoveSelf(RemoveSelfForm form, CancellationToken token = default)
|
2026-10-04 03:16:59 +02:00
|
|
|
{
|
|
|
|
|
var result = new WebResult();
|
|
|
|
|
if (!ModelState.IsValid)
|
2026-11-10 01:24:43 +01:00
|
|
|
return BadRequest(result.Invalidate(localizer["Invalid model."]));
|
2026-10-04 03:16:59 +02:00
|
|
|
try
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
result = await usersService.RemoveSelfAsync(User.GetUserId(), form.Password, token);
|
2026-10-04 03:16:59 +02:00
|
|
|
return result.IsValid ? Ok() : StatusCode(result.StatusCode, result);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
logger.LogError(ex, $"{nameof(User)}.{nameof(RemoveSelf)}()");
|
|
|
|
|
return BadRequest(result.Invalidate(localizer["Something went wrong."]));
|
2026-10-04 03:16:59 +02:00
|
|
|
}
|
|
|
|
|
}
|
2023-02-18 08:52:17 +01:00
|
|
|
|
|
|
|
|
#endregion User endpoints
|
|
|
|
|
|
|
|
|
|
#region Auth refresh
|
|
|
|
|
|
|
|
|
|
[HttpGet, Route("/clientapi/user/sniff/again"), Authorize(Policy = Policies.IsUser)]
|
2026-10-03 11:49:55 +02:00
|
|
|
public async Task<IActionResult> SniffAgain([FromServices] DbEntities dbEntities, CancellationToken token)
|
2023-02-18 08:52:17 +01:00
|
|
|
{
|
2026-10-03 11:49:55 +02:00
|
|
|
var result = new WebResult();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var user = await dbEntities.RootUsers.MatchID(User.GetUserId()).ExecuteFirstAsync(token);
|
|
|
|
|
if (user is not { IsBanned: false, DeletedAt: null })
|
2026-11-10 01:24:43 +01:00
|
|
|
return Unauthorized(result.Invalidate(localizer["Unauthorized."], StatusCodes.Status401Unauthorized));
|
|
|
|
|
result = await usersService.GetUserSettingsAsync(user.ID, token: token);
|
2026-10-03 11:49:55 +02:00
|
|
|
if (!result.IsValid)
|
|
|
|
|
return StatusCode(result.StatusCode, result);
|
2023-02-18 08:52:17 +01:00
|
|
|
|
2026-11-10 01:24:43 +01:00
|
|
|
return Ok(authTokenManager.GenerateToken(user, (ViewUserSettings)result.Data, User.FindFirst(AuthTokenManager.SessionId)?.Value));
|
2026-10-03 11:49:55 +02:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2026-11-10 01:24:43 +01:00
|
|
|
logger.LogError(ex, $"{nameof(User)}.{nameof(SniffAgain)}()");
|
|
|
|
|
return BadRequest(result.Invalidate(localizer["Something went wrong."]));
|
2026-10-03 11:49:55 +02:00
|
|
|
}
|
2023-02-18 08:52:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|