SaslMechanism.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace FastReport.Messaging.Authentication.Sasl
  5. {
  6. /// <summary>
  7. /// The base abstarct class for all SASL mechanisms.
  8. /// </summary>
  9. public abstract class SaslMechanism
  10. {
  11. #region Fields
  12. private string name;
  13. private string username;
  14. private string password;
  15. #endregion // Fields
  16. #region Properties
  17. /// <summary>
  18. /// Gets or sets the name of mechanism.
  19. /// </summary>
  20. public string Name
  21. {
  22. get { return name; }
  23. protected set { name = value; }
  24. }
  25. /// <summary>
  26. /// Gets or sets the username.
  27. /// </summary>
  28. public string Username
  29. {
  30. get { return username; }
  31. set { username = value; }
  32. }
  33. /// <summary>
  34. /// Gets or sets the user's password.
  35. /// </summary>
  36. public string Password
  37. {
  38. get { return password; }
  39. set { password = value; }
  40. }
  41. #endregion // Properties
  42. #region Constructors
  43. /// <summary>
  44. /// Initializes a new instance of the <see cref="SaslMechanism"/> class.
  45. /// </summary>
  46. public SaslMechanism()
  47. {
  48. name = "";
  49. username = "";
  50. password = "";
  51. }
  52. /// <summary>
  53. /// Initializes a new instance of the <see cref="SaslMechanism"/> class with specified parameters.
  54. /// </summary>
  55. /// <param name="username">The username.</param>
  56. /// <param name="password">The user's password.</param>
  57. public SaslMechanism(string username, string password)
  58. {
  59. this.name = "";
  60. this.username = username;
  61. this.password = password;
  62. }
  63. #endregion // Constructors
  64. #region Protected Methods
  65. /// <summary>
  66. /// Computes client response for server challenge.
  67. /// </summary>
  68. /// <param name="challenge">The server challenge.</param>
  69. /// <returns>The client response.</returns>
  70. protected abstract byte[] ComputeResponse(byte[] challenge);
  71. #endregion // Protected Methods
  72. #region Public Methods
  73. /// <summary>
  74. /// Gets the base64-encoded client response fo the server challenge.
  75. /// </summary>
  76. /// <param name="challenge">The base64-string containing server challenge.</param>
  77. /// <returns>The base64-string containing client response.</returns>
  78. public string GetResponse(string challenge)
  79. {
  80. byte[] data = Convert.FromBase64String(challenge);
  81. byte[] response = ComputeResponse(data);
  82. return Convert.ToBase64String(response);
  83. }
  84. /// <summary>
  85. /// Gets the client response for the server challenge.
  86. /// </summary>
  87. /// <param name="challenge">Byte array containing server challenge.</param>
  88. /// <returns>Byte array containing client response.</returns>
  89. public byte[] GetResponse(byte[] challenge)
  90. {
  91. return ComputeResponse(challenge);
  92. }
  93. #endregion // Public Methods
  94. }
  95. }