Skip to content

Commit

Permalink
Fix auth middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
glucaci committed Mar 4, 2021
1 parent 395dbaa commit cd09a29
Showing 1 changed file with 24 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,43 @@ public BewitAuthorizationMiddleware(

public async Task InvokeAsync(IMiddlewareContext context)
{
try
if (context.ContextData.TryGetValue(BewitTokenHeader.Value, out var objectToken) &&
objectToken is string bewitToken)
{
if (context.ContextData.TryGetValue(
BewitTokenHeader.Value, out var objectToken) &&
objectToken is string bewitToken)
try
{
object payload = await _tokenValidator.ValidateBewitTokenAsync(
new BewitToken<T>(bewitToken),
context.RequestAborted);

_httpContextAccessor.SetBewitPayload(payload);

await _next(context);
}
else
catch (Exception ex)
{
context.Result = ErrorBuilder.New()
.SetMessage("NotAuthorized")
.SetPath(context.Path)
.AddLocation(context.FieldSelection)
.Build();
CreateError(context, ex);
}

await _next(context);
}
catch (Exception ex)
else
{
context.Result = ErrorBuilder.New()
.SetMessage("NotAuthorized")
.SetPath(context.Path)
.SetException(ex)
.AddLocation(context.FieldSelection)
.Build();
CreateError(context);
}
}

private void CreateError(IMiddlewareContext context, Exception ex = default)
{
IErrorBuilder errorBuilder = ErrorBuilder.New()
.SetMessage("NotAuthorized")
.SetPath(context.Path)
.AddLocation(context.FieldSelection);

if (ex != default)
{
errorBuilder.SetException(ex);
}

context.Result = errorBuilder.Build();
}
}
}

0 comments on commit cd09a29

Please sign in to comment.