public async Task AuthenticateFromCacheTest()
        {
            // Arrange
            var cloudProxy        = Mock.Of <ICloudProxy>(c => c.IsActive && c.OpenAsync() == Task.FromResult(true));
            var connectionManager = Mock.Of <IConnectionManager>(
                c =>
                c.CreateCloudConnectionAsync(It.IsAny <IClientCredentials>()) == Task.FromResult(Try.Success(cloudProxy)));

            string iothubHostName    = "iothub1.azure.net";
            string callerProductInfo = "productInfo";
            string sasToken          = TokenHelper.CreateSasToken($"{iothubHostName}/devices/device1/modules/moduleId");
            var    identity          = Mock.Of <IIdentity>(i => i.Id == "d1");
            var    credentials       = new TokenCredentials(identity, sasToken, callerProductInfo);

            var storedTokenCredentials = Mock.Of <ITokenCredentials>(c => c.Token == sasToken);
            var credentialsStore       = new Mock <ICredentialsStore>();

            credentialsStore.Setup(c => c.Get(It.IsAny <IIdentity>()))
            .ReturnsAsync(Option.Some((IClientCredentials)storedTokenCredentials));
            credentialsStore.Setup(c => c.Add(It.IsAny <IClientCredentials>()))
            .Returns(Task.CompletedTask);

            var tokenCredentialsAuthenticator = new TokenCredentialsAuthenticator(connectionManager, credentialsStore.Object, iothubHostName);

            // Act
            bool isAuthenticated = await tokenCredentialsAuthenticator.AuthenticateAsync(credentials);

            // assert
            Assert.True(isAuthenticated);
            Mock.Verify(credentialsStore);
            Mock.Get(connectionManager).Verify(c => c.CreateCloudConnectionAsync(It.IsAny <IClientCredentials>()), Times.Never);
            Mock.Get(cloudProxy).Verify(c => c.OpenAsync(), Times.Never);
        }
예제 #2
0
        protected override void Load(ContainerBuilder builder)
        {
            // Task<IAuthenticator>
            builder.Register(async c =>
            {
                var connectionManager = c.Resolve <IConnectionManager>();
                ICredentialsStore credentialsStore = await c.Resolve <Task <ICredentialsStore> >();
                var tokenCredentialsAuthenticator  = new TokenCredentialsAuthenticator(connectionManager, credentialsStore, this.iothubHostName);
                return(new Authenticator(tokenCredentialsAuthenticator, this.deviceId) as IAuthenticator);
            })
            .As <Task <IAuthenticator> >()
            .SingleInstance();

            // IClientCredentialsFactory
            builder.Register(c => new ClientCredentialsFactory(this.iothubHostName, this.productInfo))
            .As <IClientCredentialsFactory>()
            .SingleInstance();

            base.Load(builder);
        }