public async Task <ChadderError> PairDevice(ChadderUserDevice device) { try { var package = new PairDeviceContent() { Book = db.LocalUser.PrivateKeyBook }; var content = await EncryptForDevice(package, device); var request = new PairDeviceParameters() { DeviceId = device.DeviceId, Data = content.Serialize() }; var result = await AuthorizedRequest <BasicResponse <string> >(Connection.AccountHub, "PairDevice", request); if (result.Error == ChadderError.OK) { device.HasUserKey = true; await sqlDB.UpdateAsync(device); } return(result.Error); } catch (Exception ex) { Insight.Report(ex); return(ChadderError.GENERAL_EXCEPTION); } }
public async void ChangeDeviceName(ChadderUserDevice device) { //++var name = await TextInputDialog(device.Name, "Change Name", "Change"); //if (name != null) //{ ChadderApp.UIHelper.ShowLoading(); // var result = await Source.ChangeDeviceName(device, name); ChadderApp.UIHelper.HideLoading(); // ShowErrorIfNotOk(result); //} }
public async Task <ChadderError> DeleteDevice(ChadderUserDevice device) { var response = await AuthorizedRequest <BasicResponse>(Connection.AccountHub, "DeleteDevice", device.DeviceId); if (response.Error == ChadderError.OK) { await sqlDB.DeleteAsync(device); db.LocalUser.Devices.Remove(device); } return(response.Error); }
public async Task <ChadderError> ChangeDeviceName(ChadderUserDevice device, string name) { var request = new ChangeDeviceNameParameter() { DeviceId = device.DeviceId, Name = name }; var result = await AuthorizedRequest <BasicResponse>(Connection.AccountHub, "ChangeDeviceName", request); if (result.Error == ChadderError.OK) { device.Name = name; await sqlDB.UpdateAsync(device); } return(result.Error); }
public async void DeleteDevice(ChadderUserDevice device) { ChadderApp.UIHelper.ShowLoading(); var result = await Source.DeleteDevice(device); ChadderApp.UIHelper.HideLoading(); ShowErrorIfNotOk(result); //var dialog = new ConfirmationDialogWithProgress(Activity); //dialog.Title = "Delete Device"; //dialog.Message = "Are you sure you want to delete this device?"; //dialog.Positive = "Delete"; //dialog.OnPositiveAsync += async () => //{ // var result = await ChadderDataSource.source.deleteDevice(device); // if (result != null) // Toast.MakeText(Activity, result, ToastLength.Long).Show(); //}; //dialog.Show(); }
public async void PairDevice(ChadderUserDevice device) { ChadderApp.UIHelper.ShowLoading(); var result = await Source.PairDevice(device); ChadderApp.UIHelper.HideLoading(); ShowErrorIfNotOk(result); //var dialog = new ConfirmationDialogWithProgress(Activity); //dialog.Title = GetString(Resource.String.DevicePairTitle); //dialog.Message = string.Format(GetString(Resource.String.DevicePairMessage), device.Public.FingerprintEncodedClipped); //dialog.Positive = GetString(Resource.String.DevicePairConfirm); //dialog.OnPositiveAsync += async () => //{ // var result = await ChadderDataSource.source.pairDevice(device); // if (result != null) // Toast.MakeText(Activity, result, ToastLength.Long).Show(); //}; //dialog.Show(); }
public async Task <ChadderError> GetMyDevices() { var response = await AuthorizedRequest <BasicArrayResponse <DeviceInfo> >(Connection.AccountHub, "GetMyDevices"); if (response.Error == ChadderError.OK) { foreach (var d in response.List) { var device = db.GetDevice(d.Id); if (device == null) { device = new ChadderUserDevice(d); device.CurrentDevice = device.DeviceId == db.LocalDevice.DeviceId; await db.AddDevice(device); } else { device.Update(d); await sqlDB.UpdateAsync(device); } } } return(response.Error); }
public virtual async Task <bool> ProcessPackage(Package package) { var content = Content.Deserialize(package.Data); var identifier = await content.Find <ECDH>(this); string fromId = null; string toId = null; if (identifier != null) { fromId = identifier.SourceId; toId = identifier.TargetId; } if (content == null) { return(false); } // If multiple layer of encryption were to be supported there should be a loop around this section if (content is IContentWrapper) // Decrypt section { content = await(content as IContentWrapper).GetContent(this); } // This has to be the clear content if (content is UserPublicKey) { if (package.OwnerId != null) { Insight.Track("Non-server trying to update client id"); } else { await ProcessUserPublicKey(content as UserPublicKey); } } else if (content is DevicePublicKey) { if (package.OwnerId != null) { Insight.Track("Non-server trying to update device id"); return(true); } var device = db.GetDevice((content as DevicePublicKey).DeviceId); var pbk = (content as DevicePublicKey).PublicKeyData; if (device == null) { if (pbk != null) { device = new ChadderUserDevice() { DeviceId = (content as DevicePublicKey).DeviceId, PublicKeyBookData = pbk, Name = "Temp", HasUserKey = false, CurrentDevice = false }; await db.AddDevice(device); } } else { if (pbk == null) { db.LocalUser.Devices.Remove(device); await sqlDB.DeleteAsync(device); } else { device.PublicKeyBookData = pbk; await sqlDB.UpdateAsync(device); } } } else if (content is PairDeviceContent) { if (package.OwnerId != db.LocalUser.UserId) { Insight.Track("Not this user trying to pair device"); return(true); } db.LocalUser.PrivateKeyBookData = (content as PairDeviceContent).Book.Serialize(); await sqlDB.UpdateAsync(db.LocalUser); return(true); } else if (content is TakeMessageBackContent) { await ProcessTakeBack(content as TakeMessageBackContent, fromId, toId); } else if (content is BasicMessage) // Process section { var msg = content as BasicMessage; ChadderConversation conversation = null; if (fromId == db.LocalUser.UserId) { conversation = db.GetContactConversation(toId); } else { conversation = db.GetContactConversation(package.OwnerId); } if (conversation.Messages.FirstOrDefault(i => i.MessageId == msg.Id) == null) { var record = new ChadderMessage() { MessageId = msg.Id, Type = ChadderMessage.MESSAGE_TYPE.TEXT, Status = ChadderMessage.MESSAGE_STATUS.SENT, Expiration = msg.Expiration, TimeSent = msg.TimeSent, TimeReceived = DateTime.UtcNow, TimeServer = package.Time, MyMessage = false, ConversationId = conversation.recordId, Sender = conversation.Contact, }; if (conversation.ContactUserId != fromId) { record.MyMessage = true; record.Sender = db.LocalUser; } record.UserId = record.Sender.UserId; if (content is TextMessage) { record.Body = (content as TextMessage).Body; } await db.AddMessage(record, conversation); } else { Insight.Track("Repeated Message GUID"); } } else { return(false); } return(true); }
public async Task <Content> EncryptForDevice(Content content, ChadderUserDevice device) { var key = new ECDHDevice(db.LocalDevice.DeviceId, device.DeviceId); return(await AES256WithKey.Encrypt(this, key, content)); }