-
Notifications
You must be signed in to change notification settings - Fork 352
Improving custom JWT authentication #3681
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
Draft
MichaelWagner-blue-zone
wants to merge
4
commits into
Azure:main
Choose a base branch
from
MichaelWagner-blue-zone:dev/michaelwagner-blue-zone
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
afbffb4
Merge pull request #1 from Azure/main
MichaelWagner-blue-zone 549eabd
* configurable JWT role claim handling (rolesPath, rolesSeparator)
bluezone2011 5a7cb3b
Potential fix for pull request finding
MichaelWagner-blue-zone de48093
Changes due to inputs
bluezone2011 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,6 +1,36 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace Azure.DataApiBuilder.Config.ObjectModel; | ||
|
|
||
| public record JwtOptions(string? Audience, string? Issuer); | ||
| public record JwtOptions | ||
| { | ||
| [JsonPropertyName("audience")] | ||
| public string? Audience { get; init; } | ||
|
|
||
| [JsonPropertyName("issuer")] | ||
| public string? Issuer { get; init; } | ||
|
|
||
|
MichaelWagner-blue-zone marked this conversation as resolved.
|
||
| [JsonPropertyName("rolesPath")] | ||
| public string? RolesPath { get; init; } | ||
|
|
||
| [JsonPropertyName("rolesSeparator")] | ||
| public string? RolesSeparator { get; init; } | ||
|
|
||
| [JsonPropertyName("jwksUrl")] | ||
| public string? JwksUrl { get; init; } | ||
|
|
||
| public string ResolvedRoleClaimType => string.IsNullOrWhiteSpace(RolesPath) | ||
| ? AuthenticationOptions.ROLE_CLAIM_TYPE | ||
| : RolesPath; | ||
|
|
||
| public string? ResolvedRolesSeparator => string.IsNullOrEmpty(RolesSeparator) | ||
| ? null | ||
| : RolesSeparator; | ||
|
|
||
| public string? ResolvedJwksUrl => string.IsNullOrWhiteSpace(JwksUrl) | ||
| ? (string.IsNullOrWhiteSpace(Issuer) ? null : $"{Issuer.TrimEnd('/')}/.well-known/jwks.json") | ||
| : JwksUrl; | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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,23 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| namespace Azure.DataApiBuilder.Service; | ||
|
|
||
| public static class JwtHttpClientFactory | ||
| { | ||
| public static HttpClient Create() | ||
| { | ||
| bool allowSelfSigned = Environment.GetEnvironmentVariable("USE_SELF_SIGNED_CERT") | ||
| ?.Equals("true", StringComparison.OrdinalIgnoreCase) == true; | ||
|
|
||
| HttpClientHandler handler = new(); | ||
|
|
||
| if (allowSelfSigned) | ||
| { | ||
| handler.ServerCertificateCustomValidationCallback = | ||
| HttpClientHandler.DangerousAcceptAnyServerCertificateValidator; | ||
| } | ||
|
|
||
| return new HttpClient(handler, disposeHandler: true); | ||
| } | ||
| } |
127 changes: 127 additions & 0 deletions
127
src/Core/AuthenticationHelpers/JwtRoleClaimsTransformer.cs
This file contains hidden or 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,127 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Security.Claims; | ||
| using System.Text.Json; | ||
|
|
||
| namespace Azure.DataApiBuilder.Core.AuthenticationHelpers; | ||
|
|
||
| public static class JwtRoleClaimsTransformer | ||
| { | ||
| public static void NormalizeRoleClaims( | ||
| ClaimsPrincipal principal, | ||
| string sourceRoleClaimType, | ||
| string? separator) | ||
| { | ||
| foreach (ClaimsIdentity identity in principal.Identities) | ||
| { | ||
| if (!identity.IsAuthenticated) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| List<Claim> sourceClaims = identity.Claims | ||
| .Where(c => c.Type.Equals(sourceRoleClaimType, StringComparison.Ordinal)) | ||
| .ToList(); | ||
|
|
||
| if (sourceClaims.Count == 0) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| HashSet<string> normalizedValues = new(StringComparer.OrdinalIgnoreCase); | ||
|
|
||
| foreach (Claim claim in sourceClaims) | ||
| { | ||
| foreach (string expandedValue in ExpandClaimValues(claim.Value, separator)) | ||
| { | ||
| if (!string.IsNullOrWhiteSpace(expandedValue)) | ||
| { | ||
| normalizedValues.Add(expandedValue.Trim()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| foreach (string normalizedValue in normalizedValues) | ||
| { | ||
| bool exactClaimAlreadyExists = identity.Claims.Any(c => | ||
| c.Type.Equals(sourceRoleClaimType, StringComparison.Ordinal) && | ||
| c.Value.Equals(normalizedValue, StringComparison.OrdinalIgnoreCase)); | ||
|
|
||
| if (!exactClaimAlreadyExists) | ||
| { | ||
| identity.AddClaim(new Claim(sourceRoleClaimType, normalizedValue, ClaimValueTypes.String)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static IEnumerable<string> ExpandClaimValues(string rawValue, string? separator) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(rawValue)) | ||
| { | ||
| yield break; | ||
| } | ||
|
|
||
| string trimmed = rawValue.Trim(); | ||
|
|
||
| // 1. JSON array support | ||
| if (trimmed.StartsWith('[') && trimmed.EndsWith(']')) | ||
| { | ||
| List<string>? values; | ||
| try | ||
| { | ||
| values = JsonSerializer.Deserialize<List<string>>(trimmed); | ||
| } | ||
| catch (JsonException) | ||
| { | ||
| values = null; | ||
| } | ||
|
|
||
| if (values is not null) | ||
| { | ||
| foreach (string value in values) | ||
| { | ||
| if (!string.IsNullOrWhiteSpace(value)) | ||
| { | ||
| yield return value.Trim(); | ||
| } | ||
| } | ||
|
|
||
| yield break; | ||
| } | ||
| } | ||
|
|
||
| // 2. Configurable separated string support | ||
| if (!string.IsNullOrEmpty(separator)) | ||
| { | ||
| string[] splitValues; | ||
|
|
||
| if (separator.Length == 1) | ||
| { | ||
| splitValues = trimmed.Split( | ||
| separator[0], | ||
| StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); | ||
| } | ||
| else | ||
| { | ||
| splitValues = trimmed.Split( | ||
| new[] { separator }, | ||
| StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); | ||
| } | ||
|
|
||
| foreach (string value in splitValues) | ||
| { | ||
| if (!string.IsNullOrWhiteSpace(value)) | ||
| { | ||
| yield return value; | ||
| } | ||
| } | ||
|
|
||
| yield break; | ||
| } | ||
|
|
||
| // 3. Single scalar fallback | ||
| yield return trimmed; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.