2using System.Collections.Generic;
3using System.Reflection;
24 public abstract string Name
40 public virtual IEnumerable<KeyValuePair<string, object>>
PublicWebKey
42 get {
throw new NotSupportedException(
"Algorithm does not have a public web key."); }
58 public virtual void Sign(IEnumerable<KeyValuePair<string, object>> Header,
59 IEnumerable<KeyValuePair<string, object>> Payload, out
string HeaderString,
60 out
string PayloadString, out
string Signature)
63 bool HasKeyID =
false;
65 foreach (KeyValuePair<string, object> P
in Header)
77 new KeyValuePair<string, object>(
"alg", this.
Name),
78 new KeyValuePair<string, object>(
"jwk", this.
PublicWebKey));
81 HeaderJson =
JSON.
Encode(Header,
null,
new KeyValuePair<string, object>(
"alg", this.
Name));
83 byte[] HeaderBin = Encoding.UTF8.GetBytes(HeaderJson);
86 string PayloadJson = Payload is
null ? string.Empty :
JSON.
Encode(Payload,
null);
87 byte[] PayloadBin = Encoding.UTF8.GetBytes(PayloadJson);
90 Signature = this.
Sign(HeaderString, PayloadString);
99 public abstract string Sign(
string HeaderEncoded,
string PayloadEncoded);
108 public virtual bool IsValid(
string HeaderEncoded,
string PayloadEncoded,
string SignatureEncoded)
110 return this.
Sign(HeaderEncoded, PayloadEncoded) == SignatureEncoded;
135 if (algorithms.ContainsKey(Algorithm.Name))
136 Log.
Warning(
"JWS algorithm with name " + Algorithm.Name +
" already registered.");
138 algorithms[Algorithm.Name] = Algorithm;
148 Types.OnInvalidated += Types_OnInvalidated;
155 return algorithms.TryGetValue(
Name, out Algorithm);
159 private static void Types_OnInvalidated(
object Sender, EventArgs e)
168 private static readonly Dictionary<string, IJwsAlgorithm> algorithms =
new Dictionary<string, IJwsAlgorithm>();
169 private static bool initialized =
false;
170 private static bool registered =
false;
Static class that does BASE64URL encoding (using URL and filename safe alphabet), as defined in RFC46...
static string Encode(byte[] Data)
Converts a binary block of data to a Base64URL-encoded string.
Helps with common JSON-related tasks.
static string Encode(string s)
Encodes a string for inclusion in JSON.
Static class managing the application event log. Applications and services log events on this static ...
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
static void Warning(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs a warning event.
Static class that dynamically manages types and interfaces available in the runtime environment.
static object[] NoParameters
Contains an empty array of parameter values.
static Type[] GetTypesImplementingInterface(string InterfaceFullName)
Gets all types implementing a given interface.
static ConstructorInfo GetDefaultConstructor(Type Type)
Gets the default constructor of a type, if one exists.
Abstract base class for JWS algorithm.
const string JwsContentType
application/jose+json
virtual IEnumerable< KeyValuePair< string, object > > PublicWebKey
The public JSON web key, if supported.
virtual void Sign(IEnumerable< KeyValuePair< string, object > > Header, IEnumerable< KeyValuePair< string, object > > Payload, out string HeaderString, out string PayloadString, out string Signature)
Signs data.
abstract string Sign(string HeaderEncoded, string PayloadEncoded)
Signs data.
static bool TryGetAlgorithm(string Name, out IJwsAlgorithm Algorithm)
Gets the JWS algoritm that corresponds to a given algorithm name.
abstract string Name
Short name for algorithm.
abstract void Dispose()
IDisposable.Dispose
virtual bool IsValid(string HeaderEncoded, string PayloadEncoded, string SignatureEncoded)
Checks if a signature is valid.
abstract bool HasPublicWebKey
If the algorithm has a public key.
Abstract base class for JWS algorithm.