private static ExtensionsType GetAuthnRequestExtensions(AuthenticationRequest authenticationRequest)
        {
            ExtensionsType result = new ExtensionsType();

            List <System.Xml.XmlElement> resultList = new List <System.Xml.XmlElement>();

            if (!String.IsNullOrEmpty(authenticationRequest.DocumentId))
            {
                var documentAttribute = new BankId.Merchant.Library.Xml.Schemas.Saml.Assertion.AttributeType()
                {
                    Name           = SamlAttribute.DocumentId,
                    AttributeValue = new object[] { authenticationRequest.DocumentId }
                };

                resultList.Add(GetXmlElement(documentAttribute.Serialize()));
            }

            if (resultList.Count == 0)
            {
                return(null);
            }

            result.Any = resultList.ToArray();

            return(result);
        }
Esempio n. 2
0
        /// <summary>
        /// Generates a SAML v2.0 Authentication Request with HTTP Browser Post Binding.
        /// The return string containing the request is NOT Base64 encoded.
        /// </summary>
        /// <param name="linkIDContext">the linkID authentication/payment configuration</param>
        /// <returns>SAML request</returns>
        public static AuthnRequestType generateAuthnRequest(LinkIDAuthenticationContext linkIDContext)
        {
            AuthnRequestType authnRequest = new AuthnRequestType();

            authnRequest.ForceAuthn   = true;
            authnRequest.ID           = Guid.NewGuid().ToString();
            authnRequest.Version      = "2.0";
            authnRequest.IssueInstant = DateTime.UtcNow;

            NameIDType issuer = new NameIDType();

            issuer.Value        = linkIDContext.applicationName;
            authnRequest.Issuer = issuer;

            NameIDPolicyType nameIdPolicy = new NameIDPolicyType();

            nameIdPolicy.AllowCreate          = true;
            nameIdPolicy.AllowCreateSpecified = true;
            authnRequest.NameIDPolicy         = nameIdPolicy;

            Dictionary <string, string> deviceContextMap = linkIDContext.getDeviceContextMap();
            DeviceContextType           deviceContext    = null;

            if (null != deviceContextMap && deviceContextMap.Count > 0)
            {
                deviceContext = new DeviceContextType();
                List <AttributeType> attributes = new List <AttributeType>();
                foreach (string deviceContextKey in deviceContextMap.Keys)
                {
                    string        deviceContextValue = deviceContextMap[deviceContextKey];
                    AttributeType attribute          = new AttributeType();
                    attribute.Name           = deviceContextKey;
                    attribute.AttributeValue = new object[] { deviceContextValue };
                    attributes.Add(attribute);
                    deviceContext.Items = attributes.ToArray();
                }
            }
            SubjectAttributesType subjectAttributes = null;

            if (null != linkIDContext.attributeSuggestions && linkIDContext.attributeSuggestions.Count > 0)
            {
                subjectAttributes = new SubjectAttributesType();
                List <AttributeType> attributes = new List <AttributeType>();
                foreach (string attributeName in linkIDContext.attributeSuggestions.Keys)
                {
                    List <object> values = linkIDContext.attributeSuggestions[attributeName];

                    AttributeType attribute = new AttributeType();
                    attribute.Name           = attributeName;
                    attribute.AttributeValue = values.ToArray();
                    attributes.Add(attribute);
                    subjectAttributes.Items = attributes.ToArray();
                }
            }

            PaymentContextType paymentContextType = null;

            if (null != linkIDContext.paymentContext)
            {
                Dictionary <String, String> paymentContextDict = linkIDContext.paymentContext.toDictionary();
                paymentContextType = new PaymentContextType();
                List <AttributeType> attributes = new List <AttributeType>();
                foreach (string paymentContextKey in paymentContextDict.Keys)
                {
                    string        value     = paymentContextDict[paymentContextKey];
                    AttributeType attribute = new AttributeType();
                    attribute.Name           = paymentContextKey;
                    attribute.AttributeValue = new object[] { value };
                    attributes.Add(attribute);
                    paymentContextType.Items = attributes.ToArray();
                }
            }

            CallbackType callbackType = null;

            if (null != linkIDContext.callback)
            {
                Dictionary <String, String> callbackDict = linkIDContext.callback.toDictionary();
                callbackType = new CallbackType();
                List <AttributeType> attributes = new List <AttributeType>();
                foreach (string callbackKey in callbackDict.Keys)
                {
                    string        value     = callbackDict[callbackKey];
                    AttributeType attribute = new AttributeType();
                    attribute.Name           = callbackKey;
                    attribute.AttributeValue = new object[] { value };
                    attributes.Add(attribute);
                    callbackType.Items = attributes.ToArray();
                }
            }


            if (null != deviceContext || null != subjectAttributes || null != paymentContextType || null != callbackType)
            {
                ExtensionsType    extensions     = new ExtensionsType();
                List <XmlElement> extensionsList = new List <XmlElement>();
                if (null != subjectAttributes)
                {
                    extensionsList.Add(toXmlElement(subjectAttributes));
                }
                if (null != deviceContext)
                {
                    extensionsList.Add(toXmlElement(deviceContext));
                }
                if (null != paymentContextType)
                {
                    extensionsList.Add(toXmlElement(paymentContextType));
                }
                if (null != callbackType)
                {
                    extensionsList.Add(toXmlElement(callbackType));
                }
                extensions.Any          = extensionsList.ToArray();
                authnRequest.Extensions = extensions;
            }

            return(authnRequest);
        }