tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
ApiRootController.cs
Go to the documentation of this file.
1using System;
2using System.Linq;
3using System.Threading;
4using System.Threading.Tasks;
5
6using Microsoft.AspNetCore.Authorization;
7using Microsoft.AspNetCore.Mvc;
8using Microsoft.Extensions.Logging;
9using Microsoft.Extensions.Options;
10using Microsoft.Extensions.Primitives;
11using Microsoft.Net.Http.Headers;
12
13using Octokit;
14
29
31{
35 [Route(Routes.ApiRoot)]
36 public sealed class ApiRootController : ApiController
37 {
42
47
52
57
62
67
72
88 IDatabaseContext databaseContext,
89 IAuthenticationContext authenticationContext,
95 IOptions<GeneralConfiguration> generalConfigurationOptions,
96 ILogger<ApiRootController> logger,
97 IApiHeadersProvider apiHeadersProvider,
99 : base(
100 databaseContext,
101 authenticationContext,
102 apiHeadersProvider,
103 logger,
104 false)
105 {
106 this.assemblyInformationProvider = assemblyInformationProvider ?? throw new ArgumentNullException(nameof(assemblyInformationProvider));
107 this.platformIdentifier = platformIdentifier ?? throw new ArgumentNullException(nameof(platformIdentifier));
108 this.oAuthProviders = oAuthProviders ?? throw new ArgumentNullException(nameof(oAuthProviders));
109 this.swarmService = swarmService ?? throw new ArgumentNullException(nameof(swarmService));
110 this.serverControl = serverControl ?? throw new ArgumentNullException(nameof(serverControl));
111 generalConfiguration = generalConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(generalConfigurationOptions));
112 this.loginAuthority = loginAuthority ?? throw new ArgumentNullException(nameof(loginAuthority));
113 }
114
122 [HttpGet]
123 [AllowAnonymous]
124 [ProducesResponseType(typeof(ServerInformationResponse), 200)]
125 public IActionResult ServerInfo()
126 {
127 // if they tried to authenticate in any form and failed, let them know immediately
128 bool failIfUnauthed;
129 if (ApiHeaders == null)
130 {
131 try
132 {
133 // we only allow authorization header issues
135 }
136 catch (HeadersException ex)
137 {
138 return HeadersIssue(ex);
139 }
140
141 failIfUnauthed = Request.Headers.Authorization.Count > 0;
142 }
143 else
144 failIfUnauthed = ApiHeaders.Token != null;
145
146 if (failIfUnauthed && !AuthenticationContext.Valid)
147 return Unauthorized();
148
149 return Json(new ServerInformationResponse
150 {
152 ApiVersion = ApiHeaders.Version,
153 DMApiVersion = DMApiConstants.InteropVersion,
154 MinimumPasswordLength = generalConfiguration.MinimumPasswordLength,
155 InstanceLimit = generalConfiguration.InstanceLimit,
157 UserGroupLimit = generalConfiguration.UserGroupLimit,
158 ValidInstancePaths = generalConfiguration.ValidInstancePaths,
159 WindowsHost = platformIdentifier.IsWindows,
160 SwarmServers = swarmService
162 ?.Select(swarmServerInfo => new SwarmServerResponse(swarmServerInfo))
163 .ToList(),
164 OAuthProviderInfos = oAuthProviders.ProviderInfos(),
166 });
167 }
168
178 [HttpPost]
179 [ProducesResponseType(typeof(TokenResponse), 200)]
180 [ProducesResponseType(typeof(ErrorMessageResponse), 429)]
181 public ValueTask<IActionResult> CreateToken(CancellationToken cancellationToken)
182 {
183 if (ApiHeaders == null)
184 {
185 Response.Headers.Add(HeaderNames.WWWAuthenticate, new StringValues($"basic realm=\"Create TGS {ApiHeaders.BearerAuthenticationScheme} token\""));
186 return ValueTask.FromResult(HeadersIssue(ApiHeadersProvider.HeadersException!));
187 }
188
189 return loginAuthority.InvokeTransformable<LoginResult, TokenResponse>(this, authority => authority.AttemptLogin(cancellationToken));
190 }
191
200 [HttpPost("oauth_gateway")]
201 [ProducesResponseType(typeof(OAuthGatewayResponse), 200)]
202 [ProducesResponseType(typeof(ErrorMessageResponse), 429)]
203 public ValueTask<IActionResult> CreateOAuthGatewayToken(CancellationToken cancellationToken)
204 => loginAuthority.InvokeTransformable<OAuthGatewayLoginResult, OAuthGatewayResponse>(this, authority => authority.AttemptOAuthGatewayLogin(cancellationToken));
205 }
206}
Represents the header that must be present for every server request.
Definition ApiHeaders.cs:25
static readonly Version Version
Get the version of the Api the caller is using.
Definition ApiHeaders.cs:69
Thrown when trying to generate ApiHeaders from Microsoft.AspNetCore.Http.Headers.RequestHeaders fails...
uint UserGroupLimit
The maximum number of user groups allowed.
List< string >? ValidInstancePaths
Limits the locations instances may be created or attached from.
uint MinimumPasswordLength
Minimum length of database user passwords.
uint InstanceLimit
The maximum number of Instances allowed.
Represents an error message returned by the server.
Success result for an OAuth gateway login attempt.
Represents a JWT returned by the API.
Routes to a server actions.
Definition Routes.cs:9
const string ApiRoot
The root of API methods.
Definition Routes.cs:13
Constants used for communication with the DMAPI.
static readonly Version InteropVersion
The DMAPI InteropVersion being used.
Base Controller for API functions.
IActionResult HeadersIssue(HeadersException headersException)
Response for missing/Invalid headers.
Root ApiController for the Application.
ValueTask< IActionResult > CreateOAuthGatewayToken(CancellationToken cancellationToken)
Attempt to authenticate a User using ApiController.ApiHeaders.
ValueTask< IActionResult > CreateToken(CancellationToken cancellationToken)
Attempt to authenticate a User using ApiController.ApiHeaders.
readonly IServerControl serverControl
The IServerControl for the ApiRootController.
readonly IRestAuthorityInvoker< ILoginAuthority > loginAuthority
The IRestAuthorityInvoker<TAuthority> for the ILoginAuthority.
readonly IPlatformIdentifier platformIdentifier
The IPlatformIdentifier for the ApiRootController.
readonly GeneralConfiguration generalConfiguration
The GeneralConfiguration for the ApiRootController.
IActionResult ServerInfo()
Main page of the Application.
readonly IAssemblyInformationProvider assemblyInformationProvider
The IAssemblyInformationProvider for the ApiRootController.
ApiRootController(IDatabaseContext databaseContext, IAuthenticationContext authenticationContext, IAssemblyInformationProvider assemblyInformationProvider, IOAuthProviders oAuthProviders, IPlatformIdentifier platformIdentifier, ISwarmService swarmService, IServerControl serverControl, IOptions< GeneralConfiguration > generalConfigurationOptions, ILogger< ApiRootController > logger, IApiHeadersProvider apiHeadersProvider, IRestAuthorityInvoker< ILoginAuthority > loginAuthority)
Initializes a new instance of the ApiRootController class.
readonly IOAuthProviders oAuthProviders
The IOAuthProviders for the ApiRootController.
readonly ISwarmService swarmService
The ISwarmService for the ApiRootController.
bool Valid
If the IAuthenticationContext is for a valid login.
ApiHeaders CreateAuthlessHeaders()
Attempt to create Api.ApiHeaders without checking for the presence of an Microsoft....
HeadersException? HeadersException
The Api.HeadersException thrown when attempting to parse the ApiHeaders if any.
Invokes TAuthority methods and generates IActionResult responses.
Represents a service that may take an updated Host assembly and run it, stopping the current assembly...
bool UpdateInProgress
Whether or not the server is currently updating.
For creating and accessing authentication contexts.
Dictionary< OAuthProvider, OAuthProviderInfo > ProviderInfos()
Gets a Dictionary<TKey, TValue> of the provider client IDs.
Used for swarm operations. Functions may be no-op based on configuration.
List< SwarmServerInformation >? GetSwarmServers()
Gets the list of SwarmServerInformations in the swarm, including the current one.
For identifying the current platform.
bool IsWindows
If the current platform is a Windows platform.
@ UpdateInProgress
Another update is already in progress.
@ Unauthorized
The swarm private keys didn't match.