-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow tokens to delete themselves (#165)
- Loading branch information
Showing
4 changed files
with
83 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System.Net.Mime; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.EntityFrameworkCore; | ||
using OpenShock.Common; | ||
using OpenShock.Common.Authentication; | ||
using OpenShock.Common.Authentication.ControllerBase; | ||
using OpenShock.Common.Errors; | ||
using OpenShock.Common.OpenShockDb; | ||
using OpenShock.Common.Problems; | ||
using OpenShock.Common.Utils; | ||
|
||
namespace OpenShock.API.Controller.Tokens; | ||
|
||
[ApiController] | ||
[Route("/{version:apiVersion}/tokens")] | ||
[Authorize(AuthenticationSchemes = OpenShockAuthSchemas.UserSessionApiTokenCombo)] | ||
public sealed class TokenDeleteController : AuthenticatedSessionControllerBase | ||
{ | ||
private readonly OpenShockContext _db; | ||
private readonly ILogger<TokensController> _logger; | ||
|
||
public TokenDeleteController(OpenShockContext db, ILogger<TokensController> logger) | ||
{ | ||
_db = db; | ||
_logger = logger; | ||
} | ||
|
||
/// <summary> | ||
/// Revoke a token | ||
/// </summary> | ||
/// <param name="tokenId"></param> | ||
/// <response code="200">Successfully deleted token</response> | ||
/// <response code="404">The token does not exist or you do not have access to it.</response> | ||
[HttpDelete("{tokenId}")] | ||
[ProducesResponseType(StatusCodes.Status200OK)] | ||
[ProducesResponseType<OpenShockProblem>(StatusCodes.Status404NotFound, | ||
MediaTypeNames.Application.ProblemJson)] // ApiTokenNotFound | ||
public async Task<IActionResult> DeleteToken([FromRoute] Guid tokenId) | ||
{ | ||
var auth = HttpContext.GetAuthenticationMethod(); | ||
|
||
var query = _db.ApiTokens.Where(x => x.Id == tokenId); | ||
|
||
|
||
switch (auth) | ||
{ | ||
case OpenShockAuthSchemas.UserSessionCookie: | ||
query = query.WhereIsUserOrPrivileged(x => x.User, CurrentUser); | ||
break; | ||
case OpenShockAuthSchemas.ApiToken: | ||
{ | ||
var requestTokenId = Guid.Parse(HttpContext.User.Claims.First(x => x.Type == OpenShockAuthClaims.ApiTokenId).Value); | ||
if (requestTokenId != tokenId) return Problem(ApiTokenError.ApiTokenCanOnlyDelete); | ||
break; | ||
} | ||
default: | ||
throw new Exception("Unknown auth method"); | ||
} | ||
|
||
var apiToken = await query.ExecuteDeleteAsync(); | ||
|
||
if (apiToken <= 0) | ||
{ | ||
return Problem(ApiTokenError.ApiTokenNotFound); | ||
} | ||
|
||
return Ok(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters