public async Task GetDeviceIdentityTest(
            string iotHubHostName,
            string clientId,
            string username,
            string password,
            X509Certificate2 certificate,
            IList <X509Certificate2> chain,
            bool authRetVal,
            Type expectedType)
        {
            var authenticator    = new Mock <IAuthenticator>();
            var productInfoStore = Mock.Of <IProductInfoStore>();

            authenticator.Setup(a => a.AuthenticateAsync(It.IsAny <IClientCredentials>())).ReturnsAsync(authRetVal);
            var deviceIdentityProvider = new DeviceIdentityProvider(authenticator.Object, new ClientCredentialsFactory(new IdentityProvider(iotHubHostName)), productInfoStore, true);

            if (certificate != null)
            {
                deviceIdentityProvider.RegisterConnectionCertificate(certificate, chain);
            }

            IDeviceIdentity deviceIdentity = await deviceIdentityProvider.GetAsync(clientId, username, password, null);

            Assert.IsAssignableFrom(expectedType, deviceIdentity);
        }
 public void ParseUsernameTest(string username, string expectedDeviceId, string expectedModuleId, string expectedDeviceClientType)
 {
     (string deviceId, string moduleId, string deviceClientType) = DeviceIdentityProvider.ParseUserName(username);
     Assert.Equal(expectedDeviceId, deviceId);
     Assert.Equal(expectedModuleId, moduleId);
     Assert.Equal(expectedDeviceClientType, deviceClientType);
 }
Esempio n. 3
0
        static async Task <IClientCredentials> GetClientCredentials(string iotHubHostName, string deviceId, string userName, string token, bool isCertAuthAllowed = false, string productInfo = "")
        {
            var authenticator            = Mock.Of <IAuthenticator>(a => a.AuthenticateAsync(It.IsAny <IClientCredentials>()) == Task.FromResult(true));
            var factory                  = new ClientCredentialsFactory(iotHubHostName, productInfo);
            var sasTokenIdentityProvider = new DeviceIdentityProvider(authenticator, factory, isCertAuthAllowed);

            ProtocolGateway.Identity.IDeviceIdentity deviceIdentity = await sasTokenIdentityProvider.GetAsync(deviceId, userName, token, null);

            Assert.NotNull(deviceIdentity);
            IClientCredentials clientCredentials = (deviceIdentity as ProtocolGatewayIdentity)?.ClientCredentials;

            return(clientCredentials);
        }
        public async Task GetDeviceIdentityTest(
            string iotHubHostName,
            string clientId,
            string username,
            string password,
            bool authRetVal,
            Type expectedType)
        {
            var authenticator = new Mock <IAuthenticator>();

            authenticator.Setup(a => a.AuthenticateAsync(It.IsAny <IClientCredentials>())).ReturnsAsync(authRetVal);

            IDeviceIdentityProvider deviceIdentityProvider = new DeviceIdentityProvider(authenticator.Object, new ClientCredentialsFactory(iotHubHostName), false);
            IDeviceIdentity         deviceIdentity         = await deviceIdentityProvider.GetAsync(clientId, username, password, null);

            Assert.IsAssignableFrom(expectedType, deviceIdentity);
        }
Esempio n. 5
0
        static async Task <IClientCredentials> GetClientCredentials(string iotHubHostName, string deviceId, string userName, string token, bool isCertAuthAllowed = false, string productInfo = "", X509Certificate2 certificate = null, IList <X509Certificate2> chain = null)
        {
            var authenticator = Mock.Of <IAuthenticator>(a => a.AuthenticateAsync(It.IsAny <IClientCredentials>()) == Task.FromResult(true));
            var factory       = new ClientCredentialsFactory(new IdentityProvider(iotHubHostName), productInfo);
            var credentialIdentityProvider = new DeviceIdentityProvider(authenticator, factory, isCertAuthAllowed);

            if ((certificate != null) && (chain != null))
            {
                credentialIdentityProvider.RemoteCertificateValidationCallback(certificate, chain);
            }
            ProtocolGateway.Identity.IDeviceIdentity deviceIdentity = await credentialIdentityProvider.GetAsync(deviceId, userName, token, null);

            Assert.NotNull(deviceIdentity);
            IClientCredentials clientCredentials = (deviceIdentity as ProtocolGatewayIdentity)?.ClientCredentials;

            return(clientCredentials);
        }
        public async Task GetIdentityCertAuthNotEnabled()
        {
            string iotHubHostName = "foo.azure-devices.net";
            var    authenticator  = new Mock <IAuthenticator>();

            authenticator.Setup(a => a.AuthenticateAsync(It.IsAny <IClientCredentials>())).ReturnsAsync(true);
            var deviceIdentityProvider = new DeviceIdentityProvider(authenticator.Object, new ClientCredentialsFactory(new IdentityProvider(iotHubHostName)), false);

            deviceIdentityProvider.RegisterConnectionCertificate(new X509Certificate2(), new List <X509Certificate2> {
                new X509Certificate2()
            });
            IDeviceIdentity deviceIdentity = await deviceIdentityProvider.GetAsync(
                "Device_2",
                $"127.0.0.1/Device_2/api-version=2016-11-14&DeviceClientType={Uri.EscapeDataString("Microsoft.Azure.Devices.Client/1.2.2")}",
                null,
                null);

            Assert.Equal(UnauthenticatedDeviceIdentity.Instance, deviceIdentity);
        }
Esempio n. 7
0
        static async Task <IClientCredentials> GetClientCredentials(string iotHubHostName, string deviceId, string userName, string token, bool isCertAuthAllowed = false, string productInfo = "", X509Certificate2 certificate = null, IList <X509Certificate2> chain = null, IMetadataStore metadataStore = null)
        {
            metadataStore = metadataStore ?? Mock.Of <IMetadataStore>();
            var authenticator              = Mock.Of <IAuthenticator>(a => a.AuthenticateAsync(It.IsAny <IClientCredentials>()) == Task.FromResult(true));
            var usernameParser             = new MqttUsernameParser();
            var factory                    = new ClientCredentialsFactory(new IdentityProvider(iotHubHostName), productInfo);
            var credentialIdentityProvider = new DeviceIdentityProvider(authenticator, usernameParser, factory, metadataStore, isCertAuthAllowed);

            if (certificate != null && chain != null)
            {
                credentialIdentityProvider.RegisterConnectionCertificate(certificate, chain);
            }

            IDeviceIdentity deviceIdentity = await credentialIdentityProvider.GetAsync(deviceId, userName, token, null);

            Assert.NotNull(deviceIdentity);
            IClientCredentials clientCredentials = (deviceIdentity as ProtocolGatewayIdentity)?.ClientCredentials;

            return(clientCredentials);
        }
 public void ParseUserNameErrorTest(string username)
 {
     Assert.Throws <EdgeHubConnectionException>(() => DeviceIdentityProvider.ParseUserName(username));
 }
        public void ParseUsernameTest(string username, string expectedDeviceId, string expectedModuleId, string expectedDeviceClientType, string expectedModelId)
        {
            (string deviceId, string moduleId, string deviceClientType, Option <string> modelId) = DeviceIdentityProvider.ParseUserName(username);
            Assert.Equal(expectedDeviceId, deviceId);
            Assert.Equal(expectedModuleId, moduleId);
            Assert.Equal(expectedDeviceClientType, deviceClientType);
            if (!string.IsNullOrEmpty(expectedModelId))
            {
                Assert.True(modelId.HasValue);
            }

            modelId.ForEach(mId => Assert.Equal(expectedModelId, mId));
        }
Esempio n. 10
0
 public void ParseUsernameTest(string username, string expectedDeviceId, string expectedModuleId, string expectedDeviceClientType, string expectedModelId)
 {
     (string deviceId, string moduleId, string deviceClientType, Option <string> modelId) = DeviceIdentityProvider.ParseUserName(username);
     Assert.Equal(expectedDeviceId, deviceId);
     Assert.Equal(expectedModuleId, moduleId);
     Assert.Equal(expectedDeviceClientType, deviceClientType);
     modelId.ForEach(mId => Assert.Equal(expectedModelId, mId));
 }