Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release #49

Merged
merged 4 commits into from
Jul 23, 2024
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ The frontend is configurable with the `env.js` file.
| Name | Type | Description |
|-----------------------|----------|------------------------------------------------------------------------------------------|
| `apiBaseUrl` | `string` | Base URL of the BrickInv API, e.g. `https://api.brickinv.com` or `http://localhost:5105` |
| `clerkPublishableKey` | `string` | Publishable Key of the Clerk application |
| `clerkPublishableKey` | `string` | Publishable key of the Clerk application |

### Backend

Expand All @@ -26,6 +26,8 @@ For development, the .NET user secret manager is recommended, for production a `
|--------------------------------|-------------------------------|----------|--------------------------------------------------------------------------------------|
| `AppConfig__RebrickableApiKey` | `AppConfig.RebrickableApiKey` | `string` | API key for Rebrickable, used for retrieving information about Lego sets |
| `AppConfig__AppBaseUrl` | `AppConfig.AppBaseUrl` | `string` | Base URL of the BrickInv App, e.g. `https://brickinv.com` or `http://localhost:5137` |
| `Clerk__SecretKey` | `Clerk.SecretKey` | `string` | Secret key of the Clerk application |
| `Clerk__Authority` | `Clerk.Authority` | `string` | Instance URL of the Clerk application |

## Development

Expand Down
10 changes: 2 additions & 8 deletions backend.env
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
Email__SenderAddress=""
Email__SenderName=""
Email__Server=""
Email__Port=""
Email__Username=""
Email__Password=""

AppConfig__RebrickableApiKey=""
AppConfig__AppBaseUrl=""
AppConfig__ImprintUrl=""
Clerk__SecretKey=""
Clerk__Authority=""
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