Skip to content

Commit

Permalink
Fix Sasl Plain Authenticator (#903)
Browse files Browse the repository at this point in the history
* Fix Sasl Plain Authenticator

* Add comment
  • Loading branch information
varunpuranik authored Mar 11, 2019
1 parent afabd86 commit bb6c327
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
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] != '.')
{
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);
}
}
}

0 comments on commit bb6c327

Please sign in to comment.