Skip to content

Commit

Permalink
Merge pull request #48 from bennetrr/fix/http-context-exception
Browse files Browse the repository at this point in the history
fix(backend): HttpContext Exception
  • Loading branch information
Bennet Ranft authored Jul 23, 2024
2 parents a90e4e5 + 0a6dd38 commit 73fd1d3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public partial class SetController
[HttpGet("{setId}/parts")]
public async Task<ActionResult<IEnumerable<PartDto>>> GetParts([FromRoute] string setId)
{
var organizationOrUserId = await AuthorizationUtilities.GetOrganizationOrUserId();
var organizationOrUserId = await AuthorizationUtilities.GetOrganizationOrUserId(HttpContext);

await context.Sets
.Where(x => x.Id == setId)
Expand All @@ -51,7 +51,7 @@ await context.Sets
[HttpGet("{setId}/parts/{partId}")]
public async Task<ActionResult<PartDto>> GetPart([FromRoute] string setId, [FromRoute] string partId)
{
var organizationOrUserId = await AuthorizationUtilities.GetOrganizationOrUserId();
var organizationOrUserId = await AuthorizationUtilities.GetOrganizationOrUserId(HttpContext);

var part = await context.Parts
.Where(x => x.Id == partId)
Expand Down Expand Up @@ -81,7 +81,7 @@ public async Task<ActionResult<PartDto>> GetPart([FromRoute] string setId, [From
public async Task<ActionResult<UpdatePartResponse>> UpdatePart([FromRoute] string setId, [FromRoute] string partId,
[FromBody] UpdatePartRequest request)
{
var organizationOrUserId = await AuthorizationUtilities.GetOrganizationOrUserId();
var organizationOrUserId = await AuthorizationUtilities.GetOrganizationOrUserId(HttpContext);

var part = await context.Parts
.Include(x => x.Set)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ namespace Bennetr.BrickInv.Api.Controllers;
[Authorize]
public partial class SetController(
BrickInvContext context,
ClerkApiClient clerk,
IRebrickableClient rebrickable,
IOptions<AppOptions> options) : ControllerBase
{
Expand All @@ -41,7 +40,7 @@ public partial class SetController(
[HttpGet]
public async Task<ActionResult<IEnumerable<SetDto>>> GetSets()
{
var organizationOrUserId = await AuthorizationUtilities.GetOrganizationOrUserId();
var organizationOrUserId = await AuthorizationUtilities.GetOrganizationOrUserId(HttpContext);

var sets = await context.Sets
.Where(x => x.OrganizationOrUserId == organizationOrUserId)
Expand All @@ -63,7 +62,7 @@ public async Task<ActionResult<IEnumerable<SetDto>>> GetSets()
[HttpGet("{setId}")]
public async Task<ActionResult<SetDto>> GetSet([FromRoute] string setId)
{
var organizationOrUserId = await AuthorizationUtilities.GetOrganizationOrUserId();
var organizationOrUserId = await AuthorizationUtilities.GetOrganizationOrUserId(HttpContext);

var set = await context.Sets
.Where(x => x.Id == setId)
Expand Down Expand Up @@ -118,7 +117,7 @@ public async Task<ActionResult<SetDto>> CreateSet([FromBody] CreateSetRequest re
throw;
}

var organizationOrUserId = await AuthorizationUtilities.GetOrganizationOrUserId();
var organizationOrUserId = await AuthorizationUtilities.GetOrganizationOrUserId(HttpContext);

var set = new Models.Set
{
Expand Down Expand Up @@ -188,7 +187,7 @@ public async Task<ActionResult<SetDto>> CreateSet([FromBody] CreateSetRequest re
[HttpDelete("{setId}")]
public async Task<IActionResult> DeleteSet([FromRoute] string setId)
{
var organizationOrUserId = await AuthorizationUtilities.GetOrganizationOrUserId();
var organizationOrUserId = await AuthorizationUtilities.GetOrganizationOrUserId(HttpContext);

var set = await context.Sets
.Where(x => x.Id == setId)
Expand Down Expand Up @@ -221,7 +220,7 @@ public async Task<IActionResult> DeleteSet([FromRoute] string setId)
[HttpPatch("{setId}")]
public async Task<ActionResult<SetDto>> UpdateSet([FromRoute] string setId, [FromBody] UpdateSetRequest request)
{
var organizationOrUserId = await AuthorizationUtilities.GetOrganizationOrUserId();
var organizationOrUserId = await AuthorizationUtilities.GetOrganizationOrUserId(HttpContext);

var set = await context.Sets
.Where(x => x.Id == setId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@

namespace Bennetr.BrickInv.Api.Utilities;

public class AuthorizationUtilities
public static class AuthorizationUtilities
{
private static readonly HttpContext? HttpContext = new HttpContextAccessor().HttpContext;
private static readonly JwtSecurityTokenHandler JwtHandler = new();

public static async Task<string> GetOrganizationOrUserId()
public static async Task<string> GetOrganizationOrUserId(HttpContext httpContext)
{
if (HttpContext == null) throw new Exception("GetOrganizationOrUserId called outside of a HTTP context");

var token = await HttpContext.GetTokenAsync("Bearer", "access_token");
var token = await httpContext.GetTokenAsync("Bearer", "access_token");
var jwt = JwtHandler.ReadJwtToken(token);

try
Expand Down

0 comments on commit 73fd1d3

Please sign in to comment.