-
Notifications
You must be signed in to change notification settings - Fork 291
Custom ODataUriParser extension to cast token claim values as database column value type #895
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
71e530d
Add custom OData resolver for promoting binary operand types.
seantleonard 896d655
Update method signatures to address code readability and simplificati…
seantleonard b239948
Additional null checking.
seantleonard 35c6fd6
Adds custom OData resolver only for authorization policy processing.
seantleonard 53380c2
Added unit tests and fixed errors in type handling that arose. And in…
seantleonard dac7e16
remove bad using.
seantleonard 87611e9
remove extra whitespace.
seantleonard 5674b93
updated comment to reflect that the custom resolver is NOT meant for …
seantleonard c083b98
Merge branch 'main' into dev/seleonar/policyClaimTypeCasting
seantleonard d12d7ea
update comment and casting logic
seantleonard bc7e4f6
Merge branch 'dev/seleonar/policyClaimTypeCasting' of https://github.…
seantleonard d1d8e95
Merge branch 'main' into dev/seleonar/policyClaimTypeCasting
seantleonard b82968e
Merge branch 'main' into dev/seleonar/policyClaimTypeCasting
seantleonard 3e9dbb3
Merge branch 'main' into dev/seleonar/policyClaimTypeCasting
seantleonard 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| using System; | ||
| using Microsoft.OData.Edm; | ||
| using Microsoft.OData.UriParser; | ||
|
|
||
| namespace Azure.DataApiBuilder.Service.Parsers | ||
| { | ||
| /// <summary> | ||
| /// Custom OData Resolver which attempts to assist with processing resolved token claims | ||
| /// within an authorization policy string that will be used to create an OData filter clause. | ||
| /// This resolver's type coercion is meant to be utilized for authorization policy processing | ||
| /// and NOT URL query string processing. | ||
| /// </summary> | ||
| /// <seealso cref="https://devblogs.microsoft.com/odata/tutorial-sample-odatauriparser-extension-support/#write-customized-extensions-from-scratch"/> | ||
| public class ClaimsTypeDataUriResolver : ODataUriResolver | ||
| { | ||
| /// <summary> | ||
| /// Between two nodes in the filter clause, determine the: | ||
| /// - PrimaryOperand: Node representing an OData EDM model object and has Kind == QueryNodeKind.SingleValuePropertyAccess. | ||
| /// - OperandToConvert: Node representing a constant value and has kind QueryNodeKind.Constant. | ||
| /// This resolver will overwrite the OperandToConvert node to a new ConstantNode where the value type is that of the PrimaryOperand node. | ||
| /// </summary> | ||
| /// <param name="binaryOperatorKind">the operator kind</param> | ||
| /// <param name="leftNode">the left operand</param> | ||
| /// <param name="rightNode">the right operand</param> | ||
| /// <param name="typeReference">type reference for the result BinaryOperatorNode.</param> | ||
| public override void PromoteBinaryOperandTypes(BinaryOperatorKind binaryOperatorKind, ref SingleValueNode leftNode, ref SingleValueNode rightNode, out IEdmTypeReference typeReference) | ||
| { | ||
| if (leftNode.TypeReference.PrimitiveKind() != rightNode.TypeReference.PrimitiveKind()) | ||
| { | ||
| if ((leftNode.Kind == QueryNodeKind.SingleValuePropertyAccess) && (rightNode is ConstantNode)) | ||
| { | ||
| TryConvertNodeToTargetType( | ||
| targetType: leftNode.TypeReference.PrimitiveKind(), | ||
| operandToConvert: ref rightNode | ||
| ); | ||
| } | ||
| else if (rightNode.Kind == QueryNodeKind.SingleValuePropertyAccess && leftNode is ConstantNode) | ||
| { | ||
| TryConvertNodeToTargetType( | ||
| targetType: rightNode.TypeReference.PrimitiveKind(), | ||
| operandToConvert: ref leftNode | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| base.PromoteBinaryOperandTypes(binaryOperatorKind, ref leftNode, ref rightNode, out typeReference); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Uses type specific parsers to attempt converting the supplied node to a new ConstantNode of type targetType. | ||
| /// </summary> | ||
| /// <param name="targetType">Primitive type (string, bool, int, etc.) of the primary node's value.</param> | ||
| /// <param name="operandToConvert">Node representing a constant value which should be converted to a ConstantNode of type targetType.</param> | ||
| private static void TryConvertNodeToTargetType(EdmPrimitiveTypeKind targetType, ref SingleValueNode operandToConvert) | ||
| { | ||
| ConstantNode? preConvertedConstant = operandToConvert as ConstantNode; | ||
|
|
||
| if (preConvertedConstant?.Value is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (targetType == EdmPrimitiveTypeKind.Int32) | ||
| { | ||
| if (int.TryParse(preConvertedConstant.Value.ToString(), out int result)) | ||
| { | ||
| operandToConvert = new ConstantNode(constantValue: result); | ||
| } | ||
| } | ||
| else if (targetType == EdmPrimitiveTypeKind.String) | ||
| { | ||
| string? objectValue = preConvertedConstant.Value.ToString(); | ||
| if (objectValue is not null) | ||
| { | ||
| operandToConvert = new ConstantNode(constantValue: objectValue); | ||
| } | ||
| } | ||
| else if (targetType == EdmPrimitiveTypeKind.Boolean) | ||
| { | ||
| if (bool.TryParse(preConvertedConstant.Value.ToString(), out bool result)) | ||
| { | ||
| operandToConvert = new ConstantNode(constantValue: result); | ||
| } | ||
| } | ||
| else if (targetType == EdmPrimitiveTypeKind.Guid) | ||
| { | ||
| if (Guid.TryParse(preConvertedConstant.Value.ToString(), out Guid result)) | ||
| { | ||
| operandToConvert = new ConstantNode(constantValue: result); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
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
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
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.