Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions API.IntegrationTests/Tests/OpenApiDocumentTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Net;
using System.Text.Json;

namespace OpenShock.API.IntegrationTests.Tests;

public sealed class OpenApiDocumentTests
{
[ClassDataSource<WebApplicationFactory>(Shared = SharedType.PerTestSession)]
public required WebApplicationFactory WebApplicationFactory { get; init; }

[Test]
public async Task GetOpenApiDocument_ServesSingleDocumentWithEveryVersion()
{
using var client = WebApplicationFactory.CreateClient();

var response = await client.GetAsync("/openapi.json");

await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK);
await Assert.That(response.Content.Headers.ContentType?.MediaType).IsEqualTo("application/json");

using var doc = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
var paths = doc.RootElement.GetProperty("paths").EnumerateObject().Select(p => p.Name).ToArray();

await Assert.That(paths).Contains("/1/account");
await Assert.That(paths).Contains("/2/account/login");
}
}
2 changes: 2 additions & 0 deletions API/API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
<!-- NuGet packages -->
<ItemGroup>
<PackageReference Include="AspNet.Security.OAuth.Discord" />
<!-- Referenced directly so its XML-comment source generator runs here, where AddOpenApi is called -->
<PackageReference Include="Microsoft.AspNetCore.OpenApi" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Google" />
<PackageReference Include="AspNet.Security.OAuth.Twitter" />
</ItemGroup>
Expand Down
5 changes: 2 additions & 3 deletions API/Controller/Account/Activate.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Net.Mime;
using Asp.Versioning;
using Microsoft.AspNetCore.Mvc;
using OpenShock.Common.ApiVersioning;
using OpenShock.Common.Errors;
using OpenShock.Internal.Common.Problems;

Expand All @@ -11,11 +11,10 @@ public sealed partial class AccountController
/// <summary>
/// Activate account
/// </summary>
/// <response code="200"></response>
[HttpPost("activate")]
[ApiVersion(1)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType<OpenShockProblem>(StatusCodes.Status400BadRequest, MediaTypeNames.Application.ProblemJson)]
[MapToApiVersion("1")]
public async Task<IActionResult> Activate([FromQuery(Name = "token")] string token, CancellationToken cancellationToken)
{
bool ok = await _accountService.TryActivateAccountAsync(token, cancellationToken);
Expand Down
2 changes: 2 additions & 0 deletions API/Controller/Account/Authenticated/ChangeEmail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.AspNetCore.Mvc;
using OpenShock.API.Models.Requests;
using OpenShock.API.Services.Account;
using OpenShock.Common.ApiVersioning;
using OpenShock.Common.Errors;
using OpenShock.Common.Utils;
using OpenShock.Internal.Common.Problems;
Expand All @@ -19,6 +20,7 @@ public sealed partial class AuthenticatedAccountController
/// <param name="body"></param>
/// <param name="cancellationToken"></param>
[HttpPost("email-change")]
[ApiVersion(1)]
[Consumes(MediaTypeNames.Application.Json)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType<OpenShockProblem>(StatusCodes.Status400BadRequest, MediaTypeNames.Application.ProblemJson)] // EmailChangeUnchanged
Expand Down
3 changes: 2 additions & 1 deletion API/Controller/Account/Authenticated/ChangePassword.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.AspNetCore.Mvc;
using OpenShock.API.Models.Requests;
using OpenShock.API.Services.Account;
using OpenShock.Common.ApiVersioning;
using OpenShock.Common.Errors;
using OpenShock.Common.Utils;
using OpenShock.Internal.Common.Problems;
Expand All @@ -17,9 +18,9 @@ public sealed partial class AuthenticatedAccountController
/// </summary>
/// <param name="body"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
[HttpPost("password")]
[ApiVersion(1)]
[Consumes(MediaTypeNames.Application.Json)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType<OpenShockProblem>(StatusCodes.Status403Forbidden, MediaTypeNames.Application.ProblemJson)] // PasswordChangeInvalidPassword
Expand Down
3 changes: 2 additions & 1 deletion API/Controller/Account/Authenticated/ChangeUsername.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.AspNetCore.Mvc;
using OpenShock.API.Models.Requests;
using OpenShock.API.Services.Account;
using OpenShock.Common.ApiVersioning;
using OpenShock.Common.Errors;
using OpenShock.Common.OpenShockDb;
using OpenShock.Common.Validation;
Expand All @@ -18,9 +19,9 @@ public sealed partial class AuthenticatedAccountController
/// </summary>
/// <param name="body"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
[HttpPost("username")]
[ApiVersion(1)]
[Consumes(MediaTypeNames.Application.Json)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType<OpenShockProblem>(StatusCodes.Status409Conflict, MediaTypeNames.Application.ProblemJson)] // UsernameTaken
Expand Down
2 changes: 2 additions & 0 deletions API/Controller/Account/Authenticated/Deactivate.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Diagnostics;
using System.Net.Mime;
using Microsoft.AspNetCore.Mvc;
using OpenShock.Common.ApiVersioning;
using OpenShock.Common.Errors;
using OpenShock.Internal.Common.Problems;
using AccountSvc = OpenShock.API.Services.Account;
Expand All @@ -15,6 +16,7 @@ public sealed partial class AuthenticatedAccountController
/// </summary>
/// <response code="204">Done.</response>
[HttpDelete]
[ApiVersion(1)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType<OpenShockProblem>(StatusCodes.Status403Forbidden, MediaTypeNames.Application.ProblemJson)] // CannotDeactivatePrivledgedAccount
public async Task<IActionResult> Deactivate(CancellationToken cancellationToken)
Expand Down
2 changes: 2 additions & 0 deletions API/Controller/Account/Authenticated/GetAuditLog.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Net.Mime;
using Microsoft.AspNetCore.Mvc;
using OpenShock.API.Models.Response;
using OpenShock.Common.ApiVersioning;
using OpenShock.Common.OpenShockDb;
using OpenShock.Common.Services.Audit;
using OpenShock.Common.Utils;
Expand All @@ -18,6 +19,7 @@ public sealed partial class AuthenticatedAccountController
/// <param name="cancellationToken"></param>
/// <response code="200">A page of audit log entries.</response>
[HttpGet("audit-log")]
[ApiVersion(1)]
[ProducesResponseType<PagedResult<AuditLogEntryResponse>>(StatusCodes.Status200OK, MediaTypeNames.Application.Json)]
public async Task<PagedResult<AuditLogEntryResponse>> GetAuditLog(
[FromQuery] PaginationQuery pagination,
Expand Down
2 changes: 2 additions & 0 deletions API/Controller/Account/Authenticated/GetSelf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using OpenShock.API.Models.Response;
using OpenShock.Common.ApiVersioning;
using OpenShock.Common.Authentication;
using OpenShock.Common.Extensions;
using OpenShock.Common.OpenShockDb;
Expand All @@ -15,6 +16,7 @@ public sealed partial class AuthenticatedAccountController
/// </summary>
/// <response code="200">The account information was successfully retrieved.</response>
[HttpGet]
[ApiVersion(1)]
[Authorize(AuthenticationSchemes = OpenShockAuthSchemes.UserSessionApiTokenCombo)]
[ProducesResponseType<UserSelfResponse>(StatusCodes.Status200OK, MediaTypeNames.Application.Json)]
public UserSelfResponse GetSelf() => new()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Mvc;
using OpenShock.API.Services.OAuthConnection;
using OpenShock.Common.ApiVersioning;

namespace OpenShock.API.Controller.Account.Authenticated;

Expand All @@ -14,6 +15,7 @@ public sealed partial class AuthenticatedAccountController
/// <response code="204">Connection removed.</response>
/// <response code="404">No connection found for this provider.</response>
[HttpDelete("connections/{provider}")]
[ApiVersion(1)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> RemoveOAuthConnection([FromRoute] string provider, [FromServices] IOAuthConnectionService connectionService, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.EntityFrameworkCore;
using OpenShock.API.Models.Response;
using OpenShock.API.Services.OAuthConnection;
using OpenShock.Common.ApiVersioning;

namespace OpenShock.API.Controller.Account.Authenticated;

Expand All @@ -14,6 +15,7 @@ public sealed partial class AuthenticatedAccountController
/// <returns>Array of connections with provider key, external id, display name and link time.</returns>
/// <response code="200">Returns the list of connections.</response>
[HttpGet("connections")]
[ApiVersion(1)]
[ProducesResponseType<OAuthConnectionResponse[]>(StatusCodes.Status200OK, MediaTypeNames.Application.Json)]
public IActionResult ListOAuthConnections([FromServices] IOAuthConnectionService connectionService, CancellationToken cancellationToken)
{
Expand Down
4 changes: 1 addition & 3 deletions API/Controller/Account/Authenticated/_ApiController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Asp.Versioning;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using OpenShock.API.Services.Account;
using OpenShock.Common.Authentication;
Expand All @@ -12,7 +11,6 @@ namespace OpenShock.API.Controller.Account.Authenticated;
/// </summary>
[ApiController]
[Tags("Account")]
[ApiVersion("1")]
[Route("/{version:apiVersion}/account")]
[Authorize(AuthenticationSchemes = OpenShockAuthSchemes.UserSessionCookie)]
public sealed partial class AuthenticatedAccountController : AuthenticatedSessionControllerBase
Expand Down
3 changes: 2 additions & 1 deletion API/Controller/Account/CheckUsername.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.AspNetCore.Mvc;
using OpenShock.API.Models.Requests;
using OpenShock.API.Services.Account;
using OpenShock.Common.ApiVersioning;
using OpenShock.Common.Results;
using OpenShock.Common.Validation;

Expand All @@ -16,8 +17,8 @@ public sealed partial class AccountController
/// </summary>
/// <param name="body"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpPost("username/check")] // High-volume endpoint, we don't want to rate limit this
[ApiVersion(1)]
[Consumes(MediaTypeNames.Application.Json)]
public async Task<UsernameCheckResponse> CheckUsername([FromBody] ChangeUsernameRequest body, CancellationToken cancellationToken)
{
Expand Down
6 changes: 3 additions & 3 deletions API/Controller/Account/Login.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Asp.Versioning;
using Microsoft.AspNetCore.Mvc;
using OpenShock.Common.ApiVersioning;
using OpenShock.Common.Errors;

namespace OpenShock.API.Controller.Account;
Expand All @@ -9,9 +9,9 @@ public sealed partial class AccountController
/// <summary>
/// Authenticate a user. Retired: use POST /2/account/login instead.
/// </summary>
[HttpPost("login")]
[Obsolete("Retired. Use POST /2/account/login instead.")]
[HttpPost("login")]
[ApiVersion(1)]
[ApiExplorerSettings(IgnoreApi = true)]
[MapToApiVersion("1")]
public IActionResult Login() => Problem(GoneError.EndpointRetired("POST /2/account/login"));
}
4 changes: 2 additions & 2 deletions API/Controller/Account/LoginV2.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System.Net.Mime;
using Asp.Versioning;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.RateLimiting;
using OpenShock.API.Errors;
using OpenShock.API.Models.Requests;
using OpenShock.API.Models.Response;
using OpenShock.API.Services.Account;
using OpenShock.API.Services.Turnstile;
using OpenShock.Common.ApiVersioning;
using OpenShock.Common.Errors;
using OpenShock.Common.OpenShockDb;
using OpenShock.Common.Services.AutomationTokens;
Expand All @@ -23,12 +23,12 @@ public sealed partial class AccountController
/// <response code="200">User successfully logged in</response>
/// <response code="401">Invalid username or password</response>
[HttpPost("login")]
[ApiVersion(2)]
[EnableRateLimiting("auth")]
[Consumes(MediaTypeNames.Application.Json)]
[ProducesResponseType<LoginV2OkResponse>(StatusCodes.Status200OK, MediaTypeNames.Application.Json)]
[ProducesResponseType<OpenShockProblem>(StatusCodes.Status401Unauthorized, MediaTypeNames.Application.ProblemJson)] // InvalidCredentials
[ProducesResponseType<OpenShockProblem>(StatusCodes.Status403Forbidden, MediaTypeNames.Application.ProblemJson)] // InvalidDomain, AutomationTokenNotAllowedForAccount
[MapToApiVersion("2")]
public async Task<IActionResult> LoginV2(
[FromBody] LoginV2 body,
[FromServices] ICloudflareTurnstileService turnstileService,
Expand Down
6 changes: 3 additions & 3 deletions API/Controller/Account/Logout.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Asp.Versioning;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using OpenShock.Common.ApiVersioning;
using OpenShock.Common.Extensions;
using OpenShock.Common.Services.Session;

Expand All @@ -8,8 +8,8 @@ namespace OpenShock.API.Controller.Account;
public sealed partial class AccountController
{
[HttpPost("logout")]
[ApiVersion(1)]
[ProducesResponseType(StatusCodes.Status200OK)]
[MapToApiVersion("1")]
public async Task<IActionResult> Logout([FromServices] ISessionService sessionService)
{
// Remove session if valid
Expand Down
6 changes: 3 additions & 3 deletions API/Controller/Account/PasswordResetCheckValid.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Diagnostics;
using System.Net.Mime;
using Asp.Versioning;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.RateLimiting;
using OpenShock.API.Services.Account;
using OpenShock.Common.ApiVersioning;
using OpenShock.Common.Errors;
using OpenShock.Common.Models;
using OpenShock.Internal.Common.Problems;
Expand All @@ -20,8 +20,8 @@ public sealed partial class AccountController
/// <param name="secret">The secret of the password reset</param>
[Obsolete("Retired. Use GET /1/account/password-reset/{passwordResetId}/{secret} instead.")]
[HttpHead("recover/{passwordResetId}/{secret}")]
[ApiVersion(1)]
[ApiExplorerSettings(IgnoreApi = true)]
[MapToApiVersion("1")]
public IActionResult PasswordResetCheckValidLegacy([FromRoute] Guid passwordResetId, [FromRoute] string secret)
=> Problem(GoneError.EndpointRetired("GET /1/account/password-reset/{passwordResetId}/{secret}"));

Expand All @@ -34,10 +34,10 @@ public IActionResult PasswordResetCheckValidLegacy([FromRoute] Guid passwordRese
/// <response code="200">Valid password reset process</response>
/// <response code="404">Password reset process not found</response>
[HttpGet("password-reset/{passwordResetId}/{secret}")]
[ApiVersion(1)]
[EnableRateLimiting("auth")]
[ProducesResponseType<LegacyEmptyResponse>(StatusCodes.Status200OK, MediaTypeNames.Application.Json)]
[ProducesResponseType<OpenShockProblem>(StatusCodes.Status404NotFound, MediaTypeNames.Application.ProblemJson)] // PasswordResetNotFound
[MapToApiVersion("1")]
public async Task<IActionResult> PasswordResetCheckValid([FromRoute] Guid passwordResetId, [FromRoute] string secret, CancellationToken cancellationToken)
{
var passwordResetExists = await _accountService.CheckPasswordResetExistsAsync(passwordResetId, secret, cancellationToken);
Expand Down
6 changes: 3 additions & 3 deletions API/Controller/Account/PasswordResetComplete.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Diagnostics;
using System.Net.Mime;
using Asp.Versioning;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.RateLimiting;
using OpenShock.API.Services.Account;
using OpenShock.Common.ApiVersioning;
using OpenShock.Common.Errors;
using OpenShock.Common.Models;
using OpenShock.Internal.Common.Problems;
Expand All @@ -20,8 +20,8 @@ public sealed partial class AccountController
/// <param name="secret">The secret of the password reset</param>
[Obsolete("Retired. Use POST /1/account/password-reset/{passwordResetId}/{secret}/complete instead.")]
[HttpPost("recover/{passwordResetId}/{secret}")]
[ApiVersion(1)]
[ApiExplorerSettings(IgnoreApi = true)]
[MapToApiVersion("1")]
public IActionResult PasswordResetCompleteLegacy([FromRoute] Guid passwordResetId, [FromRoute] string secret)
=> Problem(GoneError.EndpointRetired("POST /1/account/password-reset/{passwordResetId}/{secret}/complete"));

Expand All @@ -35,11 +35,11 @@ public IActionResult PasswordResetCompleteLegacy([FromRoute] Guid passwordResetI
/// <response code="200">Password successfully changed</response>
/// <response code="404">Password reset process not found</response>
[HttpPost("password-reset/{passwordResetId}/{secret}/complete")]
[ApiVersion(1)]
[EnableRateLimiting("auth")]
[Consumes(MediaTypeNames.Application.Json)]
[ProducesResponseType<LegacyEmptyResponse>(StatusCodes.Status200OK, MediaTypeNames.Application.Json)]
[ProducesResponseType<OpenShockProblem>(StatusCodes.Status404NotFound, MediaTypeNames.Application.ProblemJson)] // PasswordResetNotFound
[MapToApiVersion("1")]
public async Task<IActionResult> PasswordResetComplete([FromRoute] Guid passwordResetId,
[FromRoute] string secret, [FromBody] PasswordResetProcessData body,
CancellationToken cancellationToken)
Expand Down
6 changes: 3 additions & 3 deletions API/Controller/Account/PasswordResetInitiate.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Asp.Versioning;
using Microsoft.AspNetCore.Mvc;
using OpenShock.Common.ApiVersioning;
using OpenShock.Common.Errors;

namespace OpenShock.API.Controller.Account;
Expand All @@ -9,9 +9,9 @@ public sealed partial class AccountController
/// <summary>
/// Initiate a password reset. Retired: use POST /2/account/password-reset instead.
/// </summary>
[HttpPost("reset")]
[Obsolete("Retired. Use POST /2/account/password-reset instead.")]
[HttpPost("reset")]
[ApiVersion(1)]
[ApiExplorerSettings(IgnoreApi = true)]
[MapToApiVersion("1")]
public IActionResult PasswordResetInitiate() => Problem(GoneError.EndpointRetired("POST /2/account/password-reset"));
}
Loading
Loading