Skip to content

Commit

Permalink
The message handler gets passed to the Start method
Browse files Browse the repository at this point in the history
Putting it in the constructor didn't make sense, because some message handlers may not be usable with DI.
  • Loading branch information
fubar-coder committed May 1, 2019
1 parent bf0ae62 commit e1cea6b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 16 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ The following example creates a PAM transaction and tries to authenticate a user
// Create the service
var pamService = new PamService(
// The service options
new OptionsWrapper<PamSessionConfiguration>(new PamSessionConfiguration()),
// The PAM message handler
new ConsoleMessageHandler());
new OptionsWrapper<PamSessionConfiguration>(new PamSessionConfiguration()));

// The PAM message handler
var messageHandler = new ConsoleMessageHandler();

// Create a PAM transaction
using (var pamTransaction = pamService.Start())
using (var pamTransaction = pamService.Start(messageHandler))
{
// Change the user prompt
pamTransaction.UserPrompt = "my customized user login prompt: ";
Expand Down
3 changes: 2 additions & 1 deletion samples/LoginSharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ private static void ExecuteLogin()
while (true)
{
var service = services.GetRequiredService<IPamService>();
using var transaction = service.Start();
var messageHandler = services.GetRequiredService<IPamMessageHandler>();
using var transaction = service.Start(messageHandler);
transaction.UserPrompt = $"{hostName} login: ";
transaction.UserName = null;

Expand Down
5 changes: 4 additions & 1 deletion src/FubarDev.PamSharp/IPamService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ public interface IPamService
/// <summary>
/// Initializes a PAM transaction.
/// </summary>
/// <param name="messageHandler">The message handler.</param>
/// <param name="user">The user name.</param>
/// <returns>The new PAM transaction.</returns>
IPamTransaction Start(string? user = null);
IPamTransaction Start(
IPamMessageHandler messageHandler,
string? user = null);
}
}
13 changes: 5 additions & 8 deletions src/FubarDev.PamSharp/PamService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ namespace FubarDev.PamSharp
/// </summary>
public class PamService : IPamService
{
private readonly IPamMessageHandler _messageHandler;

private readonly ILogger<PamService>? _logger;

private readonly PamSessionConfiguration _configuration;
Expand All @@ -27,23 +25,22 @@ public class PamService : IPamService
/// Initializes a new instance of the <see cref="PamService"/> class.
/// </summary>
/// <param name="options">The options for the PAM service.</param>
/// <param name="messageHandler">The message handler.</param>
/// <param name="logger">The logger.</param>
public PamService(
IOptions<PamSessionConfiguration> options,
IPamMessageHandler messageHandler,
ILogger<PamService>? logger = null)
{
_messageHandler = messageHandler;
_logger = logger;
_configuration = options.Value;
}

/// <inheritdoc/>
public IPamTransaction Start(string? user = null)
public IPamTransaction Start(
IPamMessageHandler messageHandler,
string? user = null)
{
var conversationHandler = _configuration.CreateMessaging?.Invoke(_messageHandler)
?? new PamConversationHandler(_messageHandler);
var conversationHandler = _configuration.CreateMessaging?.Invoke(messageHandler)
?? new PamConversationHandler(messageHandler);

PamStatus ConversationCallback(int messageCount, IntPtr messages, out IntPtr responseArrayPtr, IntPtr appDataPtr)
{
Expand Down
6 changes: 4 additions & 2 deletions test/FubarDev.PamSharp.Tests/PamTransactionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ public void CanRequestTransaction()
IPamTransaction pamTransaction;
using (var scope = _serviceProvider.CreateScope())
{
var messageHandler = scope.ServiceProvider.GetRequiredService<IPamMessageHandler>();
var service = scope.ServiceProvider.GetRequiredService<IPamService>();
pamTransaction = service.Start();
pamTransaction = service.Start(messageHandler);
}

Assert.NotEqual(IntPtr.Zero, pamTransaction.Handle);
Expand All @@ -43,8 +44,9 @@ public void CanStartAndStopTransaction()
{
using (var scope = _serviceProvider.CreateScope())
{
var messageHandler = scope.ServiceProvider.GetRequiredService<IPamMessageHandler>();
var service = scope.ServiceProvider.GetRequiredService<IPamService>();
using (service.Start())
using (service.Start(messageHandler))
{
// Do nothing
}
Expand Down

0 comments on commit e1cea6b

Please sign in to comment.