Skip to content

Commit e1cea6b

Browse files
committed
The message handler gets passed to the Start method
Putting it in the constructor didn't make sense, because some message handlers may not be usable with DI.
1 parent bf0ae62 commit e1cea6b

File tree

5 files changed

+20
-16
lines changed

5 files changed

+20
-16
lines changed

README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ The following example creates a PAM transaction and tries to authenticate a user
1414
// Create the service
1515
var pamService = new PamService(
1616
// The service options
17-
new OptionsWrapper<PamSessionConfiguration>(new PamSessionConfiguration()),
18-
// The PAM message handler
19-
new ConsoleMessageHandler());
17+
new OptionsWrapper<PamSessionConfiguration>(new PamSessionConfiguration()));
18+
19+
// The PAM message handler
20+
var messageHandler = new ConsoleMessageHandler();
2021

2122
// Create a PAM transaction
22-
using (var pamTransaction = pamService.Start())
23+
using (var pamTransaction = pamService.Start(messageHandler))
2324
{
2425
// Change the user prompt
2526
pamTransaction.UserPrompt = "my customized user login prompt: ";

samples/LoginSharp/Program.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ private static void ExecuteLogin()
5757
while (true)
5858
{
5959
var service = services.GetRequiredService<IPamService>();
60-
using var transaction = service.Start();
60+
var messageHandler = services.GetRequiredService<IPamMessageHandler>();
61+
using var transaction = service.Start(messageHandler);
6162
transaction.UserPrompt = $"{hostName} login: ";
6263
transaction.UserName = null;
6364

src/FubarDev.PamSharp/IPamService.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ public interface IPamService
1212
/// <summary>
1313
/// Initializes a PAM transaction.
1414
/// </summary>
15+
/// <param name="messageHandler">The message handler.</param>
1516
/// <param name="user">The user name.</param>
1617
/// <returns>The new PAM transaction.</returns>
17-
IPamTransaction Start(string? user = null);
18+
IPamTransaction Start(
19+
IPamMessageHandler messageHandler,
20+
string? user = null);
1821
}
1922
}

src/FubarDev.PamSharp/PamService.cs

+5-8
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ namespace FubarDev.PamSharp
1717
/// </summary>
1818
public class PamService : IPamService
1919
{
20-
private readonly IPamMessageHandler _messageHandler;
21-
2220
private readonly ILogger<PamService>? _logger;
2321

2422
private readonly PamSessionConfiguration _configuration;
@@ -27,23 +25,22 @@ public class PamService : IPamService
2725
/// Initializes a new instance of the <see cref="PamService"/> class.
2826
/// </summary>
2927
/// <param name="options">The options for the PAM service.</param>
30-
/// <param name="messageHandler">The message handler.</param>
3128
/// <param name="logger">The logger.</param>
3229
public PamService(
3330
IOptions<PamSessionConfiguration> options,
34-
IPamMessageHandler messageHandler,
3531
ILogger<PamService>? logger = null)
3632
{
37-
_messageHandler = messageHandler;
3833
_logger = logger;
3934
_configuration = options.Value;
4035
}
4136

4237
/// <inheritdoc/>
43-
public IPamTransaction Start(string? user = null)
38+
public IPamTransaction Start(
39+
IPamMessageHandler messageHandler,
40+
string? user = null)
4441
{
45-
var conversationHandler = _configuration.CreateMessaging?.Invoke(_messageHandler)
46-
?? new PamConversationHandler(_messageHandler);
42+
var conversationHandler = _configuration.CreateMessaging?.Invoke(messageHandler)
43+
?? new PamConversationHandler(messageHandler);
4744

4845
PamStatus ConversationCallback(int messageCount, IntPtr messages, out IntPtr responseArrayPtr, IntPtr appDataPtr)
4946
{

test/FubarDev.PamSharp.Tests/PamTransactionTests.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ public void CanRequestTransaction()
2727
IPamTransaction pamTransaction;
2828
using (var scope = _serviceProvider.CreateScope())
2929
{
30+
var messageHandler = scope.ServiceProvider.GetRequiredService<IPamMessageHandler>();
3031
var service = scope.ServiceProvider.GetRequiredService<IPamService>();
31-
pamTransaction = service.Start();
32+
pamTransaction = service.Start(messageHandler);
3233
}
3334

3435
Assert.NotEqual(IntPtr.Zero, pamTransaction.Handle);
@@ -43,8 +44,9 @@ public void CanStartAndStopTransaction()
4344
{
4445
using (var scope = _serviceProvider.CreateScope())
4546
{
47+
var messageHandler = scope.ServiceProvider.GetRequiredService<IPamMessageHandler>();
4648
var service = scope.ServiceProvider.GetRequiredService<IPamService>();
47-
using (service.Start())
49+
using (service.Start(messageHandler))
4850
{
4951
// Do nothing
5052
}

0 commit comments

Comments
 (0)