Esempio n. 1
0
        /// <summary>Returns a new instance of <see cref="Jwks"/>.</summary>
        /// <param name="issuer">The issuer of the keys.</param>
        /// <param name="json">a string that contains JSON Web Key parameters in JSON format.</param>
        public static Jwks FromJson(string issuer, ReadOnlySpan <byte> json)
        {
            // a JWKS is :
            // {
            //   "keys": [
            //   { jwk1 },
            //   { jwk2 },
            //   ...
            //   ]
            // }
            var jwks   = new Jwks(issuer);
            var reader = new Utf8JsonReader(json, true, default);

            if (reader.Read() && reader.TokenType is JsonTokenType.StartObject && reader.Read())
            {
                while (reader.TokenType is JsonTokenType.PropertyName)
                {
                    var propertyName = reader.ValueSpan;
                    if (propertyName.Length == 4)
                    {
                        ref byte propertyNameRef = ref MemoryMarshal.GetReference(propertyName);
                        if (IntegerMarshal.ReadUInt32(ref propertyNameRef) == keys /* keys */)
                        {
                            if (reader.Read() && reader.TokenType is JsonTokenType.StartArray)
                            {
                                while (reader.Read() && reader.TokenType is JsonTokenType.StartObject)
                                {
                                    Jwk jwk = Jwk.FromJsonReader(ref reader);
                                    jwks.Add(jwk);
                                }

                                if (!(reader.TokenType is JsonTokenType.EndArray) || !reader.Read())
                                {
                                    ThrowHelper.ThrowInvalidOperationException_MalformedJwks();
                                }
                            }

                            continue;
                        }
                    }

                    reader.Read();
                    if (reader.TokenType >= JsonTokenType.String && reader.TokenType <= JsonTokenType.Null)
                    {
                        reader.Read();
                    }
                    else if (reader.TokenType == JsonTokenType.StartObject)
                    {
                        JsonParser.ConsumeJsonObject(ref reader);
                    }
                    else if (reader.TokenType == JsonTokenType.StartArray)
                    {
                        JsonParser.ConsumeJsonArray(ref reader);
                    }
                    else
                    {
                        ThrowHelper.ThrowInvalidOperationException_MalformedJwks();
                    }
                }
            }
Esempio n. 2
0
        public static Address FromJson(ReadOnlySpan <byte> json)
        {
            Utf8JsonReader reader = new Utf8JsonReader(json, true, default);

            var address = new Address();

            while (reader.Read())
            {
                switch (reader.TokenType)
                {
                case JsonTokenType.EndObject:
                    return(address);

                case JsonTokenType.PropertyName:
                    var propertyName = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan;

                    reader.Read();
                    switch (reader.TokenType)
                    {
                    case JsonTokenType.String:
                        if (propertyName.SequenceEqual(OidcClaims.FormattedUtf8))
                        {
                            address.Formatted = reader.GetString();
                        }
                        else if (propertyName.SequenceEqual(OidcClaims.StreetAddressUtf8))
                        {
                            address.StreetAddress = reader.GetString();
                        }
                        else if (propertyName.SequenceEqual(OidcClaims.LocalityUtf8))
                        {
                            address.Locality = reader.GetString();
                        }
                        else if (propertyName.SequenceEqual(OidcClaims.RegionUtf8))
                        {
                            address.Region = reader.GetString();
                        }
                        else if (propertyName.SequenceEqual(OidcClaims.PostalCodeUtf8))
                        {
                            address.PostalCode = reader.GetString();
                        }
                        else if (propertyName.SequenceEqual(OidcClaims.CountryUtf8))
                        {
                            address.Country = reader.GetString();
                        }
                        break;

                    case JsonTokenType.StartArray:
                        JsonParser.ConsumeJsonArray(ref reader);
                        break;

                    case JsonTokenType.StartObject:
                        JsonParser.ConsumeJsonObject(ref reader);
                        break;
                    }
                    break;

                default:
                    ThrowHelper.ThrowFormatException_MalformedJson();
                    break;
                }
            }

            ThrowHelper.ThrowFormatException_MalformedJson();
            return(null);
        }