private static bool TryReadAttributeAs <T>(this XmlReader reader, string name, Func <string, T> convert, out T value)
        {
            if (!reader.TryReadAttribute(name, out var str))
            {
                return(Out.False(out value));
            }

            value = convert(str);
            return(value != null);
        }
        public static bool TryReadElementContent(this XmlReader reader, string name, string ns, out string content)
        {
            if (!reader.IsStartElement(name, ns))
            {
                return(Out.False(out content));
            }
            var value = reader.ReadElementContentAsString();

            content = value;
            return(true);
        }
        protected virtual bool TryReadTokenType(XmlDictionaryReader reader, out Uri tokenType)
        {
            if (!reader.IsStartElement(Elements.TokenType, Namespace))
            {
                return(Out.False(out tokenType));
            }

            _         = reader.TryReadAttributeAsUri(Attributes.Uri, out var uri);
            _         = reader.ReadOuterXml();
            tokenType = uri;
            return(true);
        }
        protected virtual bool TryReadClaimDialect(XmlDictionaryReader reader, out Uri claimDialect)
        {
            if (!reader.IsStartElement(Elements.ClaimDialect, Namespace))
            {
                return(Out.False(out claimDialect));
            }

            _            = reader.TryReadAttributeAsUri(Attributes.Uri, out var uri);
            _            = reader.ReadOuterXml();
            claimDialect = uri;
            return(true);
        }
Пример #5
0
        public virtual bool TryReadEndpointReference(XmlDictionaryReader reader, out EndpointReference endpointReference)
        {
            if (!reader.IsStartElement(Elements.EndpointReference, Namespace))
            {
                return(Out.False(out endpointReference));
            }

            var endpoint = new EndpointReference();

            ReadEndpointReferenceAttributes(reader, endpoint);
            reader.ForEachChild(r => TryReadEndpointReferenceChild(r, endpoint));

            endpointReference = endpoint;
            return(true);
        }
Пример #6
0
        protected virtual bool TryReadAddress(XmlDictionaryReader reader, out Uri address)
        {
            if (!reader.TryReadElementContent(Elements.Address, Namespace, out var content))
            {
                return(Out.False(out address));
            }

            if (!Uri.IsWellFormedUriString(content, UriKind.Absolute))
            {
                return(Out.True(null, out address));
            }

            address = new Uri(content, UriKind.Absolute);
            return(true);
        }
        protected virtual bool TryReadAutomaticPseudonyms(XmlDictionaryReader reader, out bool?automaticPseudonyms)
        {
            if (!reader.TryReadElementContent(Elements.AutomaticPseudonyms, Namespace, out var content))
            {
                return(Out.False(out automaticPseudonyms));
            }

            if (!bool.TryParse(content, out var parsed))
            {
                // The element has been read, but the contents of the element wasn't a valid boolean value. Default to false.
                return(Out.True(false, out automaticPseudonyms));
            }

            automaticPseudonyms = parsed;
            return(true);
        }
Пример #8
0
        public virtual bool TryReadClaimType(XmlDictionaryReader reader, out ClaimType claimType)
        {
            if (!reader.IsStartElement(Elements.ClaimType, Namespace))
            {
                return(Out.False(out claimType));
            }

            if (!reader.TryReadAttributeAsUri(Attributes.Uri, out var uri))
            {
                throw new InvalidOperationException($"Element {Elements.ClaimType} must have a valid {Attributes.Uri} attribute.");
            }

            var type = new ClaimType(uri);

            if (reader.TryReadAttributeAsBoolean(Attributes.Optional, out var optional))
            {
                type.Optional = optional;
            }

            reader.ForEachChild(r =>
            {
                if (r.TryReadElementContent(Elements.DisplayName, Namespace, out var displayName))
                {
                    type.DisplayName = displayName;
                }
                else if (r.TryReadElementContent(Elements.Description, Namespace, out var description))
                {
                    type.Description = description;
                }
                else if (r.TryReadElementContent(Elements.DisplayValue, Namespace, out var displayValue))
                {
                    type.DisplayValue = displayValue;
                }
                else if (r.TryReadElementContent(Elements.Value, Namespace, out var value))
                {
                    type.Value = value;
                }
                else
                {
                    return(false);
                }
                return(true);
            });

            claimType = type;
            return(true);
        }
        protected virtual bool TryReadTokenTypesOffered(XmlDictionaryReader reader, out List <Uri> tokenTypes)
        {
            if (!reader.IsStartElement(Elements.TokenTypesOffered, Namespace))
            {
                return(Out.False(out tokenTypes));
            }
            var list = new List <Uri>();

            reader.ForEachChild(r =>
            {
                if (TryReadTokenType(reader, out var tokenType))
                {
                    list.Add(tokenType);
                    return(true);
                }
                return(false);
            });
            tokenTypes = list;
            return(true);
        }
        protected virtual bool TryReadLogicalServiceNamesOffered(XmlDictionaryReader reader, out List <Uri> logicalServiceNamesOffered)
        {
            if (!reader.IsStartElement(Elements.LogicalServiceNamesOffered, Namespace))
            {
                return(Out.False(out logicalServiceNamesOffered));
            }
            var list = new List <Uri>();

            reader.ForEachChild(r =>
            {
                if (TryReadIssuerName(reader, out var issuerName))
                {
                    list.Add(issuerName);
                    return(true);
                }
                return(false);
            });
            logicalServiceNamesOffered = list;
            return(true);
        }
        protected virtual bool TryReadClaimTypes(XmlDictionaryReader reader, string name, string ns, out List <ClaimType> claimTypes)
        {
            if (!reader.IsStartElement(name, ns))
            {
                return(Out.False(out claimTypes));
            }
            var list = new List <ClaimType>();

            reader.ForEachChild(r =>
            {
                if (WsAuthorizationSerializer.TryReadClaimType(reader, out var claimType))
                {
                    list.Add(claimType);
                    return(true);
                }
                return(false);
            });
            claimTypes = list;
            return(true);
        }
        protected virtual bool TryReadClaimDialectsOffered(XmlDictionaryReader reader, out List <Uri> claimDialectsOffered)
        {
            if (!reader.IsStartElement(Elements.ClaimDialectsOffered, Namespace))
            {
                return(Out.False(out claimDialectsOffered));
            }
            var list = new List <Uri>();

            reader.ForEachChild(r =>
            {
                if (TryReadClaimDialect(reader, out var claimDialect))
                {
                    list.Add(claimDialect);
                    return(true);
                }
                return(false);
            });
            claimDialectsOffered = list;
            return(true);
        }
Пример #13
0
        public virtual bool TryReadEndpointReferenceCollection(XmlDictionaryReader reader, string name, string ns, out EndpointReferenceCollection endpointReferenceCollection)
        {
            if (!reader.IsStartElement(name, ns))
            {
                return(Out.False(out endpointReferenceCollection));
            }
            var collection = new EndpointReferenceCollection();

            reader.ForEachChild(r =>
            {
                if (TryReadEndpointReference(r, out var endpointReference))
                {
                    collection.Add(endpointReference);
                    return(true);
                }
                return(false);
            });

            endpointReferenceCollection = collection;
            return(true);
        }
        protected override bool TryReadRoleDescriptor(XmlDictionaryReader reader, out RoleDescriptor role)
        {
            if (!reader.IsStartElement(Saml2MetadataConstants.Elements.RoleDescriptor, Saml2MetadataConstants.Namespace))
            {
                return(Out.False(out role));
            }

            var d = null as RoleDescriptor;

            if (reader.TryReadFederationEndpointType(out var type))
            {
                if (type == FederationEndpointType.ApplicationService)
                {
                    d = new ApplicationServiceDescriptor();
                }
                if (type == FederationEndpointType.AttributeService)
                {
                    d = new AttributeServiceDescriptor();
                }
                if (type == FederationEndpointType.PseudonymService)
                {
                    d = new PseudonymServiceDescriptor();
                }
                if (type == FederationEndpointType.SecurityTokenService)
                {
                    d = new SecurityTokenServiceDescriptor();
                }
            }
            if (d == null)
            {
                d = new RoleDescriptor();
            }
            ReadRoleDescriptorAttributes(reader, d);

            reader.ForEachChild(r => TryReadRoleDescriptorChild(r, d), out var signature);
            d.Signature = signature;

            role = d;
            return(true);
        }
        public static bool TryReadFederationEndpointType(this XmlDictionaryReader reader, out FederationEndpointType type)
        {
            if (!reader.TryReadAttribute(XsiConstants.Attributes.Type, XsiConstants.Namespace, out var value))
            {
                return(Out.False(out type));
            }
            if (value.IndexOf(':') < 0)
            {
                return(Out.False(out type));
            }
            var split = value.Split(':');

            if (split.Length != 2)
            {
                return(Out.False(out type));
            }
            var prefix = split[0];
            var ns     = reader.LookupNamespace(prefix);

            if (string.IsNullOrEmpty(ns))
            {
                return(Out.False(out type));
            }
            if (ns != WsFederationConstants.Namespace)
            {
                return(Out.False(out type));
            }

            var typeName = split[1];

            if (typeName.EndsWith("Type"))
            {
                typeName = typeName.Remove(typeName.Length - 4);
            }

            return(Enum.TryParse(typeName, out type));
        }