/// <summary>
        /// Validates the attribute.
        /// </summary>
        /// <remarks>
        /// [SAML2.0 standard] section 2.7.3.1
        /// </remarks>
        /// <param name="samlAttribute">The SAML attribute.</param>
        public void ValidateAttribute(SamlAttribute samlAttribute)
        {
            if (samlAttribute == null)
            {
                throw new ArgumentNullException("samlAttribute");
            }

            if (!Saml20Utils.ValidateRequiredString(samlAttribute.Name))
            {
                throw new Saml20FormatException("Name attribute of Attribute element MUST contain at least one non-whitespace character");
            }

            if (samlAttribute.AttributeValue != null)
            {
                foreach (object o in samlAttribute.AttributeValue)
                {
                    if (o == null)
                    {
                        throw new Saml20FormatException("null-AttributeValue elements are not supported");
                    }
                }
            }

            if (samlAttribute.AnyAttr != null)
            {
                _anyAttributeValidator.ValidateXmlAnyAttributes(samlAttribute.AnyAttr);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Adds an attribute by name using the specified name format.
        /// </summary>
        /// <param name="attrName">Name of the attribute.</param>
        /// <param name="nameFormat">The name format of the attribute.</param>
        public void AddAttribute(string attrName, Saml20NameFormat nameFormat)
        {
            if (_attributes.Any(at => at.Name == attrName && at.NameFormat == GetNameFormat(nameFormat)))
            {
                throw new InvalidOperationException(string.Format("An attribute with name \"{0}\" and name format \"{1}\" has already been added", attrName, Enum.GetName(typeof(Saml20NameFormat), nameFormat)));
            }

            var attr = new SamlAttribute
                           {
                               Name = attrName,
                               NameFormat = GetNameFormat(nameFormat)
                           };

            _attributes.Add(attr);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Assembles our basic test assertion
        /// </summary>
        /// <returns>The <see cref="Assertion"/>.</returns>
        public static Assertion GetBasicAssertion()
        {
            var assertion = new Assertion
                                {
                                    Issuer = new NameId(),
                                    Id = "_b8977dc86cda41493fba68b32ae9291d",
                                    IssueInstant = DateTime.UtcNow,
                                    Version = "2.0"
                                };

            assertion.Issuer.Value = GetBasicIssuer();
            assertion.Subject = new Subject();
            var subjectConfirmation = new SubjectConfirmation
            {
                Method = SubjectConfirmation.BearerMethod,
                SubjectConfirmationData =
                    new SubjectConfirmationData
                    {
                        NotOnOrAfter = new DateTime(2008, 12, 31, 12, 0, 0, 0),
                        Recipient = "http://borger.dk"
                    }
            };
            assertion.Subject.Items = new object[] { subjectConfirmation };
            assertion.Conditions = new Conditions { NotOnOrAfter = new DateTime(2008, 12, 31, 12, 0, 0, 0) };
            var audienceRestriction = new AudienceRestriction { Audience = GetAudiences().Select(u => u.ToString()).ToList() };
            assertion.Conditions.Items = new List<ConditionAbstract>(new ConditionAbstract[] { audienceRestriction });

            AuthnStatement authnStatement;
            {
                authnStatement = new AuthnStatement();
                assertion.Items = new StatementAbstract[] { authnStatement };
                authnStatement.AuthnInstant = new DateTime(2008, 1, 8);
                authnStatement.SessionIndex = "70225885";
                authnStatement.AuthnContext = new AuthnContext
                                                  {
                                                      Items = new object[]
                                                                  {
                                                                      "urn:oasis:names:tc:SAML:2.0:ac:classes:X509",
                                                                      "http://www.safewhere.net/authncontext/declref"
                                                                  },
                                                      ItemsElementName = new[]
                                                                             {
                                                                                 AuthnContextType.AuthnContextClassRef,
                                                                                 AuthnContextType.AuthnContextDeclRef
                                                                             }
                                                  };
            }

            AttributeStatement attributeStatement;
            {
                attributeStatement = new AttributeStatement();
                var surName = new SamlAttribute
                    {
                        FriendlyName = "SurName",
                        Name = "urn:oid:2.5.4.4",
                        NameFormat = SamlAttribute.NameformatUri,
                        AttributeValue = new[] { "Fry" }
                    };

                var commonName = new SamlAttribute
                    {
                        FriendlyName = "CommonName",
                        Name = "urn:oid:2.5.4.3",
                        NameFormat = SamlAttribute.NameformatUri,
                        AttributeValue = new[] { "Philip J. Fry" }
                    };

                var userName = new SamlAttribute
                    {
                        Name = "urn:oid:0.9.2342.19200300.100.1.1",
                        NameFormat = SamlAttribute.NameformatUri,
                        AttributeValue = new[] { "fry" }
                    };

                var email = new SamlAttribute
                    {
                        FriendlyName = "Email",
                        Name = "urn:oid:0.9.2342.19200300.100.1.3",
                        NameFormat = SamlAttribute.NameformatUri,
                        AttributeValue = new[] { "*****@*****.**" }
                    };

                attributeStatement.Items = new object[] { surName, commonName, userName, email };
            }

            assertion.Items = new StatementAbstract[] { authnStatement, attributeStatement };

            return assertion;
        }