tgstation-server 6.19.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.Mvc;
7using Microsoft.Extensions.Logging;
8using Microsoft.Extensions.Options;
9using Microsoft.Extensions.Primitives;
10using Microsoft.Net.Http.Headers;
11
12using Octokit;
13
28
30{
34 [Route(Routes.ApiRoot)]
35 public sealed class ApiRootController : ApiController
36 {
41
46
51
56
61
66
70 readonly IOptionsSnapshot<GeneralConfiguration> generalConfigurationOptions;
71
75 readonly IOptionsSnapshot<SecurityConfiguration> securityConfigurationOptions;
76
93 IDatabaseContext databaseContext,
94 IAuthenticationContext authenticationContext,
100 IOptionsSnapshot<GeneralConfiguration> generalConfigurationOptions,
101 IOptionsSnapshot<SecurityConfiguration> securityConfigurationOptions,
102 ILogger<ApiRootController> logger,
103 IApiHeadersProvider apiHeadersProvider,
105 : base(
106 databaseContext,
107 authenticationContext,
108 apiHeadersProvider,
109 logger,
110 false)
111 {
112 this.assemblyInformationProvider = assemblyInformationProvider ?? throw new ArgumentNullException(nameof(assemblyInformationProvider));
113 this.platformIdentifier = platformIdentifier ?? throw new ArgumentNullException(nameof(platformIdentifier));
114 this.oAuthProviders = oAuthProviders ?? throw new ArgumentNullException(nameof(oAuthProviders));
115 this.swarmService = swarmService ?? throw new ArgumentNullException(nameof(swarmService));
116 this.serverControl = serverControl ?? throw new ArgumentNullException(nameof(serverControl));
117 this.generalConfigurationOptions = generalConfigurationOptions ?? throw new ArgumentNullException(nameof(generalConfigurationOptions));
118 this.securityConfigurationOptions = securityConfigurationOptions ?? throw new ArgumentNullException(nameof(securityConfigurationOptions));
119 this.loginAuthority = loginAuthority ?? throw new ArgumentNullException(nameof(loginAuthority));
120 }
121
129 [HttpGet]
130 [ProducesResponseType(typeof(ServerInformationResponse), 200)]
131 public IActionResult ServerInfo()
132 {
133 // if they tried to authenticate in any form and failed, let them know immediately
134 bool failIfUnauthed;
135 if (ApiHeaders == null)
136 {
137 try
138 {
139 // we only allow authorization header issues
141 }
142 catch (HeadersException ex)
143 {
144 return HeadersIssue(ex);
145 }
146
147 failIfUnauthed = Request.Headers.Authorization.Count > 0;
148 }
149 else
150 failIfUnauthed = ApiHeaders.Token != null;
151
152 if (failIfUnauthed && !AuthenticationContext.Valid)
153 return Unauthorized();
154
155 return Json(new ServerInformationResponse
156 {
158 ApiVersion = ApiHeaders.Version,
159 DMApiVersion = DMApiConstants.InteropVersion,
160 MinimumPasswordLength = generalConfigurationOptions.Value.MinimumPasswordLength,
161 InstanceLimit = generalConfigurationOptions.Value.InstanceLimit,
162 UserLimit = generalConfigurationOptions.Value.UserLimit,
163 UserGroupLimit = generalConfigurationOptions.Value.UserGroupLimit,
164 ValidInstancePaths = generalConfigurationOptions.Value.ValidInstancePaths,
165 WindowsHost = platformIdentifier.IsWindows,
166 SwarmServers = swarmService
168 ?.Select(swarmServerInfo => new SwarmServerResponse(swarmServerInfo))
169 .ToList(),
170 OAuthProviderInfos = oAuthProviders.ProviderInfos(),
171 OidcProviderInfos = securityConfigurationOptions.Value.OidcProviderInfos().ToList(),
173 OidcStrictMode = securityConfigurationOptions.Value.OidcStrictMode,
174 });
175 }
176
186 [HttpPost]
187 [ProducesResponseType(typeof(TokenResponse), 200)]
188 [ProducesResponseType(typeof(ErrorMessageResponse), 429)]
189 public ValueTask<IActionResult> CreateToken(CancellationToken cancellationToken)
190 {
191 if (ApiHeaders == null)
192 {
193 Response.Headers.Add(HeaderNames.WWWAuthenticate, new StringValues($"basic realm=\"Create TGS {ApiHeaders.BearerAuthenticationScheme} token\""));
194 return ValueTask.FromResult(HeadersIssue(ApiHeadersProvider.HeadersException!));
195 }
196
197 return loginAuthority.InvokeTransformable<LoginResult, TokenResponse>(this, authority => authority.AttemptLogin(cancellationToken));
198 }
199
208 [HttpPost("oauth_gateway")]
209 [ProducesResponseType(typeof(OAuthGatewayResponse), 200)]
210 [ProducesResponseType(typeof(ErrorMessageResponse), 429)]
211 public ValueTask<IActionResult> CreateOAuthGatewayToken(CancellationToken cancellationToken)
212 => loginAuthority.InvokeTransformable<OAuthGatewayLoginResult, OAuthGatewayResponse>(this, authority => authority.AttemptOAuthGatewayLogin(cancellationToken));
213 }
214}
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...
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 IOptionsSnapshot< SecurityConfiguration > securityConfigurationOptions
The IOptionsSnapshot<TOptions> of SecurityConfiguration for the ApiRootController.
ApiRootController(IDatabaseContext databaseContext, IAuthenticationContext authenticationContext, IAssemblyInformationProvider assemblyInformationProvider, IOAuthProviders oAuthProviders, IPlatformIdentifier platformIdentifier, ISwarmService swarmService, IServerControl serverControl, IOptionsSnapshot< GeneralConfiguration > generalConfigurationOptions, IOptionsSnapshot< SecurityConfiguration > securityConfigurationOptions, ILogger< ApiRootController > logger, IApiHeadersProvider apiHeadersProvider, IRestAuthorityInvoker< ILoginAuthority > loginAuthority)
Initializes a new instance of the ApiRootController class.
readonly IRestAuthorityInvoker< ILoginAuthority > loginAuthority
The IRestAuthorityInvoker<TAuthority> for the ILoginAuthority.
readonly IPlatformIdentifier platformIdentifier
The IPlatformIdentifier for the ApiRootController.
IActionResult ServerInfo()
Main page of the Application.
readonly IAssemblyInformationProvider assemblyInformationProvider
The IAssemblyInformationProvider for the ApiRootController.
readonly IOAuthProviders oAuthProviders
The IOAuthProviders for the ApiRootController.
readonly IOptionsSnapshot< GeneralConfiguration > generalConfigurationOptions
The IOptionsSnapshot<TOptions> of GeneralConfiguration 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.