Skip to content

Commit a0e8207

Browse files
committed
Add simple authentication plugin registry and extensibility API.
Signed-off-by: Bradley Grainger <[email protected]>
1 parent 5a28a6d commit a0e8207

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace MySqlConnector.Authentication
5+
{
6+
/// <summary>
7+
/// A registry of known authentication plugins.
8+
/// </summary>
9+
public static class AuthenticationPlugins
10+
{
11+
/// <summary>
12+
/// Registers the specified authentication plugin. The name of this plugin must be unique.
13+
/// </summary>
14+
/// <param name="plugin">The authentication plugin.</param>
15+
public static void Register(IAuthenticationPlugin plugin)
16+
{
17+
if (plugin is null)
18+
throw new ArgumentNullException(nameof(plugin));
19+
if (string.IsNullOrEmpty(plugin.Name))
20+
throw new ArgumentException("Invalid plugin name.", nameof(plugin));
21+
22+
lock (s_lock)
23+
s_plugins.Add(plugin.Name, plugin);
24+
}
25+
26+
internal static bool TryGetPlugin(string name, out IAuthenticationPlugin plugin)
27+
{
28+
lock (s_lock)
29+
return s_plugins.TryGetValue(name, out plugin);
30+
}
31+
32+
static readonly object s_lock = new object();
33+
static readonly Dictionary<string, IAuthenticationPlugin> s_plugins = new Dictionary<string, IAuthenticationPlugin>();
34+
}
35+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
3+
namespace MySqlConnector.Authentication
4+
{
5+
/// <summary>
6+
/// The primary interface implemented by an authentication plugin.
7+
/// </summary>
8+
public interface IAuthenticationPlugin
9+
{
10+
/// <summary>
11+
/// The authentication plugin name.
12+
/// </summary>
13+
string Name { get; }
14+
15+
/// <summary>
16+
/// Creates the authentication response.
17+
/// </summary>
18+
/// <param name="password">The client's password.</param>
19+
/// <param name="authenticationData">The authentication data supplied by the server; this is the <code>auth method data</code>
20+
/// from the <a href="https://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::AuthSwitchRequest">Authentication
21+
/// Method Switch Request Packet</a>.</param>
22+
/// <returns></returns>
23+
byte[] CreateResponse(string password, ReadOnlySpan<byte> authenticationData);
24+
}
25+
}

0 commit comments

Comments
 (0)