Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
AuthenticationMechanism.cs
1using System;
2using System.Collections.Generic;
3using System.Net.Security;
4using System.Security.Cryptography;
5using System.Text;
6using System.Threading.Tasks;
8
10{
15 {
16 private static readonly RandomNumberGenerator rnd = RandomNumberGenerator.Create();
17
22 {
23 }
24
28 public abstract string Name
29 {
30 get;
31 }
32
36 public abstract int Weight
37 {
38 get;
39 }
40
46 public abstract bool Allowed(SslStream SslStream);
47
53 protected KeyValuePair<string, string>[] ParseCommaSeparatedParameterList(string s)
54 {
55 List<KeyValuePair<string, string>> Result = new List<KeyValuePair<string, string>>();
56 StringBuilder sb = new StringBuilder();
57 string Key = string.Empty;
58 int State = 0;
59
60 foreach (char ch in s)
61 {
62 switch (State)
63 {
64 case 0: // ID
65 if (ch == '=')
66 {
67 Key = sb.ToString();
68 sb.Clear();
69 State++;
70 }
71 else if (ch == ',')
72 {
73 Result.Add(new KeyValuePair<string, string>(sb.ToString(), string.Empty));
74 sb.Clear();
75 }
76 else
77 sb.Append(ch);
78 break;
79
80 case 1: // Value, first character
81 if (ch == '"')
82 State += 2;
83 else if (ch == ',')
84 {
85 Result.Add(new KeyValuePair<string, string>(Key, string.Empty));
86 sb.Clear();
87 State = 0;
88 Key = string.Empty;
89 }
90 else
91 {
92 sb.Append(ch);
93 State++;
94 }
95 break;
96
97 case 2: // Value, following characters
98 if (ch == ',')
99 {
100 Result.Add(new KeyValuePair<string, string>(Key, sb.ToString()));
101 sb.Clear();
102 State = 0;
103 Key = string.Empty;
104 }
105 else
106 sb.Append(ch);
107 break;
108
109 case 3: // Value, between quotes
110 if (ch == '"')
111 State--;
112 else if (ch == '\\')
113 State++;
114 else
115 sb.Append(ch);
116 break;
117
118 case 4: // Escaped character
119 sb.Append(ch);
120 State--;
121 break;
122 }
123 }
124
125 if (State == 2 && !string.IsNullOrEmpty(Key))
126 Result.Add(new KeyValuePair<string, string>(Key, sb.ToString()));
127
128 return Result.ToArray();
129 }
130
136 protected static byte[] CONCAT(params byte[][] Data)
137 {
138 int c = 0;
139
140 foreach (byte[] Part in Data)
141 c += Part.Length;
142
143 int i = 0;
144 int j;
145 byte[] Result = new byte[c];
146
147 foreach (byte[] Part in Data)
148 {
149 j = Part.Length;
150 Array.Copy(Part, 0, Result, i, j);
151 i += j;
152 }
153
154 return Result;
155 }
156
162 protected static string CONCAT(params string[] Parameters)
163 {
164 StringBuilder sb = new StringBuilder();
165
166 foreach (string s in Parameters)
167 sb.Append(s);
168
169 return sb.ToString();
170 }
171
178 protected static byte[] CONCAT(byte[] Data, params string[] Parameters)
179 {
180 return CONCAT(Data, Encoding.UTF8.GetBytes(CONCAT(Parameters)));
181 }
182
188 protected static string HEX(byte[] Data)
189 {
190 return Hashes.BinaryToString(Data);
191 }
192
200 protected static byte[] XOR(byte[] U1, byte[] U2)
201 {
202 int i, c = U1.Length;
203 if (U2.Length != c)
204 throw new Exception("Arrays must be of the same size.");
205
206 byte[] Response = new byte[c];
207
208 for (i = 0; i < c; i++)
209 Response[i] = (byte)(U1[i] ^ U2[i]);
210
211 return Response;
212 }
213
221 public abstract Task<bool?> AuthenticationRequest(string Data, ISaslServerSide Connection, ISaslPersistenceLayer PersistenceLayer);
222
230 public abstract Task<bool?> ResponseRequest(string Data, ISaslServerSide Connection, ISaslPersistenceLayer PersistenceLayer);
231
237 public abstract Task Initialize();
238
246 public abstract Task<bool?> Authenticate(string UserName, string Password, ISaslClientSide Connection);
247
253 protected static byte[] GetRandomBytes(int Count)
254 {
255 if (Count < 0)
256 throw new ArgumentException("Count must be positive.", nameof(Count));
257
258 byte[] Result = new byte[Count];
259
260 lock(rnd)
261 {
262 rnd.GetBytes(Result);
263 }
264
265 return Result;
266 }
267 }
268}
Base class for all authentication mechanisms.
static string HEX(byte[] Data)
Converts a byte array to a hexadecimal string.
abstract Task< bool?> AuthenticationRequest(string Data, ISaslServerSide Connection, ISaslPersistenceLayer PersistenceLayer)
Authentication request has been made.
static byte[] CONCAT(byte[] Data, params string[] Parameters)
Concatenates a byte array with a sequence of strings.
static byte[] CONCAT(params byte[][] Data)
Concatenates a sequence of byte arrays.
abstract int Weight
Weight of mechanisms. The higher the value, the more preferred.
abstract bool Allowed(SslStream SslStream)
Checks if a mechanism is allowed during the current conditions.
abstract Task< bool?> ResponseRequest(string Data, ISaslServerSide Connection, ISaslPersistenceLayer PersistenceLayer)
Response request has been made.
abstract Task Initialize()
Performs intitialization of the mechanism. Can be used to set static properties that will be used thr...
static byte[] XOR(byte[] U1, byte[] U2)
XORs two byte arrays.
static string CONCAT(params string[] Parameters)
Concatenates a sequence of strings.
AuthenticationMechanism()
Base class for all authentication mechanisms.
KeyValuePair< string, string >[] ParseCommaSeparatedParameterList(string s)
Parses a parameter list in a challenge string.
abstract Task< bool?> Authenticate(string UserName, string Password, ISaslClientSide Connection)
Authenticates the user using the provided credentials.
static byte[] GetRandomBytes(int Count)
Gets an array of random bytes.
abstract string Name
Name of the mechanism.
Contains methods for simple hash calculations.
Definition: Hashes.cs:59
static string BinaryToString(byte[] Data)
Converts an array of bytes to a string with their hexadecimal representations (in lower case).
Definition: Hashes.cs:65
Interface for authentication mechanisms.
Interface for client-side client connections.
Interface for XMPP Server persistence layers. The persistence layer should implement caching.
Interface for server-side client connections.