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

Update JsonWebToken to enable extensibility #2582

Merged
merged 4 commits into from
May 15, 2024
Merged

Update JsonWebToken to enable extensibility #2582

merged 4 commits into from
May 15, 2024

Conversation

pmaytak
Copy link
Contributor

@pmaytak pmaytak commented May 4, 2024

Fixes #2581.

Extract token reading code into a separate ReadPayloadValue so it can be overridden with custom logic in child classes.
Make ReadPayloadValue private protected (accessible by types derived from the containing class, but only within its containing assembly or friend assemblies).

Example derived class:

    public class CustomJsonWebToken : JsonWebToken
    {
        public CustomJsonWebToken (string jwtEncodedString) : base(jwtEncodedString) { }

        /// <summary>
        /// Tries to read custom claims first.
        /// If nothing matched, reads common claims and the rest of unnamed claims.
        /// </summary>
        private protected override void ReadPayloadValue(ref Utf8JsonReader reader, Dictionary<string, object> claims)
        {
            if (reader.ValueTextEquals("CustomClaim"))
            {
                _customClaim = JsonSerializerPrimitives.ReadString(ref reader, "CustomClaim", ClassName, true);
                claims["CustomClaim"] = _customClaim ;
            }
            else
            {
                base.ReadPropertyValue(ref reader, claims);
            }
        }

        private string _customClaim;

        public string CustomClaim
        {
            get
            {
                _customClaim ??= Payload.GetStringValue("CustomClaim");
                return _customClaim;
            }
        }
    }

There was no change in performance.

@pmaytak pmaytak marked this pull request as ready for review May 7, 2024 17:37
@pmaytak pmaytak requested a review from a team as a code owner May 7, 2024 17:37
@pmaytak pmaytak requested review from brentschmaltz, keegan-caruso and a team May 14, 2024 21:52
@pmaytak pmaytak merged commit 909669f into dev May 15, 2024
5 checks passed
@pmaytak pmaytak deleted the pmaytak/jwt-ext branch May 15, 2024 06:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature Request] Update JsonWebToken to enable extensibility
4 participants