Skip to content

qml_login

Qt Quick login dialog using the launcher's existing authentication API.

LoginController

Bases: QObject

State shared by the two QML pages; credentials stay in Python.

Source code in common/ayon_common/connection/ui/qml_login.py
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
class LoginController(QtCore.QObject):
    """State shared by the two QML pages; credentials stay in Python."""

    changed = QtCore.Signal()
    server_url_changed = QtCore.Signal()
    known_servers_changed = QtCore.Signal()
    username_changed = QtCore.Signal()
    images_changed = QtCore.Signal()
    authenticated = QtCore.Signal(str, str, str)
    logged_out = QtCore.Signal()
    dismissed = QtCore.Signal()
    clear_password = QtCore.Signal()

    def __init__(self, parent=None):
        super().__init__(parent)
        self._url = ""
        self._username = ""
        self._known_servers = []
        self._api_key = None
        self._initialized = False
        self._connection_failed = False
        self._force_username = False
        self._logged_in = False
        self._logged_in_username = ""
        self._logged_in_url = ""
        self._logged_in_token = None
        # Valid supplied credentials (url, token, username) the user can
        #   continue with.
        self._session = None
        # Details of the signed in user
        self._session_user_loaded = False
        self._session_expired = False
        self._full_name = ""
        self._email = ""
        # Optional images by kind ("avatar", "logo", "background")
        self._images = {}
        self._page = 0
        self._busy = False
        self._error = ""
        self._browser_supported = False
        self._waiting = False
        self._listener = None
        self._deadline = 0
        self._request_id = 0
        self._requests = {}
        # Opening the browser can block (e.g. 'webbrowser' waits for the
        #   browser process on some platforms). It runs separately so it
        #   never blocks processing of the callback token.
        self._browser_open_id = 0
        self._browser_open_request = None
        self._fetch_id = 0
        self._fetches = {}
        self._latest_fetch = {}
        self._closed = False
        self._timer = QtCore.QTimer(self)
        self._timer.setInterval(100)
        self._timer.timeout.connect(self._poll_browser)

    @QtCore.Property(str, notify=server_url_changed)
    def serverUrl(self):
        return self._url

    @QtCore.Property("QVariantList", notify=known_servers_changed)
    def knownServers(self):
        """Server urls used on this machine, most recently used first."""
        return list(self._known_servers)

    @QtCore.Property(str, notify=changed)
    def sessionServerUrl(self):
        """Server of the current session, it cannot be removed."""
        return self._logged_in_url if self._logged_in else ""

    @QtCore.Property(str, notify=username_changed)
    def username(self):
        return self._username

    @QtCore.Property(bool, notify=changed)
    def forceUsername(self):
        return self._force_username

    @QtCore.Property(bool, notify=changed)
    def loggedIn(self):
        return self._logged_in

    @QtCore.Property(str, notify=changed)
    def loggedInUsername(self):
        return self._logged_in_username

    @QtCore.Property(str, notify=changed)
    def sessionUsername(self):
        if self._logged_in:
            return self._logged_in_username
        return self._session[2] if self._session else ""

    @QtCore.Property(str, notify=changed)
    def sessionDisplayName(self):
        return self._full_name or self.sessionUsername

    @QtCore.Property(str, notify=changed)
    def sessionShortName(self):
        """First name of the signed in user, username if it is not known."""
        if self._full_name:
            return self._full_name.split()[0]
        return self.sessionUsername

    @QtCore.Property(str, notify=changed)
    def sessionEmail(self):
        return self._email

    # Images are data URLs, empty until they are loaded from the server.
    @QtCore.Property(str, notify=images_changed)
    def sessionAvatar(self):
        return self._images.get("avatar", "")

    @QtCore.Property(str, notify=images_changed)
    def studioLogo(self):
        return self._images.get("logo", "")

    @QtCore.Property(str, notify=images_changed)
    def studioBackground(self):
        return self._images.get("background", "")

    @QtCore.Property(bool, notify=changed)
    def canContinue(self):
        """Supplied credentials are valid for the server on sign-in page."""
        return (
            self._session is not None
            and self._page == 1
            and _normalize_url(self._url) == _normalize_url(self._session[0])
        )

    @QtCore.Property(bool, notify=changed)
    def isCurrentSession(self):
        """Server on the sign-in page is the one of the current session."""
        return (
            self._logged_in
            and not self._session_expired
            and self._page == 1
            and _normalize_url(self._url) == _normalize_url(
                self._logged_in_url
            )
        )

    @QtCore.Property(int, notify=changed)
    def page(self):
        return self._page

    @QtCore.Property(bool, notify=changed)
    def busy(self):
        return self._busy

    @QtCore.Property(str, notify=changed)
    def errorMessage(self):
        return self._error

    @QtCore.Property(bool, notify=changed)
    def browserSupported(self):
        return self._browser_supported

    @QtCore.Property(bool, notify=changed)
    def waitingForBrowser(self):
        return self._waiting

    @QtCore.Property(bool, notify=changed)
    def connectionFailed(self):
        return self._connection_failed

    def set_url(self, url):
        if (url or "").strip().rstrip("/") != self._url.strip().rstrip("/"):
            # A supplied key belongs only to its original server.
            self._api_key = None
        self._url = url or ""
        self.server_url_changed.emit()

    def set_username(self, username):
        self._username = username or ""
        self.username_changed.emit()

    def set_known_servers(self, urls):
        self._known_servers = [url for url in (urls or []) if url]
        self.known_servers_changed.emit()

    def set_force_username(self, value):
        self._force_username = bool(value)
        self.changed.emit()

    def set_api_key(self, api_key):
        self._api_key = api_key or None

    def set_logged_in(self, logged_in, username=None, url=None, api_key=None):
        """Show the current session and allow logout (change user mode).

        Login options are hidden while the current session's server is
        selected; changing the server shows them again. The api key is
        only used to show details of the signed in user.
        """
        self._logged_in = bool(logged_in)
        self._logged_in_username = (username or "") if logged_in else ""
        self._logged_in_url = (url or self._url) if logged_in else ""
        self._logged_in_token = (api_key or None) if logged_in else None
        self._set_session_user(None)
        self._session_user_loaded = False
        self._session_expired = False
        self.changed.emit()

    def _expire_session(self):
        """The current session cannot be continued; ask to sign in."""
        self._session_expired = True
        self._error = "Your login has expired. Please sign in again."

    def _set_session_user(self, user):
        attrib = (user or {}).get("attrib") or {}
        self._full_name = (attrib.get("fullName") or "").strip()
        self._email = (attrib.get("email") or "").strip()
        self._clear_images("avatar")

    def _clear_images(self, *kinds):
        """Forget loaded images and ignore the ones still loading."""
        for kind in kinds:
            self._images.pop(kind, None)
            self._latest_fetch.pop(kind, None)
        self.images_changed.emit()

    def initialize(self):
        """Validate prefilled connection details once the dialog is shown."""
        if self._initialized or self._closed:
            return
        self._initialized = True
        if self._url.strip():
            self.validateServer(self._url)

    def _start(self, operation, function, *args):
        self._request_id += 1
        request_id = self._request_id
        request = _Request()
        self._requests[request_id] = request
        request.completed.connect(self._complete, QtCore.Qt.QueuedConnection)
        self._busy = True
        self._error = ""
        self.changed.emit()
        request.start(request_id, operation, function, args)

    def _fetch_image(self, kind, function, *args):
        """Load an optional image without blocking the login flow."""
        self._fetch_id += 1
        fetch_id = self._fetch_id
        request = _Request()
        self._fetches[fetch_id] = request
        self._latest_fetch[kind] = fetch_id
        request.completed.connect(self._fetched, QtCore.Qt.QueuedConnection)
        request.start(fetch_id, kind, function, args)

    @QtCore.Slot(int, str, object, str)
    def _fetched(self, fetch_id, kind, result, error):
        self._fetches.pop(fetch_id, None)
        if (
            self._closed or error or not result
            or self._latest_fetch.get(kind) != fetch_id
        ):
            return
        self._images[kind] = result
        self.images_changed.emit()

    def _fetch_avatar(self):
        if self._session:
            url, token, username = self._session
        else:
            url, token, username = (
                self._url, self._logged_in_token, self._logged_in_username
            )
        if token and username:
            self._fetch_image("avatar", fetch_avatar, url, token, username)

    @QtCore.Slot(int, str, object, str)
    def _complete(self, request_id, operation, result, error):
        self._requests.pop(request_id, None)
        if self._closed or request_id != self._request_id:
            return
        self._busy = False
        if operation == "session_user":
            self._session_user_loaded = True
            self._page = 1
            if error:
                # Details of the current session are optional.
                pass
            elif result:
                self._set_session_user(result)
                self._fetch_avatar()
            else:
                self._expire_session()
        elif error:
            self._error = error
            if operation in ("server", "saved_login"):
                self._connection_failed = True
            if operation in ("open_browser", "browser_login"):
                self._stop_listener()
            if operation == "password":
                self.clear_password.emit()
        elif operation == "server":
            self._url, self._browser_supported = result
            self.server_url_changed.emit()
            self._clear_images(*STUDIO_IMAGES)
            for kind in STUDIO_IMAGES:
                self._fetch_image(kind, fetch_studio_image, self._url, kind)
            if self._api_key:
                self._start(
                    "saved_login", saved_login,
                    self._url, self._api_key, self._username,
                )
                return
            self._page = 1
            if self.isCurrentSession and not self._session_user_loaded:
                if not self._logged_in_token:
                    self._expire_session()
                else:
                    # Check the current session before it is shown
                    self._page = 0
                    self._start(
                        "session_user", get_token_user,
                        self._url, self._logged_in_token,
                    )
                    return
        elif operation == "saved_login":
            self._api_key = None
            if result is None:
                self._page = 1
                self._error = (
                    "Your saved credentials are no longer valid."
                    " Please sign in again."
                )
            else:
                # Do not continue automatically, the user may want to
                #   login as a different user.
                self._session = result[:3]
                self._page = 1
                self._set_session_user(result[3])
                self._fetch_avatar()
        elif operation == "logout":
            self._session = None
            self._set_session_user(None)
        elif operation in ("password", "browser_login"):
            self.clear_password.emit()
            self.authenticated.emit(*result)
        self.changed.emit()

    @QtCore.Slot()
    def refreshKnownServers(self):
        """Reload server urls used on this machine."""
        if not self._closed:
            self.set_known_servers(get_known_servers())

    @QtCore.Slot(str)
    def removeKnownServer(self, url):
        """Forget a known server url and its stored login token.

        The server of the current session is kept, logout removes it.
        """
        if self._closed or not url:
            return
        if self._logged_in and _normalize_url(url) == _normalize_url(
            self._logged_in_url
        ):
            return
        forget_server(url)
        self.refreshKnownServers()

    @QtCore.Slot(str)
    def validateServer(self, url):
        if self._closed or self._busy or self._page != 0:
            return
        self._connection_failed = False
        self.set_url(url)
        self._start("server", check_server, url)

    @QtCore.Slot(str, str)
    def signIn(self, username, password):
        if self._closed or self._busy or self._waiting or self._page != 1:
            return
        username = self._username if self._force_username else username.strip()
        if not username or not password:
            self._error = "Enter your username and password."
            self.changed.emit()
            return
        self._username = username
        self._start("password", password_login, self._url, username, password)

    @QtCore.Slot()
    def openBrowser(self):
        if (
            self._closed or self._busy or self._waiting
            or self._page != 1 or not self._browser_supported
        ):
            return
        try:
            self._listener = LoginServerListener(self._url)
            self._listener.start()
        except Exception:
            self._stop_listener()
            self._error = (
                "Could not start browser login. Try again or sign in below."
            )
            self.changed.emit()
            return
        redirect = f"http://localhost:{self._listener.port}"
        url = self._url + "/?" + urlencode({"auth_redirect": redirect})
        self._waiting = True
        self._error = ""
        self._deadline = time.monotonic() + BROWSER_TIMEOUT
        self.clear_password.emit()
        self._timer.start()
        self.changed.emit()

        self._browser_open_id += 1
        request = _Request()
        self._browser_open_request = request
        request.completed.connect(
            self._browser_opened, QtCore.Qt.QueuedConnection
        )
        request.start(
            self._browser_open_id, "open_browser", open_browser, (url,)
        )

    @QtCore.Slot(int, str, object, str)
    def _browser_opened(self, open_id, operation, result, error):
        if open_id != self._browser_open_id:
            return
        self._browser_open_request = None
        # Ignore errors when the login was cancelled or already finished.
        if self._closed or not error or not self._listener:
            return
        self._stop_listener()
        self._error = error
        self.changed.emit()

    def _poll_browser(self):
        if not self._listener:
            return
        if time.monotonic() >= self._deadline:
            self.cancelBrowser()
            self._error = "Browser login timed out. Please try again."
            self.changed.emit()
            return
        if self._busy:
            return
        token = self._listener.get_token()
        if not token:
            return
        self._stop_listener()
        # Keep the cancel control available while checking the callback token.
        self._waiting = True
        forced_username = self._username if self._force_username else None
        self._start(
            "browser_login", browser_login, self._url, token, forced_username
        )

    def _stop_listener(self):
        self._timer.stop()
        listener, self._listener = self._listener, None
        self._waiting = False
        if listener:
            threading.Thread(target=listener.stop, daemon=True).start()

    @QtCore.Slot()
    def cancelBrowser(self):
        self._request_id += 1
        self._stop_listener()
        self._busy = False
        self._error = ""
        self.changed.emit()

    @QtCore.Slot()
    def back(self):
        if self._closed:
            return
        self.cancelBrowser()
        self._page = 0
        self._browser_supported = False
        self._api_key = None
        self._connection_failed = False
        self.clear_password.emit()
        self.changed.emit()

    @QtCore.Slot()
    def continueSession(self):
        if self._closed or self._busy or self._waiting:
            return
        if self.canContinue:
            self.authenticated.emit(*self._session)
        elif self.isCurrentSession:
            # Keep the current session as is.
            self.dismissed.emit()

    @QtCore.Slot()
    def logout(self):
        if self._closed:
            return
        if self._logged_in:
            # The caller logs out the current session.
            self.cancelBrowser()
            self.logged_out.emit()
        elif self.canContinue and not self._busy and not self._waiting:
            # Expire the supplied credentials and ask to sign in again.
            url, token, _ = self._session
            self._start(
                "logout", logout_from_server, url, token, REQUEST_TIMEOUT
            )

    @QtCore.Slot()
    def clearError(self):
        if self._error or self._connection_failed:
            self._error = ""
            self._connection_failed = False
            self.changed.emit()

    def shutdown(self):
        self._closed = True
        self._api_key = None
        self._session = None
        self._logged_in_token = None
        self.cancelBrowser()
        self.clear_password.emit()

canContinue()

Supplied credentials are valid for the server on sign-in page.

Source code in common/ayon_common/connection/ui/qml_login.py
409
410
411
412
413
414
415
416
@QtCore.Property(bool, notify=changed)
def canContinue(self):
    """Supplied credentials are valid for the server on sign-in page."""
    return (
        self._session is not None
        and self._page == 1
        and _normalize_url(self._url) == _normalize_url(self._session[0])
    )

initialize()

Validate prefilled connection details once the dialog is shown.

Source code in common/ayon_common/connection/ui/qml_login.py
510
511
512
513
514
515
516
def initialize(self):
    """Validate prefilled connection details once the dialog is shown."""
    if self._initialized or self._closed:
        return
    self._initialized = True
    if self._url.strip():
        self.validateServer(self._url)

isCurrentSession()

Server on the sign-in page is the one of the current session.

Source code in common/ayon_common/connection/ui/qml_login.py
418
419
420
421
422
423
424
425
426
427
428
@QtCore.Property(bool, notify=changed)
def isCurrentSession(self):
    """Server on the sign-in page is the one of the current session."""
    return (
        self._logged_in
        and not self._session_expired
        and self._page == 1
        and _normalize_url(self._url) == _normalize_url(
            self._logged_in_url
        )
    )

knownServers()

Server urls used on this machine, most recently used first.

Source code in common/ayon_common/connection/ui/qml_login.py
349
350
351
352
@QtCore.Property("QVariantList", notify=known_servers_changed)
def knownServers(self):
    """Server urls used on this machine, most recently used first."""
    return list(self._known_servers)

refreshKnownServers()

Reload server urls used on this machine.

Source code in common/ayon_common/connection/ui/qml_login.py
632
633
634
635
636
@QtCore.Slot()
def refreshKnownServers(self):
    """Reload server urls used on this machine."""
    if not self._closed:
        self.set_known_servers(get_known_servers())

removeKnownServer(url)

Forget a known server url and its stored login token.

The server of the current session is kept, logout removes it.

Source code in common/ayon_common/connection/ui/qml_login.py
638
639
640
641
642
643
644
645
646
647
648
649
650
651
@QtCore.Slot(str)
def removeKnownServer(self, url):
    """Forget a known server url and its stored login token.

    The server of the current session is kept, logout removes it.
    """
    if self._closed or not url:
        return
    if self._logged_in and _normalize_url(url) == _normalize_url(
        self._logged_in_url
    ):
        return
    forget_server(url)
    self.refreshKnownServers()

sessionServerUrl()

Server of the current session, it cannot be removed.

Source code in common/ayon_common/connection/ui/qml_login.py
354
355
356
357
@QtCore.Property(str, notify=changed)
def sessionServerUrl(self):
    """Server of the current session, it cannot be removed."""
    return self._logged_in_url if self._logged_in else ""

sessionShortName()

First name of the signed in user, username if it is not known.

Source code in common/ayon_common/connection/ui/qml_login.py
385
386
387
388
389
390
@QtCore.Property(str, notify=changed)
def sessionShortName(self):
    """First name of the signed in user, username if it is not known."""
    if self._full_name:
        return self._full_name.split()[0]
    return self.sessionUsername

set_logged_in(logged_in, username=None, url=None, api_key=None)

Show the current session and allow logout (change user mode).

Login options are hidden while the current session's server is selected; changing the server shows them again. The api key is only used to show details of the signed in user.

Source code in common/ayon_common/connection/ui/qml_login.py
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
def set_logged_in(self, logged_in, username=None, url=None, api_key=None):
    """Show the current session and allow logout (change user mode).

    Login options are hidden while the current session's server is
    selected; changing the server shows them again. The api key is
    only used to show details of the signed in user.
    """
    self._logged_in = bool(logged_in)
    self._logged_in_username = (username or "") if logged_in else ""
    self._logged_in_url = (url or self._url) if logged_in else ""
    self._logged_in_token = (api_key or None) if logged_in else None
    self._set_session_user(None)
    self._session_user_loaded = False
    self._session_expired = False
    self.changed.emit()

LoginError

Bases: Exception

An authentication error that can be displayed to the user.

Source code in common/ayon_common/connection/ui/qml_login.py
51
52
class LoginError(Exception):
    """An authentication error that can be displayed to the user."""

QmlServerLoginWindow

Bases: QDialog

Embed QML while preserving the existing modal dialog result contract.

Source code in common/ayon_common/connection/ui/qml_login.py
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
class QmlServerLoginWindow(QtWidgets.QDialog):
    """Embed QML while preserving the existing modal dialog result contract."""

    def __init__(
        self, parent=None, *, url=None, username=None, api_key=None,
        force_username=False, logged_in=False,
    ):
        super().__init__(parent)
        self.setWindowTitle("Sign in to AYON")
        self.setWindowIcon(QtGui.QIcon(get_icon_path()))
        self.setMinimumSize(480, 560)
        self.resize(560, 720)
        self._result = (None, None, None, False)

        self.view = QtQuickWidgets.QQuickWidget(self)
        # The view destroys its QML scene before its QObject children. Keep
        # the controller alive until all bindings that reference it are gone.
        self.controller = LoginController(self.view)
        self.controller.authenticated.connect(self._authenticated)
        self.controller.logged_out.connect(self._logged_out)
        self.controller.dismissed.connect(self.reject)
        self.controller.set_url(url)
        self.controller.set_username(username)
        # The key of the current session is not used to log in again.
        self.controller.set_api_key(None if logged_in else api_key)
        self.controller.set_force_username(force_username)
        self.controller.refreshKnownServers()
        self.controller.set_logged_in(logged_in, username, url, api_key)

        self.view.setResizeMode(
            QtQuickWidgets.QQuickWidget.SizeRootObjectToView
        )
        self.view.setClearColor(QtGui.QColor("#252B32"))
        self.view.rootContext().setContextProperty("login", self.controller)
        self.view.setSource(QtCore.QUrl.fromLocalFile(str(
            Path(__file__).parent / "qml" / "Login.qml"
        )))
        if self.view.status() == QtQuickWidgets.QQuickWidget.Error:
            errors = "\n".join(
                error.toString() for error in self.view.errors()
            )
            raise RuntimeError(f"Could not load the AYON login UI:\n{errors}")
        layout = QtWidgets.QVBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.addWidget(self.view)

    def set_url(self, url):
        self.controller.set_url(url)

    def set_username(self, username):
        self.controller.set_username(username)

    def set_force_username(self, force_username):
        self.controller.set_force_username(force_username)

    def set_api_key(self, api_key):
        self.controller.set_api_key(api_key)

    def set_logged_in(self, logged_in, username=None, url=None, api_key=None):
        self.controller.set_logged_in(logged_in, username, url, api_key)

    def showEvent(self, event):
        super().showEvent(event)
        self.controller.initialize()

    @QtCore.Slot(str, str, str)
    def _authenticated(self, url, token, username):
        self._result = (url, token, username, False)
        self.accept()

    @QtCore.Slot()
    def _logged_out(self):
        self._result = (None, None, None, True)
        self.accept()

    def result(self):
        return self._result

    def done(self, result):
        self.controller.shutdown()
        super().done(result)

    def closeEvent(self, event):
        self.controller.shutdown()
        super().closeEvent(event)

check_server(url)

Normalize the address and verify that it exposes an AYON API.

Source code in common/ayon_common/connection/ui/qml_login.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
def check_server(url):
    """Normalize the address and verify that it exposes an AYON API."""
    url = url.strip()
    if not url:
        raise LoginError("Enter your AYON server address.")
    if "://" not in url:
        url = "https://" + url
    parsed = urlsplit(url)
    if (
        parsed.scheme not in ("http", "https")
        or not parsed.hostname
        or parsed.username is not None
        or parsed.password is not None
        or parsed.query
        or parsed.fragment
    ):
        raise LoginError(
            "Use an HTTP or HTTPS server address without credentials,"
            " query parameters, or a fragment."
        )
    url = validate_url(url, timeout=REQUEST_TIMEOUT)
    api = ayon_api.ServerAPI(url, timeout=REQUEST_TIMEOUT, max_retries=0)
    version = api.server_version_tuple
    if len(version) < 3 or not all(
        isinstance(part, int) for part in version[:3]
    ):
        raise LoginError("This address did not return an AYON server version.")
    return url, tuple(version[:3]) >= (1, 3, 2)

fetch_avatar(url, token, username)

Return the round avatar of the user as a data URL, or ''.

Source code in common/ayon_common/connection/ui/qml_login.py
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
def fetch_avatar(url, token, username):
    """Return the round avatar of the user as a data URL, or ''."""
    api = ayon_api.ServerAPI(
        url, token=token, timeout=REQUEST_TIMEOUT, max_retries=0
    )
    response = api.raw_get(f"users/{username}/avatar")
    # Users without an avatar get generated SVG initials; QML draws those.
    if response.status_code != 200 or "svg" in (response.content_type or ""):
        return ""
    image = QtGui.QImage.fromData(response.content)
    if image.isNull():
        return ""
    side = min(image.width(), image.height())
    image = image.copy(
        (image.width() - side) // 2, (image.height() - side) // 2, side, side
    ).scaled(
        AVATAR_SIZE, AVATAR_SIZE,
        QtCore.Qt.IgnoreAspectRatio, QtCore.Qt.SmoothTransformation,
    )
    avatar = QtGui.QImage(
        AVATAR_SIZE, AVATAR_SIZE, QtGui.QImage.Format_ARGB32_Premultiplied
    )
    avatar.fill(QtCore.Qt.transparent)
    painter = QtGui.QPainter(avatar)
    painter.setRenderHint(QtGui.QPainter.Antialiasing)
    painter.setPen(QtCore.Qt.NoPen)
    painter.setBrush(QtGui.QBrush(image))
    painter.drawEllipse(0, 0, AVATAR_SIZE, AVATAR_SIZE)
    painter.end()
    return _image_data_url(avatar)

fetch_studio_image(url, kind)

Return the studio logo or login background as a data URL, or ''.

Source code in common/ayon_common/connection/ui/qml_login.py
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
def fetch_studio_image(url, kind):
    """Return the studio logo or login background as a data URL, or ''."""
    keys, (width, height), image_format = STUDIO_IMAGES[kind]
    api = ayon_api.ServerAPI(url, timeout=REQUEST_TIMEOUT, max_retries=0)
    info = api.raw_get(
        "info", params={"full": "true"}, handle_invalid_token=False
    ).data
    path = next((info[key] for key in keys if info.get(key)), None)
    if not path:
        return ""
    base_url = api.get_base_url()
    image_url = urljoin(base_url + "/", path)
    # Only load images from the server the user is connecting to.
    if not image_url.startswith(base_url + "/"):
        return ""
    response = api.raw_get(image_url, handle_invalid_token=False)
    if response.status_code != 200:
        return ""
    image = QtGui.QImage.fromData(response.content)
    if image.isNull():
        return ""
    if image.width() > width or image.height() > height:
        image = image.scaled(
            width, height,
            QtCore.Qt.KeepAspectRatio, QtCore.Qt.SmoothTransformation,
        )
    return _image_data_url(image, image_format)

forget_server(url)

Remove server url and its stored login token from this machine.

Source code in common/ayon_common/connection/ui/qml_login.py
79
80
81
82
83
84
85
86
87
88
89
90
91
def forget_server(url):
    """Remove server url and its stored login token from this machine."""
    from ayon_common.connection.credentials import (
        remove_server,
        remove_url_cache,
    )

    remove_server(url)
    try:
        remove_url_cache(url)
    except Exception:
        # Keyring may not be available, the url is removed anyway.
        log.warning("Failed to remove token of %s", url, exc_info=True)

get_known_servers()

Urls of servers used on this machine, most recently used first.

Source code in common/ayon_common/connection/ui/qml_login.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def get_known_servers():
    """Urls of servers used on this machine, most recently used first."""
    # Imported here, the credentials module imports the login UI.
    from ayon_common.connection.credentials import get_servers_info_data

    try:
        urls = get_servers_info_data().get("urls") or {}
    except Exception:
        return []
    if not isinstance(urls, dict):
        return []

    def _sort_key(item):
        info = item[1] if isinstance(item[1], dict) else {}
        # Format 'YYYY/MM/DD HH:MM:SS' sorts chronologically as a string.
        return str(info.get("updated_dt") or "")

    known = []
    for url, _ in sorted(urls.items(), key=_sort_key, reverse=True):
        if isinstance(url, str) and url.strip():
            known.append(url)
    return known

get_token_user(url, token)

Return the authenticated user, or None for an invalid token.

Source code in common/ayon_common/connection/ui/qml_login.py
135
136
137
138
139
140
141
142
143
def get_token_user(url, token):
    """Return the authenticated user, or None for an invalid token."""
    api = ayon_api.ServerAPI(
        url, token=token, timeout=REQUEST_TIMEOUT, max_retries=0
    )
    try:
        return api.get_user()
    except UnauthorizedError:
        return None

get_token_username(url, token)

Return the authenticated username, or None for an invalid token.

Source code in common/ayon_common/connection/ui/qml_login.py
146
147
148
149
def get_token_username(url, token):
    """Return the authenticated username, or None for an invalid token."""
    user = get_token_user(url, token)
    return user.get("name") if user else None

saved_login(url, token, expected_username)

Check supplied credentials; let network errors propagate for retry.

Source code in common/ayon_common/connection/ui/qml_login.py
152
153
154
155
156
157
158
def saved_login(url, token, expected_username):
    """Check supplied credentials; let network errors propagate for retry."""
    user = get_token_user(url, token)
    username = user.get("name") if user else None
    if not username or (expected_username and username != expected_username):
        return None
    return url, token, username, user