Example #1
0
    def refresh_token(self):
        LOG.debug('Refreshing token')
        if identity_lock.acquire(blocking=False):
            try:
                data = self.send({
                    "path": "auth/token",
                    "headers": {
                        "Authorization": "Bearer " + self.identity.refresh
                    }
                })
                IdentityManager.save(data, lock=False)
                LOG.debug('Saved credentials')
            except HTTPError as e:
                if e.response.status_code == 401:
                    LOG.error('Could not refresh token, invalid refresh code.')
                else:
                    raise

            finally:
                identity_lock.release()
        else:  # Someone is updating the identity wait for release
            with identity_lock:
                LOG.debug('Refresh is already in progress, waiting until done')
                time.sleep(1.2)
                os.sync()
                self.identity = IdentityManager.load(lock=False)
                LOG.debug('new credentials loaded')
Example #2
0
    def refresh_token(self):
        LOG.debug('Refreshing token')
        if identity_lock.acquire(blocking=False):
            try:
                data = self.send({
                    "path": "auth/token",
                    "headers": {
                        "Authorization": "Bearer " + self.identity.refresh,
                        "Device": self.identity.uuid
                    }
                })
                IdentityManager.save(data, lock=False)
                LOG.debug('Saved credentials')
            except HTTPError as e:
                if e.response.status_code == 401:
                    LOG.error('Could not refresh token, invalid refresh code.')
                else:
                    raise

            finally:
                identity_lock.release()
        else:  # Someone is updating the identity wait for release
            with identity_lock:
                LOG.debug('Refresh is already in progress, waiting until done')
                time.sleep(1.2)
                os.sync()
                self.identity = IdentityManager.load(lock=False)
                LOG.debug('new credentials loaded')
Example #3
0
 def refresh_token(self):
     data = self.send({
         "path": "auth/token",
         "headers": {
             "Authorization": "Bearer " + self.identity.refresh
         }
     })
     IdentityManager.save(data)
 def refresh_token(self):
     data = self.send({
         "path": "auth/token",
         "headers": {
             "Authorization": "Bearer " + self.identity.refresh
         }
     })
     IdentityManager.save(data)
Example #5
0
 def check_token(self):
     if self.identity.refresh and self.identity.is_expired():
         self.identity = IdentityManager.load()
         if self.identity.is_expired():
             data = self.send({
                 "path": "auth/token",
                 "headers": {
                     "Authorization": "Bearer " + self.identity.refresh
                 }
             })
             IdentityManager.save(data)
    def activate(self):
        try:
            token = self.data.get("token")
            login = self.api.activate(self.state, token)
            self.enclosure.activate_mouth_events()
            self.speak_dialog("pairing.paired")
            IdentityManager.save(login)
            self.emitter.emit(Message("mycroft.paired", login))
        except:
            self.data["expiration"] -= self.delay

            if self.data.get("expiration") <= 0:
                self.data = None
                self.handle_pairing()
            else:
                self.__create_activator()
Example #7
0
 def select_stt(self, message):
     selection = message.data["engine"]
     self.send_stop_signal("pairing.stt.menu.stop")
     if selection == "google":
         self.change_to_chromium()
     elif selection == "kaldi":
         self.change_to_kaldi()
     if not self.using_mock:
         self.enable_mock()
         # create pairing file with dummy data
         login = {"uuid": self.state,
                  "access":
                      "OVOSdbF1wJ4jA5lN6x6qmVk_QvJPqBQZTUJQm7fYzkDyY_Y=",
                  "refresh":
                      "OVOS66c5SpAiSpXbpHlq9HNGl1vsw_srX49t5tCv88JkhuE=",
                  "expires_at": time.time() + 999999}
         IdentityManager.save(login)
     self.handle_tts_menu()
    def on_activate(self):
        try:
            # wait for a signal from the backend that pairing is complete
            token = self.data.get("token")
            login = self.api.activate(self.state, token)

            # shut down thread that repeats the code to the user
            if self.repeater:
                self.repeater.cancel()
                self.repeater = None

            # is_speaking() and stop_speaking() support is mycroft-core 0.8.16+
            try:
                if mycroft.util.is_speaking():
                    # Assume speaking is the pairing code.  Stop TTS
                    mycroft.util.stop_speaking()
            except:
                pass

            self.enclosure.activate_mouth_events()  # clears the display
            self.speak_dialog("pairing.paired")
            
            # wait_while_speaking() support is mycroft-core 0.8.16+
            try:
                mycroft.util.wait_while_speaking()
            except:
                pass

            IdentityManager.save(login)
            self.emitter.emit(Message("mycroft.paired", login))

            # Un-mute.  Would have been muted during onboarding for a new
            # unit, and not dangerous to do if pairing was started
            # independently.
            self.emitter.emit(Message("mycroft.mic.unmute", None))

        except:
            if self.last_request < time.time():
                self.data = None
                self.handle_pairing()
            else:
                self.__create_activator()
Example #9
0
 def refresh_token(self):
     LOG.debug('Refreshing token')
     if identity_lock.acquire(blocking=False):
         try:
             data = self.send({
                 "path": "auth/token",
                 "headers": {
                     "Authorization": "Bearer " + self.identity.refresh
                 }
             })
             IdentityManager.save(data, lock=False)
             LOG.debug('Saved credentials')
         finally:
             identity_lock.release()
     else:  # Someone is updating the identity wait for release
         with identity_lock:
             LOG.debug('Refresh is already in progress, waiting until done')
             time.sleep(1.2)
             os.sync()
             self.identity = IdentityManager.load(lock=False)
             LOG.debug('new credentials loaded')
Example #10
0
 def refresh_token(self):
     LOG.debug('Refreshing token')
     if identity_lock.acquire(blocking=False):
         try:
             data = self.send({
                 "path": "auth/token",
                 "headers": {
                     "Authorization": "Bearer " + self.identity.refresh
                 }
             })
             IdentityManager.save(data, lock=False)
             LOG.debug('Saved credentials')
         finally:
             identity_lock.release()
     else:  # Someone is updating the identity wait for release
         with identity_lock:
             LOG.debug('Refresh is already in progress, waiting until done')
             time.sleep(1.2)
             os.sync()
             self.identity = IdentityManager.load(lock=False)
             LOG.debug('new credentials loaded')
Example #11
0
    def check_for_activate(self):
        """Method is called every 10 seconds by Timer. Checks if user has
        activated the device yet on home.mycroft.ai and if not repeats
        the pairing code every 60 seconds.
        """
        try:
            # Attempt to activate.  If the user has completed pairing on the,
            # backend, this will succeed.  Otherwise it throws and HTTPError()

            token = self.data.get("token")
            login = self.api.activate(self.state, token)  # HTTPError() thrown

            # When we get here, the pairing code has been entered on the
            # backend and pairing can now be saved.
            # The following is kinda ugly, but it is really critical that we
            # get this saved successfully or we need to let the user know that
            # they have to perform pairing all over again at the website.
            try:
                IdentityManager.save(login)
            except Exception as e:
                self.log.debug("First save attempt failed: " + repr(e))
                time.sleep(2)
                try:
                    IdentityManager.save(login)
                except Exception as e2:
                    # Something must be seriously wrong
                    self.log.debug("Second save attempt failed: " + repr(e2))
                    self.abort_and_restart()

            if mycroft.audio.is_speaking():
                # Assume speaking is the pairing code.  Stop TTS of that.
                mycroft.audio.stop_speaking()

            self.enclosure.activate_mouth_events()  # clears the display

            # Notify the system it is paired
            self.gui.show_page("pairing_done.qml", override_idle=False)
            self.bus.emit(Message("mycroft.paired", login))

            self.pairing_performed = True
            with self.pair_dialog_lock:
                if self.mycroft_ready:
                    # Tell user they are now paired
                    self.speak_dialog(self.paired_dialog)
                    mycroft.audio.wait_while_speaking()
                else:
                    self.speak_dialog("wait.for.startup")
                    mycroft.audio.wait_while_speaking()

            # Un-mute.  Would have been muted during onboarding for a new
            # unit, and not dangerous to do if pairing was started
            # independently.
            self.bus.emit(Message("mycroft.mic.unmute", None))

            # Send signal to update configuration
            self.bus.emit(Message("configuration.updated"))

            # Allow this skill to auto-update again
            self.reload_skill = True
        except HTTPError:
            # speak pairing code every 60th second
            with self.counter_lock:
                if self.count == 0:
                    self.speak_code()
                self.count = (self.count + 1) % 6

            if time.monotonic() > self.time_code_expires:
                # After 20 hours the token times out.  Restart
                # the pairing process.
                with self.counter_lock:
                    self.count = -1
                self.data = None
                self.handle_pairing()
            else:
                # trigger another check in 10 seconds
                self.__create_activator()
        except Exception as e:
            self.log.debug("Unexpected error: " + repr(e))
            self.abort_and_restart()