Exemplo n.º 1
0
        public bool AuthenticateAnonymousDevice(string device_id, string device_token)
        {
            TableOperation retrieveOperation = TableOperation.Retrieve <DynamicTableEntity>(device_id,
                                                                                            device_token);

            try
            {
                DynamicTableEntity device_entity = (DynamicTableEntity)this.DevicesTable.Execute
                                                       (retrieveOperation).Result;
                if (device_entity == null)
                {
                    return(false);
                }
                else
                {
                    IStorageDevice device = this.DeviceEntityResolver(device_entity.PartitionKey,
                                                                      device_entity.RowKey, device_entity.Timestamp, device_entity.Properties,
                                                                      device_entity.ETag);
                    OverlordIdentity.InitializeDeviceIdentity(device_id, device_token,
                                                              device.Sensors.Select(s => s.Key).ToArray <string>());
                    return(true);
                }
            }
            catch (Exception e)
            {
                Log.ReadTableFailure(string.Format
                                         ("Failed to retrieve device entity: Id: {0}, Token: {1}.", device_id, device_token), e);
                throw;
            }
        }
        public IStorageDeviceReading AddDeviceReading(DateTime time, IDictionary <string, object> values)
        {
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.FindDevice);
            IStorageDevice device = this.GetCurrentDevice();

            return(this.AddDeviceReading(device, time, values));
        }
Exemplo n.º 3
0
        public IStorageDevice FindDevice(Guid id, string token)
        {
            TableOperation retrieveOperation = TableOperation.Retrieve <DynamicTableEntity>(id.ToUrn(),
                                                                                            token);

            try
            {
                DynamicTableEntity device_entity = (DynamicTableEntity)this.DevicesTable
                                                   .Execute(retrieveOperation).Result;
                if (device_entity == null)
                {
                    return(null);
                }
                return(this.DeviceEntityResolver(device_entity.PartitionKey, device_entity.RowKey,
                                                 device_entity.Timestamp, device_entity.Properties, device_entity.ETag));
            }
            catch (Exception e)
            {
                Log.ReadTableFailure(string.Format("Failed to read table for device: Id: {0}, Token: {1}.",
                                                   id.ToUrn(), token), e);
                throw;
            }
            finally
            {
                OverlordIdentity.DeleteClaim(Resource.Storage, StorageAction.FindDevice);
            }
        }
Exemplo n.º 4
0
        public IStorageUser FindUser(Guid id, string token)
        {
            TableOperation retrieveOperation = TableOperation.Retrieve <DynamicTableEntity>(id.ToUrn(), token);

            try
            {
                DynamicTableEntity user_entity = (DynamicTableEntity)this.UsersTable
                                                 .Execute(retrieveOperation).Result;
                if (user_entity == null)
                {
                    return(null);
                }
                else
                {
                    IStorageUser user = this.UserEntityResolver(user_entity.PartitionKey, user_entity.RowKey, user_entity.Timestamp,
                                                                user_entity.Properties, user_entity.ETag);

                    return(user);
                }
            }
            catch (Exception e)
            {
                Log.ReadTableFailure(string.Format("Failed to find user entity: Id: {0}, Token: {1}.",
                                                   id.ToUrn(), token), e);
                throw;
            }
            finally
            {
                OverlordIdentity.DeleteClaim(Resource.Storage, StorageAction.FindUser);
            }
        }
Exemplo n.º 5
0
        protected async Task AuthenticateAsync(string userName, string password, HttpAuthenticationContext context,
                                               CancellationToken cancellationToken)
        {
            if (userName == null || password == null)
            {
                context.ErrorResult = new AuthenticationFailureResult("Null authentication field.", context.Request);
                return;
            }

            Tuple <string, string, IList <string> > result = await Api.AuthenticateDeviceAsync(userName, password);

            if (result == null)
            {
                //Log.AuthenticateDevice(userName, password, false);
                context.ErrorResult = new AuthenticationFailureResult("Bad credentials.", context.Request);
                return;
            }
            else
            {
                context.Principal = OverlordIdentity.InitializeDevicePrincipal(result.Item1,
                                                                               result.Item2, result.Item3);
                //Log.AuthenticateDevice(userName, password, true);
                return;
            }
        }
Exemplo n.º 6
0
        public IStorageUser AddUser(string name, string token, GeoIp geo_ip, string id = null)
        {
            IStorageUser user = new IStorageUser()
            {
                Id       = string.IsNullOrEmpty(id) ? Guid.NewGuid() : id.UrnToGuid(),
                Token    = token,
                UserName = name,
                Devices  = new List <Guid>()
            };
            TableOperation insertOperation = TableOperation.Insert(AzureStorage.CreateUserTableEntity(user));
            TableResult    result;

            try
            {
                result = this.UsersTable.Execute(insertOperation);
                Log.WriteTableSuccess(string.Format("Added user entity: {0}, Id: {1}, Token {2}.",
                                                    user.UserName, user.Id.ToUrn(), user.Token));
                return(user);
            }
            catch (Exception e)
            {
                Log.WriteTableFailure(string.Format("Failed to add user entity: {0}, Id: {1}, Token {2}.",
                                                    user.UserName, user.Id.ToUrn(), user.Token), e);
                throw;
            }
            finally
            {
                OverlordIdentity.DeleteClaim(Resource.Storage, StorageAction.AddUser);
            }
        }
Exemplo n.º 7
0
        private IList <IStorageChannel> CreateSensorChannels()
        {
            OverlordIdentity.InitializeAnonymousIdentity();
            AzureStorage storage = new AzureStorage();

            storage.AuthenticateAnonymousDevice(TestData.device_02_id.UrnToId(),
                                                TestData.device_02_token);
            //OverlordIdentity.AddClaim(Resource.Storage, StorageAction.FindDevice);
            //IStorageDevice = storage.GetCurrentUser();

            IStorageAlert alert_1 = new IStorageAlert()
            {
                SensorType  = "I0",
                IntMaxValue = 100,
                IntMinValue = -10
            };
            IStorageAlert alert_2 = new IStorageAlert()
            {
                SensorType  = "I0",
                IntMaxValue = 40,
                IntMinValue = 0
            };

            List <IStorageAlert> alerts = new List <IStorageAlert>()
            {
                alert_1, alert_2
            };

            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddChannel);
            IStorageChannel channel = storage.AddChannel("xUnit_IngestTests_Channel_01",
                                                         "A test channel for xUnit AzureStorageIngestTests", "I0", "Test integer units",
                                                         alerts);

            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddChannel);
            alert_1 = new IStorageAlert()
            {
                SensorType  = "S0",
                StringValue = "Alert 1!"
            };
            alert_2 = new IStorageAlert()
            {
                SensorType  = "S0",
                StringValue = "Alert 1!"
            };

            alerts = new List <IStorageAlert>()
            {
                alert_1, alert_2
            };
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddChannel);
            IStorageChannel channel_2 = storage.AddChannel("xUnit_IngestTests_Channel_03",
                                                           "A test channel for xUnit AzureStorageIngestTests", "S0", "Test stringr units",
                                                           alerts);

            return(new List <IStorageChannel>()
            {
                channel, channel_2
            });
        }
        public void CanAuthorizeAuthenticateAnonymousUser()
        {
            AzureStorage storage = new AzureStorage();

            OverlordIdentity.InitializeUserIdentity(TestData.user_01_id.UrnToId(), "admin", new string[0]);
            Assert.Throws(typeof(System.Security.SecurityException),
                          () => storage.AuthenticateAnonymousUser(TestData.user_01_id, "admin"));
        }
Exemplo n.º 9
0
        public static void AddDeviceReading(DateTime time, IDictionary <string, object> values)
        {
            AzureStorage storage = new AzureStorage();

            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddDeviceReading);
            storage.AddDeviceReading(time, values);
            Log.AddDeviceReadingSuccess("Added device readings.");
        }
Exemplo n.º 10
0
        public void CanAuthorizeAddUser()
        {
            OverlordIdentity.InitializeUserIdentity(TestData.user_01_id.UrnToId(), "admin", new string[0]);
            AzureStorage storage = new AzureStorage();

            Assert.Throws(typeof(System.Security.SecurityException), () =>
                          storage.AddUser("XUnit_CanAuthorizeAddAzureStorageTests.user_Test_Name", "XUnit_CanAuthorizeAddAzureStorageTests.user_Test_Token",
                                          null));
        }
Exemplo n.º 11
0
        public void CanAuthenticateAnonymousDevice()
        {
            OverlordIdentity.InitializeAnonymousIdentity();
            AzureStorage storage = new AzureStorage();

            Assert.True(storage.AuthenticateAnonymousDevice(TestData.device_01_id.UrnToId(),
                                                            TestData.device_01_token));
            Assert.False(OverlordIdentity.HasClaim(Authentication.Role, UserRole.Anonymous));
            Assert.True(OverlordIdentity.HasClaim(Authentication.Role, UserRole.Device));
        }
Exemplo n.º 12
0
        public void CanAddUser()
        {
            OverlordIdentity.InitializeAdminUserIdentity(TestData.user_01_id,
                                                         TestData.user_01_token, new string[0]);
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddUser);
            AzureStorage storage = new AzureStorage();
            IStorageUser user    = storage.AddUser("xUnit_CanAddUserTest_Name", "xUnit_CanAddUserTest_Token", null);

            Assert.NotNull(user.Id);
            Assert.Equal("xUnit_CanAddUserTest_Name", user.UserName);
            Assert.Equal("xUnit_CanAddUserTest_Token", user.Token);
        }
Exemplo n.º 13
0
        public void CanAuthorizeDeleteUser()
        {
            OverlordIdentity.InitializeUserIdentity(TestData.user_01_id.UrnToId(), "admin", new string[0]);
            AzureStorage storage = new AzureStorage();

            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.FindUser);
            IStorageUser user = storage.FindUser("d155074f-4e85-4cb5-a597-8bfecb0dfc04".ToGuid(), "admin");

            Assert.Throws(typeof(System.Security.SecurityException), () => storage.DeleteUser(user));
            OverlordIdentity.InitializeAdminUserIdentity(TestData.user_01_id.UrnToId(), "admin", new string[0]);
            Assert.Throws(typeof(System.Security.SecurityException), () => storage.DeleteUser(user));
        }
Exemplo n.º 14
0
 public void CanInitializeIdentity()
 {
     Assert.False(OverlordIdentity.HasClaim(Authentication.Role, UserRole.Anonymous));
     OverlordIdentity.InitializeAnonymousIdentity();
     Assert.True(OverlordIdentity.HasClaim(Authentication.Role, UserRole.Anonymous));
     OverlordIdentity.InitializeDeviceIdentity(TestData.device_01_id.UrnToId(), TestData.device_01_token, new string[0]);
     Assert.False(OverlordIdentity.HasClaim(Authentication.Role, UserRole.Anonymous));
     Assert.True(OverlordIdentity.HasClaim(Authentication.Role, UserRole.Device));
     OverlordIdentity.InitializeUserIdentity(TestData.user_01_id.UrnToId(), TestData.user_01_token, new string[0]);
     Assert.False(OverlordIdentity.HasClaim(Authentication.Role, UserRole.Device));
     Assert.True(OverlordIdentity.HasClaim(Authentication.Role, UserRole.User));
 }
Exemplo n.º 15
0
        public void CanFindUser()
        {
            //InitialiseTestData();
            AzureStorage storage = new AzureStorage();

            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.FindUser);
            IStorageUser user = storage.FindUser(TestData.user_02_id.UrnToGuid(), TestData.user_02_token);

            Assert.NotNull(user);
            Assert.Equal(user.UserName, TestData.user_02_name);
            Assert.Equal(user.Token, TestData.user_02_token);
        }
Exemplo n.º 16
0
        public void CanAddDeviceReading()
        {
            Assert.True(Api.AuthenticateDevice(TestData.device_01_id, TestData.device_01_token));
            OverlordIdentity.AddClaim(Resource.Api, ApiAction.AddReading);
            DateTime time = TestData.GenerateRandomTime(null, null, null,
                                                        DateTime.Now.Hour);
            IDictionary <string, object> sensor_values = new Dictionary <string, object>()
            {
                { TestData.sensor_01_name, TestData.GenerateRandomString(43) }
            };

            Api.AddDeviceReading(time, sensor_values);
        }
Exemplo n.º 17
0
        public void CanAddSensor()
        {
            AzureStorage storage = new AzureStorage();

            OverlordIdentity.InitializeAnonymousIdentity();
            storage.AuthenticateAnonymousDevice(TestData.device_02_id.UrnToId(),
                                                TestData.device_02_token);
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddSensor);
            IStorageSensor sensor = storage.AddSensor(TestData.sensor_01_name, TestData.sensor_01_unit, null, null);

            Assert.NotNull(sensor);
            Assert.Equal(sensor.Name, TestData.sensor_01_name);
            Assert.Equal(sensor.Unit, TestData.sensor_01_unit);
        }
Exemplo n.º 18
0
        private void InitialiseTestData()
        {
            AzureStorage storage = new AzureStorage();

            OverlordIdentity.InitializeAdminUserIdentity(TestData.user_01_id, TestData.user_01_token, new string[0]);
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddUser);
            IStorageUser user_01 = storage.AddUser(TestData.user_01_name, TestData.user_01_token, null, TestData.user_01_id);

            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddUser);
            IStorageUser user_02 = storage.AddUser(TestData.user_02_name, TestData.user_02_token, null, TestData.user_02_id);

            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddUser);
            IStorageUser user_03 = storage.AddUser(TestData.user_03_name, TestData.user_03_token, null, TestData.user_03_id);
        }
Exemplo n.º 19
0
        public void CanFindDevice()
        {
            //InitialiseTestData();
            AzureStorage storage = new AzureStorage();

            OverlordIdentity.InitializeUserIdentity(TestData.user_02_id, TestData.user_02_token, new string[0]);
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.FindUser);
            IStorageUser user = storage.FindUser(TestData.user_01_id.UrnToGuid(), TestData.user_01_token);

            Assert.NotNull(user);
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.FindDevice);
            IStorageDevice device = storage.FindDevice(TestData.device_01_id.UrnToGuid(), TestData.device_01_token);

            Assert.NotNull(device);
        }
Exemplo n.º 20
0
        public bool AuthenticateAnonymousUser(string user_id, string user_token)
        {
            TableOperation retrieveOperation = TableOperation.Retrieve <DynamicTableEntity>(user_id,
                                                                                            user_token);

            try
            {
                DynamicTableEntity user_entity = (DynamicTableEntity)this.UsersTable.Execute
                                                     (retrieveOperation).Result;
                if (user_entity == null)
                {
                    return(false);
                }
                else
                {
                    IStorageUser user = this.UserEntityResolver(user_entity.PartitionKey,
                                                                user_entity.RowKey, user_entity.Timestamp, user_entity.Properties, user_entity.ETag);
                    OverlordIdentity.InitializeUserIdentity(user.Id.ToUrn(), user.Token,
                                                            user.Devices.Select(d => d.ToUrn()).ToList <String>());
                    return(true);
                }
            }
            catch (Exception e)
            {
                Log.ReadTableFailure(string.Format("Failed to read user entity: Id: {0}, Token: {1}.",
                                                   user_id, user_token), e);
                throw;
            }
            finally
            {
            }


            /*
             * TableOperation retrieveOperation = TableOperation.Retrieve<DynamicTableEntity>(urn_id, token);
             * TableQuery<DynamicTableEntity> query = new TableQuery<DynamicTableEntity>().Where(
             *  TableQuery.CombineFilters(
             *      TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, urn_id.UrnToId()),
             *      TableOperators.And,
             *      TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, token)
             *  )
             * );
             * IEnumerable<IStorageUser> user = this.UsersTable.ExecuteQuery<DynamicTableEntity, IStorageUser>(query, this.UserEntityResolverFunc);
             * return user.FirstOrDefault();
             */
            //return null;
        }
Exemplo n.º 21
0
        public void CanAddDeviceReadingParallel()
        {
            Dictionary <int, IStorageDevice> devices = new Dictionary <int, IStorageDevice>();

            OverlordIdentity.InitializeAnonymousIdentity();
            AzureStorage storage = new AzureStorage();

            storage.AuthenticateAnonymousUser(TestData.user_02_id.UrnToId(),
                                              TestData.user_02_token);
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.FindUser);
            IStorageUser user = storage.GetCurrentUser();

            for (int i = 0; i <= 5; i++)

            {
                OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddDevice);
                IStorageDevice device = storage.AddDevice(user, "xUnit_CanAddDevicereadingParallel_Device_" + i.ToString(),
                                                          "xUnit_CanAddDevicereadingParallel_Device_Token", null, null);
                devices.Add(i, device);
            }
            OverlordIdentity.InitializeAnonymousIdentity();
            Parallel.For(0, devices.Count, d =>
            {
                Log.Partition();
                storage.AuthenticateAnonymousDevice(devices[d].Id.ToUrn(), devices[0].Token);
                IDictionary <string, object> sensor_values = TestData.GenerateRandomSensorData(10);
                OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddDeviceReading);
                storage.AddDeviceReading(TestData.GenerateRandomTime(null, null, null, null),
                                         sensor_values);
                //Sleep for a random interval
                Thread.Sleep(TestData.GenerateRandomInteger(0, 1000));

                //Add another set of sensor data
                OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddDeviceReading);
                storage.AddDeviceReading(TestData.GenerateRandomTime(null, null, null, null),
                                         TestData.GenerateRandomSensorData(sensor_values));

                //Sleep for a random interval
                Thread.Sleep(TestData.GenerateRandomInteger(0, 1000));

                //Add another set of sensor data
                OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddDeviceReading);
                storage.AddDeviceReading(TestData.GenerateRandomTime(null, null, null, null),
                                         TestData.GenerateRandomSensorData(sensor_values));
            });
        }
Exemplo n.º 22
0
        public void CanAuthorizeAddSensor()
        {
            AzureStorage storage = new AzureStorage();

            Assert.Throws(typeof(System.Security.SecurityException), () =>
                          storage.AddSensor("foo", "bar", null, null));

            //Throws security exception even if correct identity.
            OverlordIdentity.InitializeDeviceIdentity(TestData.device_01_id.UrnToId(), TestData.device_01_token, new string[0]);
            Assert.Throws(typeof(System.Security.SecurityException), () =>
                          storage.AddSensor("foo", "bar", null, null));

            //Doesn't throw security exception when proper permission is present.
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddSensor);
            IStorageSensor s = storage.AddSensor(TestData.sensor_01_name, "bar", null, null);

            Assert.NotNull(s);
            Assert.True(s.Name == TestData.sensor_01_name);
        }
Exemplo n.º 23
0
        public IStorageSensor AddSensor(string sensor_name, string sensor_units,
                                        IList <Guid> sensor_channels, IList <IStorageAlert> sensor_alerts)
        {
            if (!sensor_name.IsVaildSensorName())
            {
                throw new ArgumentException(
                          string.Format("Invalid sensor name: {0}", sensor_name));
            }
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.FindDevice);
            IStorageDevice device = this.GetCurrentDevice();
            IStorageSensor sensor = new IStorageSensor()
            {
                DeviceId = device.Id,
                Name     = sensor_name,
                Unit     = sensor_units,
                Channels = sensor_channels,
                Alerts   = sensor_alerts
            };

            if (device.Sensors.Keys.Contains(sensor_name))
            {
                device.Sensors.Remove(sensor_name);
            }
            device.Sensors.Add(sensor_name, sensor);
            try
            {
                OverlordIdentity.AddClaim(Resource.Storage, StorageAction.UpdateDevice);
                this.UpdateDevice(device);
                Log.WriteTableSuccess(string.Format("Added sensor {0} to device entity: Id: {1}, Token: {2}",
                                                    sensor.Name, device.Id.ToUrn(), device.Token));
                return(sensor);
            }
            catch (Exception e)
            {
                Log.ReadTableFailure(string.Format("Failed to read table for device: Id: {0}, Token: {1}.",
                                                   device.Id.ToUrn(), device.Token), e);
                throw;
            }
            finally
            {
                OverlordIdentity.DeleteClaim(Resource.Storage, StorageAction.AddSensor);
            }
        }
Exemplo n.º 24
0
        public void CanAddDeviceReading()
        {
            OverlordIdentity.InitializeAnonymousIdentity();
            AzureStorage storage = new AzureStorage();

            storage.AuthenticateAnonymousDevice(TestData.device_01_id.UrnToId(),
                                                TestData.device_01_token);
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.FindDevice);
            IStorageDevice device = storage.GetCurrentDevice();
            IDictionary <string, object> sensor_values = TestData.GenerateRandomSensorData(10);

            foreach (KeyValuePair <string, object> s in sensor_values)
            {
                OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddSensor);
                storage.AddSensor(s.Key, "CanAddDeviceReading_Test", null, null);
            }
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddDeviceReading);
            storage.AddDeviceReading(DateTime.Now, sensor_values);
        }
Exemplo n.º 25
0
        public void CanAddDevice()
        {
            AzureStorage storage = new AzureStorage();

            OverlordIdentity.InitializeUserIdentity(TestData.user_02_id,
                                                    TestData.user_02_token, new string[0]);
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.FindUser);
            IStorageUser user = storage.FindUser(TestData.user_02_id.UrnToGuid(),
                                                 TestData.user_02_token);

            Assert.NotNull(user);
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddDevice);
            IStorageDevice device = storage.AddDevice(user, "xUnit_CanAddDeviceTest_Name",
                                                      "xUnit_CanAddDeviceTest_Token", null);

            Assert.NotNull(device.Id);
            Assert.Equal("xUnit_CanAddDeviceTest_Name", device.Name);
            Assert.Equal("xUnit_CanAddDeviceTest_Token", device.Token);
            Assert.True(user.Devices.Contains(device.Id));
        }
Exemplo n.º 26
0
        public IStorageDevice AddDevice(IStorageUser user, string name, string token, GeoIp location,
                                        string id = null)
        {
            IStorageDevice device = new IStorageDevice()
            {
                Id      = string.IsNullOrEmpty(id) ? Guid.NewGuid() : id.UrnToGuid(),
                UserId  = user.Id,
                Token   = token,
                Name    = name,
                Sensors = new Dictionary <string, IStorageSensor>()
            };

            try
            {
                TableOperation insert_device_operation = TableOperation
                                                         .Insert(AzureStorage.CreateDeviceTableEntity(device));
                TableResult result;
                result      = this.DevicesTable.Execute(insert_device_operation);
                device.ETag = result.Etag;
                user.Devices.Add(device.Id);
                TableOperation update_user_operation = TableOperation.Merge(CreateUserTableEntity(user));
                result    = this.UsersTable.Execute(update_user_operation);
                user.ETag = result.Etag;
                Log.WriteTableSuccess(string.
                                      Format("Added device entity: {0}, Id: {1}, Token {2} to Devices table.",
                                             device.Name, device.Id.ToUrn(), device.Token));
                Log.WriteTableSuccess(string.Format("Added device entity: {0}, Id: {1}, to User entity {2}.",
                                                    device.Name, device.Id.ToUrn(), device.Token, user.Id.ToUrn()));
                return(device);
            }
            catch (Exception e)
            {
                Log.WriteTableFailure(string.Format("Failed to add device entity: {0}, Id: {1}, Token {2}.",
                                                    device.Name, device.Id.ToUrn(), device.Token), e);
                throw;
            }
            finally
            {
                OverlordIdentity.DeleteClaim(Resource.Storage, StorageAction.AddDevice);
            }
        }
Exemplo n.º 27
0
        private void InitialiseTestData()
        {
            AzureStorage storage = new AzureStorage();

            OverlordIdentity.InitializeUserIdentity(TestData.user_02_id, TestData.user_02_token, new string[0]);

            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.FindUser);
            IStorageUser user = storage.FindUser(TestData.user_02_id.UrnToGuid(), TestData.user_02_token);

            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddDevice);
            IStorageDevice device_01 = storage.AddDevice(user, TestData.device_01_name, TestData.device_01_token, null, TestData.device_01_id);

            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.FindUser);
            //user = storage.FindUser(AzureStorageTests.user_02_id.UrnToGuid(), AzureStorageTests.user_02_token);
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddDevice);
            IStorageDevice device_02 = storage.AddDevice(user, TestData.device_02_name, TestData.device_02_token, null, TestData.device_02_id);

            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.FindUser);
            //user = storage.FindUser(AzureStorageTests.user_02_id.UrnToGuid(), AzureStorageTests.user_02_token);
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.AddDevice);
            IStorageDevice device_03 = storage.AddDevice(user, TestData.device_03_name, TestData.device_03_token, null, TestData.device_03_id);
        }
Exemplo n.º 28
0
        public bool DeleteUser(IStorageUser user)
        {
            TableOperation delete_user_operation = TableOperation.Delete(CreateUserTableEntity(user));

            try
            {
                TableResult result = this.UsersTable.Execute(delete_user_operation);
                Log.WriteTableSuccess(string.Format("Deleted user entity: {0}, Id: {1}, Token: {2}",
                                                    user.UserName, user.Id.ToUrn(), user.Token, user.Id.ToUrn()));
                return(true);
            }
            catch (Exception e)
            {
                Log.WriteTableFailure(string.Format("Failed to delete user entity: Id: {0}, Token: {1}.",
                                                    user.Id.ToUrn(), user.Token), e);
                throw;
            }
            finally
            {
                OverlordIdentity.DeleteClaim(Resource.Storage, StorageAction.DeleteUser);
            }
        }
Exemplo n.º 29
0
        public IStorageDevice UpdateDevice(IStorageDevice device)
        {
            TableOperation update_device_operation = TableOperation.Merge(CreateDeviceTableEntity(device));

            try
            {
                TableResult result = this.DevicesTable.Execute(update_device_operation);
                Log.WriteTableSuccess(string.Format("Updated device entity: {0}, Id: {1}, Token: {2}",
                                                    device.Name, device.Id.ToUrn(), device.Token, device.Id.ToUrn()));
                device.ETag = result.Etag;
                return(device);
            }
            catch (Exception e)
            {
                Log.WriteTableFailure(string.Format("Failed to update device entity: Id: {0}, Token: {1}.",
                                                    device.Id.ToUrn(), device.Token), e);
                throw;
            }
            finally
            {
                OverlordIdentity.DeleteClaim(Resource.Storage, StorageAction.UpdateDevice);
            }
        }
Exemplo n.º 30
0
        public IStorageChannel AddChannel(string channel_name, string channel_description,
                                          string sensor_type, string channel_units, List <IStorageAlert> alerts)
        {
            OverlordIdentity.AddClaim(Resource.Storage, StorageAction.FindDevice);
            IStorageDevice device = this.GetCurrentDevice();

            IStorageChannel channel = new IStorageChannel()
            {
                Id          = Guid.NewGuid(),
                Name        = channel_name,
                Description = channel_description,
                SensorType  = sensor_type,
                Alerts      = alerts
            };

            try
            {
                TableOperation insert_channel_operation = TableOperation
                                                          .Insert(AzureStorage.CreateChannelTableEntity(channel));
                TableResult result;
                result = this.ChannelsTable.Execute(insert_channel_operation);
                Log.WriteTableSuccess(string.Format("Added Channel entity: {0}, Id: {1}.",
                                                    channel.Name, channel.Id.ToUrn()));
                return(channel);
            }
            catch (Exception e)
            {
                Log.WriteTableFailure(string.Format("Failed to add Channel entity: {0}, Id: {1}.", channel.Name,
                                                    channel.Id), e);
                throw;
            }
            finally
            {
                OverlordIdentity.DeleteClaim(Resource.Storage, StorageAction.AddChannel);
            }
        }