// Copyright Epic Games, Inc. All Rights Reserved.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using EpicGames.Horde.Accounts;
using EpicGames.Horde.Acls;
using EpicGames.Horde.Server;
using Horde.Server.Accounts;
using Horde.Server.Acls;
using Horde.Server.Authentication;
using Horde.Server.Users;
using Horde.Server.Utilities;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
#pragma warning disable CA1054 // URI-like parameters should not be strings
namespace Horde.Server.Server
{
///
/// Model for Horde account login view
///
public class HordeAccountLoginViewModel
{
///
/// Where to post the form
///
public string? FormPostUrl { get; set; }
///
/// Optional error message to display
///
public string? ErrorMessage { get; set; }
}
///
/// Controller managing account status
///
[ApiController]
[Route("[controller]")]
public class AccountController : Controller
{
///
/// Style sheet for HTML responses
///
const string StyleSheet =
"body { font-family: 'Segoe UI', 'Roboto', arial, sans-serif; } " +
"p { margin:20px; font-size:13px; } " +
"h1 { margin:20px; font-size:32px; font-weight:200; } " +
"table { margin:10px 20px; } " +
"td { margin:5px; font-size:13px; }";
readonly IUserCollection _users;
readonly IAccountCollection _hordeAccounts;
readonly string _authenticationScheme;
readonly IOptionsSnapshot _globalConfig;
///
/// Constructor
///
public AccountController(IUserCollection users, IAccountCollection hordeAccounts, IOptionsMonitor serverSettings, IOptionsSnapshot globalConfig)
{
_users = users;
_hordeAccounts = hordeAccounts;
_authenticationScheme = GetAuthScheme(serverSettings.CurrentValue.AuthMethod);
_globalConfig = globalConfig;
}
///
/// Get auth scheme name for a given auth method
///
/// Authentication method
/// Name of authentication scheme
public static string GetAuthScheme(AuthMethod method)
{
return method switch
{
AuthMethod.Anonymous => AnonymousAuthHandler.AuthenticationScheme,
AuthMethod.Okta => OktaAuthHandler.AuthenticationScheme,
AuthMethod.OpenIdConnect => OpenIdConnectDefaults.AuthenticationScheme,
AuthMethod.Horde => CookieAuthenticationDefaults.AuthenticationScheme,
_ => throw new ArgumentOutOfRangeException(nameof(method), method, null)
};
}
///
/// Gets the current login status
///
/// The current login state
[HttpGet]
[Route("/account")]
public ActionResult State()
{
StringBuilder content = new StringBuilder();
content.Append($"
Horde Server
");
if (User.Identity?.IsAuthenticated ?? false)
{
content.Append(CultureInfo.InvariantCulture, $"