Skip to content

Commit

Permalink
Allow users to be set by command line
Browse files Browse the repository at this point in the history
  • Loading branch information
rnwood committed Apr 21, 2024
1 parent 8d60684 commit ae78ccb
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
13 changes: 13 additions & 0 deletions Rnwood.Smtp4dev.Tests/CommandLine/ParseCommandLineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,18 @@ public void SmtpPortMustBeValidTcpPort(int portNumber)
Action act = () => new ConfigurationBuilder().AddCommandLineOptions(commandLineOptions).Build().Bind(cmdLineOptions);
act.Should().Throw<TargetInvocationException>().WithInnerException<ArgumentOutOfRangeException>();
}

[Fact]
public void CanParseMultipleUsers()
{
var commandLineOptions = CommandLineParser.TryParseCommandLine(new[] { $"--user:u1=p1", "--user:u2=p2" }, false);
var cmdLineOptions = new CommandLineOptions();
new ConfigurationBuilder().AddCommandLineOptions(commandLineOptions).Build().Bind(cmdLineOptions);
cmdLineOptions.ServerOptions.Users.Length.Should().Be(2);
cmdLineOptions.ServerOptions.Users[0].Username.Should().Be("u1");
cmdLineOptions.ServerOptions.Users[0].Password.Should().Be("p1");
cmdLineOptions.ServerOptions.Users[1].Username.Should().Be("u2");
cmdLineOptions.ServerOptions.Users[1].Password.Should().Be("p2");
}
}
}
5 changes: 4 additions & 1 deletion Rnwood.Smtp4dev/CommandLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ public static MapOptions<CommandLineOptions> TryParseCommandLine(IEnumerable<str
{ "applicationName=","", data => map.Add(data, x => x.ApplicationName), true},
{ "authenticationrequired", "Requires that SMTP and IMAP clients authenticate", data => map.Add((data != null).ToString(), x => x.ServerOptions.AuthenticationRequired) },
{ "secureconnectionrequired", "Requires that SMTP clients use SSL/TLS", data => map.Add((data != null).ToString(), x => x.ServerOptions.SecureConnectionRequired) },
{ "webauthenticationrequired", "Require authentication for web interface", data => map.Add((data != null).ToString(), x => x.ServerOptions.WebAuthenticationRequired) }
{ "webauthenticationrequired", "Require authentication for web interface", data => map.Add((data != null).ToString(), x => x.ServerOptions.WebAuthenticationRequired) },
{ "user=", "Adds a user and password combination for web, SMTP and IMAP. Use format username=password. This option can be repeated to add multiple users.", data =>
map.Add(data, x => x.ServerOptions.Users)
}

};

Expand Down
2 changes: 1 addition & 1 deletion Rnwood.Smtp4dev/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"Rnwood.Smtp4dev": {
"commandName": "Project",
"commandLineArgs": "--hostname=BLAH --smtpport=2525",
"commandLineArgs": "--hostname=BLAH --smtpport=2525 --user:u1=p1 --user:u2=p2",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
Expand Down
25 changes: 24 additions & 1 deletion Rnwood.Smtp4dev/Server/Settings/User.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
namespace Rnwood.Smtp4dev.Server.Settings
using System;
using System.ComponentModel;
using System.Globalization;
using System.Linq;

namespace Rnwood.Smtp4dev.Server.Settings
{
[TypeConverter(typeof(UserFromStringConverter))]
public class User
{
public string Username { get; set; }
public string Password { get; set; }
}

public class UserFromStringConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string);
}

public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
string stringValue = value as string;

string[] values = stringValue.Split('=', 2);

return new User { Username = values[0], Password = values.Last() ?? "" };
}
}
}

0 comments on commit ae78ccb

Please sign in to comment.