2024-12-09 21:11:46 +01:00
|
|
|
// Copyright (C) 2023-present sam/u1f320 (vulpine.solutions)
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2024-06-12 16:19:49 +02:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2024-12-10 14:09:32 +01:00
|
|
|
using System.Text.Json.Serialization;
|
2024-12-04 01:48:52 +01:00
|
|
|
using System.Web;
|
|
|
|
using Foxnouns.Backend.Database;
|
|
|
|
using Foxnouns.Backend.Database.Models;
|
|
|
|
using Foxnouns.Backend.Extensions;
|
|
|
|
using Foxnouns.Backend.Utils;
|
|
|
|
using Humanizer;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-06-12 16:19:49 +02:00
|
|
|
|
2024-12-04 01:48:52 +01:00
|
|
|
namespace Foxnouns.Backend.Services.Auth;
|
2024-06-12 16:19:49 +02:00
|
|
|
|
2024-12-09 21:07:53 +01:00
|
|
|
public partial class RemoteAuthService(
|
2024-12-04 01:48:52 +01:00
|
|
|
Config config,
|
|
|
|
ILogger logger,
|
|
|
|
DatabaseContext db,
|
|
|
|
KeyCacheService keyCacheService
|
|
|
|
)
|
2024-06-12 16:19:49 +02:00
|
|
|
{
|
2024-09-13 14:56:38 +02:00
|
|
|
private readonly ILogger _logger = logger.ForContext<RemoteAuthService>();
|
2024-06-12 16:19:49 +02:00
|
|
|
private readonly HttpClient _httpClient = new();
|
|
|
|
|
|
|
|
public record RemoteUser(string Id, string Username);
|
2024-12-04 01:48:52 +01:00
|
|
|
|
2024-12-10 14:09:32 +01:00
|
|
|
[SuppressMessage("ReSharper", "ClassNeverInstantiated.Local")]
|
|
|
|
private record OauthTokenResponse(
|
|
|
|
[property: JsonPropertyName("access_token")] string AccessToken,
|
|
|
|
[property: JsonPropertyName("token_type")] string TokenType
|
|
|
|
);
|
|
|
|
|
2024-12-04 01:48:52 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Validates whether a user can still add a new account of the given AuthType, and throws an error if they can't.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="userId">The user to check.</param>
|
|
|
|
/// <param name="authType">The auth type to check.</param>
|
|
|
|
/// <param name="instance">The optional fediverse instance to generate a state for.</param>
|
|
|
|
/// <returns>A state for the given auth type and user ID.</returns>
|
|
|
|
/// <exception cref="ApiError.BadRequest">The given user can't add another account of this type.
|
|
|
|
/// This exception should not be caught by controller code.</exception>
|
|
|
|
public async Task<string> ValidateAddAccountRequestAsync(
|
|
|
|
Snowflake userId,
|
|
|
|
AuthType authType,
|
|
|
|
string? instance = null
|
|
|
|
)
|
|
|
|
{
|
2024-12-08 15:07:25 +01:00
|
|
|
int existingAccounts = await db
|
2024-12-04 01:48:52 +01:00
|
|
|
.AuthMethods.Where(m => m.UserId == userId && m.AuthType == authType)
|
|
|
|
.CountAsync();
|
|
|
|
if (existingAccounts > AuthUtils.MaxAuthMethodsPerType)
|
|
|
|
{
|
|
|
|
throw new ApiError.BadRequest(
|
|
|
|
$"Too many linked {authType.Humanize()} accounts, maximum of {AuthUtils.MaxAuthMethodsPerType} per account."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return HttpUtility.UrlEncode(
|
|
|
|
await keyCacheService.GenerateAddExtraAccountStateAsync(authType, userId, instance)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Checks whether the given state is correct for the given user/auth type combination.
|
|
|
|
/// </summary>
|
|
|
|
/// <exception cref="ApiError.BadRequest">The state doesn't match.
|
|
|
|
/// This exception should not be caught by controller code.</exception>
|
|
|
|
public async Task ValidateAddAccountStateAsync(
|
|
|
|
string state,
|
|
|
|
Snowflake userId,
|
|
|
|
AuthType authType,
|
|
|
|
string? instance = null
|
|
|
|
)
|
|
|
|
{
|
2024-12-08 15:07:25 +01:00
|
|
|
AddExtraAccountState? accountState = await keyCacheService.GetAddExtraAccountStateAsync(
|
|
|
|
state
|
|
|
|
);
|
2024-12-04 01:48:52 +01:00
|
|
|
if (
|
|
|
|
accountState == null
|
|
|
|
|| accountState.AuthType != authType
|
|
|
|
|| accountState.UserId != userId
|
|
|
|
|| (instance != null && accountState.Instance != instance)
|
|
|
|
)
|
2024-12-08 15:17:18 +01:00
|
|
|
{
|
2024-12-04 01:48:52 +01:00
|
|
|
throw new ApiError.BadRequest("Invalid state", "state", state);
|
2024-12-08 15:17:18 +01:00
|
|
|
}
|
2024-12-04 01:48:52 +01:00
|
|
|
}
|
2024-10-02 00:28:07 +02:00
|
|
|
}
|