using System.Collections.Generic; namespace Jwt { /// /// Builder for objects. /// public class JwtBuilder { private readonly JwtData _data = new JwtData(); /// /// Adds the specified payload to the JWT. /// /// The payload. /// The instance. public JwtBuilder WithPayload(object payload) { _data.Payload = payload; return this; } /// /// Marks the specified payload as already serialized. /// /// The instance. public JwtBuilder IsSerialized() { _data.Serialized = true; return this; } /// /// Adds the specified key string to the JWT. /// /// The string representation of the key. /// The instance. public JwtBuilder WithKey(string key) { _data.Key = key; return this; } /// /// Adds the specified key bytes to the JWT. /// /// The bytes representation of the key. /// The instance. public JwtBuilder WithKey(byte[] keyBytes) { _data.KeyBytes = keyBytes; return this; } /// /// Specifies the algorithm being used for hashing the JWT. /// /// The algorithm being used for hashing the JWT. /// The instance. public JwtBuilder WithAlgorithm(JwtHashAlgorithm algorithm) { _data.Algorithm = algorithm; return this; } /// /// Adds the specified key/value pair as header to the JWT. /// /// The key of the key/value pair. /// The value of the key/value pair. /// The instance. public JwtBuilder WithHeader(string key, object value) { if (_data.ExtraHeaders == null) { _data.ExtraHeaders = new Dictionary(); } _data.ExtraHeaders.Add(key, value); return this; } /// /// Adds the specified dictionary as headers to the JWT. /// /// The dictionary with the headers of the JWT. /// The instance. public JwtBuilder WithHeaders(IDictionary dict) { _data.ExtraHeaders = dict; return this; } /// /// Builds the data to a object. /// /// A object. public JwtData Build() => _data; /// /// Builds and encodes the current object. /// /// An encoded JSON Web Token. public string Encode() => JsonWebToken.Encode(Build()); } }