JwtBuilder.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System.Collections.Generic;
  2. namespace Jwt
  3. {
  4. /// <summary>
  5. /// Builder for <see cref="JwtData"/> objects.
  6. /// </summary>
  7. public class JwtBuilder
  8. {
  9. private readonly JwtData _data = new JwtData();
  10. /// <summary>
  11. /// Adds the specified payload to the JWT.
  12. /// </summary>
  13. /// <param name="payload">The payload.</param>
  14. /// <returns>The <see cref="JwtBuilder"/> instance.</returns>
  15. public JwtBuilder WithPayload(object payload)
  16. {
  17. _data.Payload = payload;
  18. return this;
  19. }
  20. /// <summary>
  21. /// Marks the specified payload as already serialized.
  22. /// </summary>
  23. /// <returns>The <see cref="JwtBuilder"/> instance.</returns>
  24. public JwtBuilder IsSerialized()
  25. {
  26. _data.Serialized = true;
  27. return this;
  28. }
  29. /// <summary>
  30. /// Adds the specified key string to the JWT.
  31. /// </summary>
  32. /// <param name="key">The string representation of the key.</param>
  33. /// <returns>The <see cref="JwtBuilder"/> instance.</returns>
  34. public JwtBuilder WithKey(string key)
  35. {
  36. _data.Key = key;
  37. return this;
  38. }
  39. /// <summary>
  40. /// Adds the specified key bytes to the JWT.
  41. /// </summary>
  42. /// <param name="keyBytes">The bytes representation of the key.</param>
  43. /// <returns>The <see cref="JwtBuilder"/> instance.</returns>
  44. public JwtBuilder WithKey(byte[] keyBytes)
  45. {
  46. _data.KeyBytes = keyBytes;
  47. return this;
  48. }
  49. /// <summary>
  50. /// Specifies the algorithm being used for hashing the JWT.
  51. /// </summary>
  52. /// <param name="algorithm">The algorithm being used for hashing the JWT.</param>
  53. /// <returns>The <see cref="JwtBuilder"/> instance.</returns>
  54. public JwtBuilder WithAlgorithm(JwtHashAlgorithm algorithm)
  55. {
  56. _data.Algorithm = algorithm;
  57. return this;
  58. }
  59. /// <summary>
  60. /// Adds the specified key/value pair as header to the JWT.
  61. /// </summary>
  62. /// <param name="key">The key of the key/value pair.</param>
  63. /// <param name="value">The value of the key/value pair.</param>
  64. /// <returns>The <see cref="JwtBuilder"/> instance.</returns>
  65. public JwtBuilder WithHeader(string key, object value)
  66. {
  67. if (_data.ExtraHeaders == null)
  68. {
  69. _data.ExtraHeaders = new Dictionary<string, object>();
  70. }
  71. _data.ExtraHeaders.Add(key, value);
  72. return this;
  73. }
  74. /// <summary>
  75. /// Adds the specified dictionary as headers to the JWT.
  76. /// </summary>
  77. /// <param name="dict">The dictionary with the headers of the JWT.</param>
  78. /// <returns>The <see cref="JwtBuilder"/> instance.</returns>
  79. public JwtBuilder WithHeaders(IDictionary<string, object> dict)
  80. {
  81. _data.ExtraHeaders = dict;
  82. return this;
  83. }
  84. /// <summary>
  85. /// Builds the data to a <see cref="JwtData"/> object.
  86. /// </summary>
  87. /// <returns>A <see cref="JwtData"/> object.</returns>
  88. public JwtData Build() => _data;
  89. /// <summary>
  90. /// Builds and encodes the current <see cref="JwtBuilder"/> object.
  91. /// </summary>
  92. /// <returns>An encoded JSON Web Token.</returns>
  93. public string Encode() => JsonWebToken.Encode(Build());
  94. }
  95. }