Esempio n. 1
0
        /**
         * Handles a message incoming through rift. First checks its opcode, then possibly decrypts the contents.
         */
        public void HandleMessage(dynamic msg)
        {
            if (key != null)
            {
                try
                {
                    var contents = CryptoHelpers.DecryptAES(key, (string)msg);
                    this.HandleMimicMessage(SimpleJson.DeserializeObject(contents));
                }
                catch
                {
                    // Ignore message.
                    return;
                }
            }

            if (!(msg is JsonArray))
            {
                return;
            }

            if (msg[0] == (long)MobileOpcode.Secret)
            {
                var info = CryptoHelpers.DecryptRSA((string)msg[1]);

                if (info == null)
                {
                    this.SendRaw("[" + MobileOpcode.SecretResponse + ",false]");
                    return;
                }

                dynamic contents = SimpleJson.DeserializeObject(info);
                if (contents["secret"] == null || contents["identity"] == null || contents["device"] == null || contents["browser"] == null)
                {
                    return;
                }

                // If this device is already approved, immediately respond.
                if (Persistence.IsDeviceApproved(contents["identity"]))
                {
                    this.key = Convert.FromBase64String((string)contents["secret"]);
                    this.SendRaw("[" + (long)MobileOpcode.SecretResponse + ",true]");
                }
                else
                {
                    // Note: UI prompt needs to happen on an STA thread.
                    //var promptThread = new Thread(() =>
                    //{
                    // Else, prompt the user.
                    //var window = new DeviceConnectionPrompt((string)contents["device"], (string)contents["browser"], result =>
                    //{
                    //    // If approved, save the device identity and set the key.
                    //    if (result)
                    //    {
                    //        this.key = Convert.FromBase64String((string)contents["secret"]);
                    //        Persistence.ApproveDevice(contents["identity"]);
                    //    }

                    //    // Send the result of the prompt to the device.
                    //    this.SendRaw("[" + (long)MobileOpcode.SecretResponse + "," + result.ToString().ToLower() + "]");
                    //});

                    //window.Show();
                    //window.Focus();
                    //Dispatcher.Run();
                    //});

                    this.key = Convert.FromBase64String((string)contents["secret"]);
                    Persistence.ApproveDevice(contents["identity"]);
                    this.SendRaw("[" + (long)MobileOpcode.SecretResponse + ",true]");

                    //promptThread.SetApartmentState(ApartmentState.STA);
                    //promptThread.Start();
                }
            }
        }