// Copyright Epic Games, Inc. All Rights Reserved. using HordeServer.Authentication; using HordeServer.Models; using HordeServer.Services; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.OpenIdConnect; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; namespace HordeServer.Controllers { /// /// Controller managing account status /// [ApiController] [Route("[controller]")] public class AccountController : Controller { const string DefaultAuthenticationScheme = OktaDefaults.AuthenticationScheme; /// /// 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; }"; /// /// The ACL service singleton /// AclService AclService; /// /// Constructor /// /// ACL service instance public AccountController(AclService AclService) { this.AclService = AclService; } /// /// Gets the current login status /// /// The current login state [HttpGet] [Route("/account")] public async Task State() { StringBuilder Content = new StringBuilder(); Content.Append($"

Horde Server

"); if (User.Identity.IsAuthenticated) { Content.Append($"

User {User.Identity.Name} is logged in. Log out

"); if (await AclService.AuthorizeAsync(AclAction.AdminWrite, User)) { Content.Append("

"); Content.Append("Get bearer token
"); Content.Append("Get agent registration token
"); Content.Append("Get agent software upload token
"); Content.Append("Get agent software download token
"); Content.Append("Get configuration token
"); Content.Append("Get chained job token
"); Content.Append("

"); } Content.Append($"

Claims for {User.Identity.Name}:"); Content.Append(""); foreach (System.Security.Claims.Claim Claim in User.Claims) { Content.Append($""); } Content.Append("
{Claim.Type}{Claim.Value}
"); Content.Append("

"); Content.Append($"

Built from Perforce

"); } else { Content.Append("

Login with OAuth2

"); } Content.Append(""); return new ContentResult { ContentType = "text/html", StatusCode = (int)HttpStatusCode.OK, Content = Content.ToString() }; } /// /// Login to the server /// /// Http result [HttpGet] [Route("/account/login")] public IActionResult Login() { return new ChallengeResult(DefaultAuthenticationScheme, new AuthenticationProperties { RedirectUri = "/account" }); } /// /// Logout of the current account /// /// Http result [HttpGet] [Route("/account/logout")] public async Task Logout() { await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); try { await HttpContext.SignOutAsync(DefaultAuthenticationScheme); } catch { } string Content = $"

User has been logged out. Returning to login page.

"; return new ContentResult { ContentType = "text/html", StatusCode = (int)HttpStatusCode.OK, Content = Content }; } } }