Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SaslModule.cs
1using System;
2using System.Collections.Generic;
3using System.Reflection;
4using System.Threading.Tasks;
5using Waher.Events;
7
9{
13 public class SaslModule : IModule
14 {
15 private static IAuthenticationMechanism[] mechanisms = Array.Empty<IAuthenticationMechanism>();
16
20 public async Task Start()
21 {
22 mechanisms = await GetMechanisms();
23 Types.OnInvalidated += Types_OnInvalidated;
24 }
25
29 public Task Stop()
30 {
31 Types.OnInvalidated -= Types_OnInvalidated;
32 mechanisms = Array.Empty<IAuthenticationMechanism>();
33 return Task.CompletedTask;
34 }
35
39 public static IAuthenticationMechanism[] Mechanisms => mechanisms;
40
41 private static async void Types_OnInvalidated(object Sender, EventArgs e)
42 {
43 try
44 {
45 mechanisms = await GetMechanisms();
46 }
47 catch (Exception ex)
48 {
49 Log.Exception(ex);
50 }
51 }
52
53 private static async Task<IAuthenticationMechanism[]> GetMechanisms()
54 {
55 Dictionary<string, bool> MechanismsFound = new Dictionary<string, bool>();
56 List<IAuthenticationMechanism> Result = new List<IAuthenticationMechanism>();
57 ConstructorInfo CI;
59 Type[] MechanismTypes = Types.GetTypesImplementingInterface(typeof(IAuthenticationMechanism));
60
61 foreach (Type T in MechanismTypes)
62 {
63 if (T.IsAbstract)
64 continue;
65
66 CI = T.GetConstructor(Types.NoTypes);
67 if (CI is null)
68 continue;
69
70 try
71 {
72 Mechanism = (IAuthenticationMechanism)CI.Invoke(Types.NoParameters);
73
74 if (MechanismsFound.ContainsKey(Mechanism.Name))
75 throw new Exception("Authentication mechanism collision." + T.FullName + ": " + Mechanism.Name);
76
77 await Mechanism.Initialize();
78
79 MechanismsFound[Mechanism.Name] = true;
80 Result.Add(Mechanism);
81 }
82 catch (Exception ex)
83 {
84 Log.Exception(ex);
85 }
86 }
87
88 Result.Sort((m1, m2) => m2.Weight - m1.Weight);
89
90 return Result.ToArray();
91 }
92 }
93}
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
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.
Definition: Log.cs:1647
Module maintaining available SASL mechanisms.
Definition: SaslModule.cs:14
static IAuthenticationMechanism[] Mechanisms
Available SASL mechanisms.
Definition: SaslModule.cs:39
async Task Start()
Starts the module.
Definition: SaslModule.cs:20
Task Stop()
Stops the module.
Definition: SaslModule.cs:29
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:14
static Type[] NoTypes
Contains an empty array of types.
Definition: Types.cs:543
static object[] NoParameters
Contains an empty array of parameter values.
Definition: Types.cs:548
static Type[] GetTypesImplementingInterface(string InterfaceFullName)
Gets all types implementing a given interface.
Definition: Types.cs:84
Interface for authentication mechanisms.
Task Initialize()
Performs intitialization of the mechanism. Can be used to set static properties that will be used thr...
Interface for late-bound modules loaded at runtime.
Definition: IModule.cs:9