/// <summary> /// Constructor. /// Builds a new "auth" element with the given XML representation /// </summary> /// <param name="xml">The XML representation of this element</param> public AuthenticationElement(XmlNode xml) : base(xml) { if(xml.FirstChild.Attributes["mechanism"] == null) throw new OpenXMPPException("The server is not sending standard SASL authentication elements! I cann't determine an authentication mechanism"); // Parse authorization mechanism from XML switch(xml.FirstChild.Attributes["mechanism"].Value) { case "DIGEST-MD5": this.mechanism = SaslAuthenticationMechanism.DIGEST_MD5; break; case "PLAIN": this.mechanism = SaslAuthenticationMechanism.PLAIN; break; default: this.mechanism = SaslAuthenticationMechanism.UNKNOWN; break; } // Parse encoded username and password from XML string authStr = Encoding.ASCII.GetString(Convert.FromBase64String(xml.InnerText)); string[] authParams = authStr.Split('@', '\x00'); this.domain = authParams[1]; this.username = authParams[2]; this.password = authParams[3]; }
/// <summary> /// Constructor. /// Builds a new AuthorizationStanza object with the given xml. Additional parameters are to avoid parsing overhead. /// </summary> /// <param name="xml">XML representation of this stanza.</param> /// <param name="mechanism">The SASL authorization mechanism specified in this stanza.</param> /// <param name="domain">The user's authorization domain.</param> /// <param name="username">The user's name.</param> /// <param name="password">The user's password.</param> public AuthenticationElement(XmlNode xml, SaslAuthenticationMechanism mechanism, string domain, string username, string password) : base(xml) { this.mechanism = mechanism; this.domain = domain; this.username = username; this.password = password; }
/// <summary> /// Creates a new stream element for user authentication. /// </summary> /// <param name="mech">The SASL authentication mechanism to use.</param> /// <param name="domain">The user's authentication domain (for PLAIN authentication)</param> /// <param name="username">The user's name (for PLAIN authentication)</param> /// <param name="password">The user's password (for PLAIN authentication)</param> /// <returns>A new authorization stanza for the given SASL authentication mechanism.</returns> public static AuthenticationElement GetAuthenticationElement(SaslAuthenticationMechanism mech, string domain, string username, string password) { StringWriter xmlString = new StringWriter(); XmlTextWriter writer = new XmlTextWriter(xmlString); writer.WriteStartElement("auth"); writer.WriteAttributeString("xmlns", SASL_NAMESPACE); switch(mech) { case SaslAuthenticationMechanism.DIGEST_MD5: writer.WriteAttributeString("mechanism", "DIGEST-MD5"); break; case SaslAuthenticationMechanism.PLAIN: writer.WriteAttributeString("mechanism", "PLAIN"); string authStr = string.Format("{0}@{1}\x00{2}\x00{3}", username, domain, username, password); byte[] bytes = Encoding.UTF8.GetBytes(authStr); writer.WriteBase64(bytes, 0, bytes.Length); break; default: throw new OpenXMPPException("Cannot create authorization stanza for mechanism "+mech+"."); } writer.WriteEndElement(); XmlDocument xml = new XmlDocument(); xml.LoadXml(xmlString.ToString()); return new AuthenticationElement(xml.DocumentElement, mech, domain, username, password); }