Skip to content

Commit 49bb4fd

Browse files
committed
Merge OAuth init endpoints and change to POST
1 parent 21a2cca commit 49bb4fd

5 files changed

Lines changed: 24 additions & 51 deletions

File tree

API/Controller/Account/Authenticated/OAuthConnectionAdd.cs

Lines changed: 0 additions & 34 deletions
This file was deleted.

API/Controller/OAuth/Authorize.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,36 @@ namespace OpenShock.API.Controller.OAuth;
1010
public sealed partial class OAuthController
1111
{
1212
/// <summary>
13-
/// Start OAuth authorization for a given provider (login-or-create flow).
13+
/// Start OAuth authorization for a given provider with the specified flow.
1414
/// </summary>
1515
/// <remarks>
16-
/// Initiates an OAuth challenge in "login-or-create" mode.
1716
/// Returns <c>302</c> redirect to the provider authorization page.
1817
/// </remarks>
1918
/// <param name="provider">Provider key (e.g. <c>discord</c>).</param>
19+
/// <param name="flow">Flow to run</param>
2020
/// <response code="302">Redirect to the provider authorization page.</response>
2121
/// <response code="400">Unsupported or misconfigured provider.</response>
2222
[EnableRateLimiting("auth")]
23-
[HttpGet("{provider}/authorize")]
23+
[HttpPost("{provider}/authorize")]
2424
[ProducesResponseType(StatusCodes.Status302Found)]
2525
[ProducesResponseType<OpenShockProblem>(StatusCodes.Status400BadRequest, MediaTypeNames.Application.Json)]
26-
public async Task<IActionResult> OAuthAuthorize([FromRoute] string provider)
26+
public async Task<IActionResult> OAuthAuthorize([FromRoute] string provider, [FromQuery] OAuthFlow flow)
2727
{
2828
if (!await _schemeProvider.IsSupportedOAuthScheme(provider))
2929
return Problem(OAuthError.UnsupportedProvider);
3030

31-
if (User.HasOpenShockUserIdentity())
31+
switch (flow)
3232
{
33-
return Problem(OAuthError.AnonymousOnlyEndpoint);
33+
case OAuthFlow.LoginOrCreate:
34+
if (User.HasOpenShockUserIdentity()) return Problem(OAuthError.FlowRequiresAnonymous);
35+
break;
36+
case OAuthFlow.Link:
37+
if (!User.HasOpenShockUserIdentity()) return Problem(OAuthError.FlowRequiresAuthenticatedUser);
38+
break;
39+
default:
40+
return Problem(OAuthError.UnsupportedFlow);
3441
}
3542

36-
return OAuthUtil.StartOAuth(provider, OAuthFlow.LoginOrCreate);
43+
return OAuthUtil.StartOAuth(provider, flow);
3744
}
3845
}

API/Controller/OAuth/SignupFinalize.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public async Task<IActionResult> OAuthSignupFinalize(
6161

6262
if (User.HasOpenShockUserIdentity())
6363
{
64-
return Problem(OAuthError.AnonymousOnlyEndpoint);
64+
return Problem(OAuthError.FlowRequiresAnonymous);
6565
}
6666

6767
// 1) Defense-in-depth: ensure the flow’s provider matches the route

API/Controller/OAuth/SignupGetData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public async Task<IActionResult> OAuthSignupGetData([FromRoute] string provider)
3232
{
3333
if (User.HasOpenShockUserIdentity())
3434
{
35-
return Problem(OAuthError.AnonymousOnlyEndpoint);
35+
return Problem(OAuthError.FlowRequiresAnonymous);
3636
}
3737

3838
var result = await ValidateOAuthFlowAsync();

API/OAuth/OAuthError.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,15 @@ public static class OAuthError
2727
"This OAuth flow differs from the flow the oauth flow started with",
2828
HttpStatusCode.Forbidden);
2929

30-
public static OpenShockProblem AnonymousOnlyEndpoint => new(
31-
"OAuth.Flow.AnonymousOnlyEndpoint",
30+
public static OpenShockProblem FlowRequiresAnonymous => new(
31+
"OAuth.Flow.AnonymousOnly",
3232
"You must be signed out to call this endpoint",
33-
HttpStatusCode.Unauthorized);
33+
HttpStatusCode.BadRequest);
34+
35+
public static OpenShockProblem FlowRequiresAuthenticatedUser => new(
36+
"OAuth.Link.AuthenticatedUserOnly",
37+
"You must be signed in to link an external account",
38+
HttpStatusCode.BadRequest);
3439

3540
public static OpenShockProblem FlowNotFound => new(
3641
"OAuth.Flow.NotFound",
@@ -53,11 +58,6 @@ public static class OAuthError
5358
"This external account is already linked to another user",
5459
HttpStatusCode.Conflict);
5560

56-
public static OpenShockProblem NotAuthenticatedForLink => new(
57-
"OAuth.Link.NotAuthenticated",
58-
"You must be signed in to link an external account",
59-
HttpStatusCode.Unauthorized);
60-
6161
// Misc / generic
6262
public static OpenShockProblem InternalError => new(
6363
"OAuth.InternalError",

0 commit comments

Comments
 (0)