Skip to content

Commit

Permalink
feat: add support for Int32
Browse files Browse the repository at this point in the history
  • Loading branch information
Kampfmoehre committed Aug 6, 2024
1 parent b2dd7a7 commit 54a2466
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ If a claim with the given name is found the modelbinder will try to convert the
- `string`
- `Guid`
- `Enum`
- `int`

**Note:** Which claims exist in the User object is dependent on your authentication middleware and out of the scope of this repository. For example you can extend the `AuthenticationHandler` like described in [the official docs](https://docs.microsoft.com/en-us/aspnet/core/security/authentication/?view=aspnetcore-7.0#authentication-handler) and add custom claims to the user.

Expand Down
19 changes: 19 additions & 0 deletions src/DroidSolutions.Oss.AuthClaimBinder/ClaimModelBinder.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Globalization;
using System.Security.Claims;

using DroidSolutions.Oss.AuthClaimBinder.Exceptions;
Expand Down Expand Up @@ -72,6 +73,24 @@ public Task BindModelAsync(ModelBindingContext bindingContext)

bindingContext.Result = ModelBindingResult.Success(value);
}
else if (bindingContext.ModelType == typeof(int))
{
try
{
bindingContext.Result = ModelBindingResult.Success(int.Parse(claim.Value, CultureInfo.InvariantCulture));
}
catch (Exception ex)
{
bindingContext.Result = ModelBindingResult.Failed();
_logger.LogError(ex, "The claim {FieldName} could not be parsed to an Int32!", bindingContext.FieldName);

throw new ClaimParsingException(
$"The claim {bindingContext.FieldName} could not be parsed to an Int32!",
ex,
bindingContext.FieldName,
bindingContext.ModelType);
}
}
else
{
bindingContext.Result = ModelBindingResult.Success(claim.Value);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Security.Claims;
using System.Threading.Tasks;

Expand Down Expand Up @@ -220,4 +221,47 @@ public async Task BindingModelAsync_ShouldThrowInvalidOperationException_WhenEnu

await Assert.ThrowsAsync<ClaimParsingException>(() => claimModelBinder.BindModelAsync(bindingContext.Object));
}

[Fact]
public async Task BindingModelAsync_ShouldBeAbleToParseInts()
{
int num = 42;
Claim[] claims = [new Claim("someId", num.ToString(CultureInfo.InvariantCulture))];

Check warning on line 229 in test/DroidSolutions.Oss.AuthClaimBinderTest/ClaimModelBinderTests.cs

View workflow job for this annotation

GitHub Actions / coverage

Opening square brackets should not be preceded by a space. (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1010.md)

Check warning on line 229 in test/DroidSolutions.Oss.AuthClaimBinderTest/ClaimModelBinderTests.cs

View workflow job for this annotation

GitHub Actions / build

Opening square brackets should not be preceded by a space. (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1010.md)

Check warning on line 229 in test/DroidSolutions.Oss.AuthClaimBinderTest/ClaimModelBinderTests.cs

View workflow job for this annotation

GitHub Actions / build

Opening square brackets should not be preceded by a space. (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1010.md)
ClaimsIdentity identity = new (claims, "Scheme");
ClaimsPrincipal principal = new(identity);

Mock<HttpContext> httpContext = new();
httpContext.Setup(hc => hc.User).Returns(principal);

Mock<DefaultModelBindingContext> bindingContext = new() { CallBase = true };
bindingContext.Setup(bc => bc.HttpContext).Returns(httpContext.Object);
bindingContext.Setup(bc => bc.FieldName).Returns("someId");
bindingContext.Setup(bc => bc.ModelType).Returns(typeof(int));

ClaimModelBinder claimModelBinder = new(_logMock.Object, null);

await claimModelBinder.BindModelAsync(bindingContext.Object);

Assert.Equal(num, bindingContext.Object.Result.Model);
}

[Fact]
public async Task BindingModelAsync_ShouldThrowInvalidOperationException_WhenIntIsNotParsable()
{
Claim[] claims = [new Claim("someId", "42,5d")];

Check warning on line 251 in test/DroidSolutions.Oss.AuthClaimBinderTest/ClaimModelBinderTests.cs

View workflow job for this annotation

GitHub Actions / coverage

Opening square brackets should not be preceded by a space. (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1010.md)

Check warning on line 251 in test/DroidSolutions.Oss.AuthClaimBinderTest/ClaimModelBinderTests.cs

View workflow job for this annotation

GitHub Actions / build

Opening square brackets should not be preceded by a space. (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1010.md)

Check warning on line 251 in test/DroidSolutions.Oss.AuthClaimBinderTest/ClaimModelBinderTests.cs

View workflow job for this annotation

GitHub Actions / build

Opening square brackets should not be preceded by a space. (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1010.md)
ClaimsIdentity identity = new(claims, "Scheme");
ClaimsPrincipal principal = new(identity);

Mock<HttpContext> httpContext = new();
httpContext.Setup(hc => hc.User).Returns(principal);

Mock<DefaultModelBindingContext> bindingContext = new() { CallBase = true };
bindingContext.Setup(bc => bc.HttpContext).Returns(httpContext.Object);
bindingContext.Setup(bc => bc.FieldName).Returns("someId");
bindingContext.Setup(bc => bc.ModelType).Returns(typeof(int));

ClaimModelBinder claimModelBinder = new(_logMock.Object, null);

await Assert.ThrowsAsync<ClaimParsingException>(() => claimModelBinder.BindModelAsync(bindingContext.Object));
}
}

0 comments on commit 54a2466

Please sign in to comment.