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

Fix Sasl Plain Authenticator #903

Merged
merged 8 commits into from
Mar 11, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ public async Task<IPrincipal> AuthenticateAsync(string identity, string password
throw new EdgeHubConnectionException("Identity does not contain device ID.");
}

if (!this.iotHubHostName.Equals(iotHubName))
// iotHubName can be a segment of the full iotHubHostName.
// For example, if iotHubHostName = testhub1.azure-devices.net,
// then iotHubName = testhub1 is valid.
if (!this.iotHubHostName.StartsWith(iotHubName, StringComparison.OrdinalIgnoreCase) ||
this.iotHubHostName[iotHubName.Length] != '.')
{
varunpuranik marked this conversation as resolved.
Show resolved Hide resolved
throw new EdgeHubConnectionException($"Identity contains an invalid IotHubHostName {iotHubName}, expected value {this.iotHubHostName}.");
throw new EdgeHubConnectionException($"Identity contains an invalid IotHubHostName {iotHubName}.");
}

// TODO: Figure out where the device client type parameter value should come from.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ public async void TestAuthFailed()

[Fact]
[Unit]
public async void TestAuthSucceeds()
public async void TestAuthSucceeds_Module()
{
var authenticator = Mock.Of<IAuthenticator>();
var clientCredentialsFactory = Mock.Of<IClientCredentialsFactory>();
var saslAuthenticator = new EdgeSaslPlainAuthenticator(authenticator, clientCredentialsFactory, "hub1");
var saslAuthenticator = new EdgeSaslPlainAuthenticator(authenticator, clientCredentialsFactory, "hub1.azure-devices.net");
var identity = new ModuleIdentity("hub1", "dev1", "mod1");
var clientCredentials = Mock.Of<IClientCredentials>(c => c.Identity == identity);
const string UserId = "dev1/modules/[email protected]";
Expand All @@ -109,5 +109,32 @@ public async void TestAuthSucceeds()
bool isAuthenticated = await amqpAuthenticator.AuthenticateAsync("dev1/mod1");
Assert.True(isAuthenticated);
}

[Fact]
[Unit]
public async void TestAuthSucceeds_Device()
{
var authenticator = Mock.Of<IAuthenticator>();
var clientCredentialsFactory = Mock.Of<IClientCredentialsFactory>();
var saslAuthenticator = new EdgeSaslPlainAuthenticator(authenticator, clientCredentialsFactory, "hub1.azure-devices.net");
var identity = new DeviceIdentity("hub1", "dev1");
var clientCredentials = Mock.Of<IClientCredentials>(c => c.Identity == identity);
const string UserId = "[email protected]";
const string Password = "pwd";

Mock.Get(clientCredentialsFactory).Setup(f => f.GetWithSasToken("dev1", string.Empty, string.Empty, Password, false))
.Returns(clientCredentials);
Mock.Get(authenticator).Setup(a => a.AuthenticateAsync(clientCredentials))
.ReturnsAsync(true);

IPrincipal principal = await saslAuthenticator.AuthenticateAsync(UserId, Password);
Assert.NotNull(principal);

var amqpAuthenticator = principal as IAmqpAuthenticator;
Assert.NotNull(amqpAuthenticator);

bool isAuthenticated = await amqpAuthenticator.AuthenticateAsync("dev1");
Assert.True(isAuthenticated);
}
}
}