public async Task <IPrincipal> AuthenticateAsync(string identity, string password) { try { Preconditions.CheckNonWhiteSpace(identity, nameof(identity)); Preconditions.CheckNonWhiteSpace(password, nameof(password)); (string deviceId, string moduleId, string iotHubName) = SaslIdentity.Parse(identity); // we MUST have a device ID if (string.IsNullOrWhiteSpace(deviceId)) { throw new EdgeHubConnectionException("Identity does not contain device ID."); } // 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}."); } // TODO: Figure out where the device client type parameter value should come from. IClientCredentials deviceIdentity = this.clientCredentialsFactory.GetWithSasToken(deviceId, moduleId, string.Empty, password, false, Option.None <string>(), Option.None <string>()); if (!await this.authenticator.AuthenticateAsync(deviceIdentity)) { throw new EdgeHubConnectionException("Authentication failed."); } return(new SaslPrincipal(true, deviceIdentity)); } catch (Exception ex) when(!ex.IsFatal()) { Events.AuthenticationError(ex); throw; } }
public async Task <IPrincipal> AuthenticateAsync(string identity, string password) { try { Preconditions.CheckNonWhiteSpace(identity, nameof(identity)); Preconditions.CheckNonWhiteSpace(password, nameof(password)); (string deviceId, string moduleId, string iotHubName) = SaslIdentity.Parse(identity); // we MUST have a device ID if (string.IsNullOrWhiteSpace(deviceId)) { throw new EdgeHubConnectionException("Identity does not contain device ID."); } if (!this.iotHubHostName.Equals(iotHubName)) { throw new EdgeHubConnectionException($"Identity contains an invalid IotHubHostName {iotHubName}, expected value {this.iotHubHostName}."); } // TODO: Figure out where the device client type parameter value should come from. IClientCredentials deviceIdentity = this.clientCredentialsFactory.GetWithSasToken(deviceId, moduleId, string.Empty, password, false); if (!await this.authenticator.AuthenticateAsync(deviceIdentity)) { throw new EdgeHubConnectionException("Authentication failed."); } return(new SaslPrincipal(true, deviceIdentity)); } catch (Exception ex) when(!ex.IsFatal()) { Events.AuthenticationError(ex); throw; } }