Exemple #1
0
 internal ErrorResponse(SamlStatus samlStatus)
 {
     Exception             = null;
     ErrorMessage          = "SAML specific error.";
     ErrorCode             = ErrorDetails = SuggestedAction = ConsumerMessage = string.Empty;
     AdditionalInformation = samlStatus;
 }
Exemple #2
0
        internal ErrorResponse(AcquirerErrorRes errRes)
        {
            ErrorCode       = errRes.Error.errorCode;
            ErrorMessage    = errRes.Error.errorMessage;
            ErrorDetails    = errRes.Error.errorDetail;
            SuggestedAction = errRes.Error.suggestedAction;
            ConsumerMessage = errRes.Error.consumerMessage;
            Exception       = null;


            var samlXml = errRes.Error.container?.Any[0].OuterXml;

            if (string.IsNullOrWhiteSpace(samlXml))
            {
                return;
            }

            var responseType = ResponseType.Deserialize(samlXml);

            if (responseType.Status != null)
            {
                AdditionalInformation = new SamlStatus(responseType.Status.StatusMessage, responseType.Status?.StatusCode?.Value, responseType.Status?.StatusCode?.StatusCode?.Value);
            }
        }
Exemple #3
0
        private SamlResponse(ResponseType responseType, ICollection <SamlAttributesEncryptionKey> encryptedAttributesEncryptionKeys = null)
        {
            if (responseType == null)
            {
                throw new ArgumentNullException("responseType");
            }

            TransactionId     = responseType.ID;
            MerchantReference = responseType.InResponseTo;
            Version           = responseType.Version;
            AcquirerId        = responseType.Issuer.Value;

            if (responseType.Status != null)
            {
                if (responseType.Status.StatusCode.StatusCode == null)
                {
                    throw new CommunicatorException("Missing second level status code");
                }
                Status = new SamlStatus(responseType.Status.StatusMessage, responseType.Status?.StatusCode?.Value, responseType.Status?.StatusCode?.StatusCode?.Value);
            }

            if (responseType.Items == null)
            {
                AttributeStatements      = new ReadOnlyCollection <SamlAttribute>(new Collection <SamlAttribute>());
                AttributesEncryptionKeys = new ReadOnlyCollection <SamlAttributesEncryptionKey>((Collection <SamlAttributesEncryptionKey>)encryptedAttributesEncryptionKeys);
                return;
            }

            var attributeStatements = new Collection <SamlAttribute>();

            // extract Consumer.BIN attribute
            if (responseType.Items.Length > 0)
            {
                var itemsField = ((AssertionType)responseType.Items[0]).Subject.Items;
                if (itemsField != null && itemsField.Length > 0)
                {
                    var value = ((NameIDType)itemsField[0]).Value;
                    var encryptedSubjectAttribute =
                        encryptedAttributesEncryptionKeys.FirstOrDefault(attr => attr.AesKey != null && string.IsNullOrEmpty(attr.AttributeName));
                    if (value.StartsWith("TRANS"))
                    {
                        attributeStatements.Add(new SamlAttribute(SamlAttribute.ConsumerTransientID, value));
                        if (encryptedSubjectAttribute != null)
                        {
                            encryptedSubjectAttribute.AttributeName = SamlAttribute.ConsumerTransientID;
                        }
                    }
                    else
                    {
                        attributeStatements.Add(new SamlAttribute(SamlAttribute.ConsumerBin, value));
                        if (encryptedSubjectAttribute != null)
                        {
                            encryptedSubjectAttribute.AttributeName = SamlAttribute.ConsumerBin;
                        }
                    }
                }
            }

            // extract attribute values
            var assertionTypes = responseType.Items.Where(x => x.GetType().Name == "AssertionType").Select(x => (AssertionType)x).ToList();

            if (!assertionTypes.Any())
            {
                AttributeStatements      = new ReadOnlyCollection <SamlAttribute>(attributeStatements);
                AttributesEncryptionKeys = new ReadOnlyCollection <SamlAttributesEncryptionKey>((Collection <SamlAttributesEncryptionKey>)encryptedAttributesEncryptionKeys);
                return;
            }

            var attributeStatementTypes =
                assertionTypes.SelectMany(x => x.Items)
                .Where(x => x.GetType().Name == "AttributeStatementType")
                .Select(x => (AttributeStatementType)x);

            var attributeTypes =
                attributeStatementTypes.SelectMany(x => x.Items)
                .Where(x => x.GetType().Name == "AttributeType")
                .Select(x => (AttributeType)x);

            foreach (var attributeType in attributeTypes)
            {
                var values =
                    attributeType.AttributeValue.Where(x => x.GetType().Name == "XmlNode[]")
                    .SelectMany(x => (XmlNode[])x).Where(x => x.NodeType == XmlNodeType.Text)
                    .Select(x => x.Value);

                var value = string.Concat(values);
                if (string.IsNullOrEmpty(value) && attributeType.AttributeValue.Length > 0)
                {
                    var attributeValue = attributeType.AttributeValue.FirstOrDefault();
                    if (attributeValue?.GetType() != typeof(object))
                    {
                        value = attributeValue?.ToString();
                    }
                }

                attributeStatements.Add(new SamlAttribute(attributeType.Name, value));
            }

            AttributeStatements      = new ReadOnlyCollection <SamlAttribute>(attributeStatements);
            AttributesEncryptionKeys = new ReadOnlyCollection <SamlAttributesEncryptionKey>((Collection <SamlAttributesEncryptionKey>)encryptedAttributesEncryptionKeys);
        }