PlainMechanism.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace FastReport.Messaging.Authentication.Sasl
  5. {
  6. /// <summary>
  7. /// The PLAIN SASL authentication mechanism.
  8. /// </summary>
  9. public class PlainMechanism : SaslMechanism
  10. {
  11. #region Constants
  12. /// <summary>
  13. /// The mechanism name.
  14. /// </summary>
  15. public const string MECHANISM_NAME = "PLAIN";
  16. #endregion // Constants
  17. #region Constructors
  18. /// <summary>
  19. /// Initializes a new instance of the <see cref="PlainMechanism"/> class.
  20. /// </summary>
  21. /// <param name="username">The username.</param>
  22. /// <param name="password">The user's password.</param>
  23. public PlainMechanism(string username, string password)
  24. {
  25. Name = MECHANISM_NAME;
  26. Username = username;
  27. Password = password;
  28. }
  29. #endregion // Constructors
  30. #region Protected Methods
  31. /// <summary>
  32. /// Computes the client response for server challenge.
  33. /// </summary>
  34. /// <param name="challenge">The challenge from server. Usually empty for PLAIN mechanism.</param>
  35. /// <returns>The response from client.</returns>
  36. protected override byte[] ComputeResponse(byte[] challenge)
  37. {
  38. if (String.IsNullOrEmpty(Username) || String.IsNullOrEmpty(Password))
  39. {
  40. return null;
  41. }
  42. return Encoding.UTF8.GetBytes("\0" + Username + "\0" + Password);
  43. }
  44. #endregion // Protected Methods
  45. }
  46. }