Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): Allow users to be set by command line args #1420

Merged
merged 1 commit into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() ?? "" };
}
}
}