Exemplo n.º 1
0
        public IStorageDevice AddDevice(string name, string token, GeoIp location,
                                        string id = null)
        {
            IStorageUser user = this.GetCurrentUser();

            return(this.AddDevice(user, name, token, location));
        }
Exemplo n.º 2
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.º 3
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.º 4
0
        internal static DynamicTableEntity CreateUserTableEntity(IStorageUser user)
        {
            Dictionary <string, EntityProperty> dictionary = new Dictionary <string, EntityProperty>();

            dictionary.Add("UserName", new EntityProperty(user.UserName));
            dictionary.Add("Devices", new EntityProperty(JsonConvert.SerializeObject(user.Devices)));

            return(new DynamicTableEntity(user.Id.ToUrn(), user.Token, user.ETag, dictionary));
        }
Exemplo n.º 5
0
        internal IStorageUser UserEntityResolver(string partitionKey, string rowKey,
                                                 DateTimeOffset timestamp, IDictionary <string, EntityProperty> properties, string etag)
        {
            IStorageUser user = new IStorageUser();

            user.Id       = Guid.ParseExact(partitionKey, "D");
            user.Token    = rowKey;
            user.ETag     = etag;
            user.UserName = properties["UserName"].StringValue;
            user.Devices  = JsonConvert.DeserializeObject <IList <Guid> >
                                (properties["Devices"].StringValue);
            return(user);
        }
Exemplo n.º 6
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.º 7
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.º 8
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);
            }
        }