Beispiel #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;
            }
        }
Beispiel #2
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));
 }
        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);
        }