# Single Sign On (SSO)

class proton.sso.ProtonSSO(appversion: str = 'Other', user_agent: str = 'None', keyring_backend_name=None)

  Proton Single Sign On implementation. This allows session persistence for the current user.

  The general approach for this is to create a SSO instance, and then to get either a specific or the default session, and work from there:

      from proton.sso import ProtonSSO
      sso = ProtonSSO()
      session = sso.get_default_session()
      # or:
      session = sso.get_session('pro') # get session for account pro

  Note that it is advised not to try to “guess” the state of the session, but instead to just try to use it, and handle any exception that would arise.

  This object uses advisory locks (using `flock`) to protect the session from multiple conflicting changes. This does not guarantee that Session objects are immune to what happens in another process (i.e. imagine if one process terminates the session.), but at least makes it consistent. In the future, it would be nice to use an IPC mechanism to make sure other processes are aware of the state change.

  __init__(appversion: str = 'Other', user_agent: str = 'None', keyring_backend_name=None)

    Create a SSO instance

    Parameters:

      - appversion (str, optional) – Application version (see `proton.session.Session`), defaults to “Other”

      - user_agent (str, optional) – User agent version (see `proton.session.Session`), defaults to “None”

      - keyring_backend_name (str, optional) – Name of Keyring backend to load (see `proton.keyring.Keyring`), defaults to None

  property sessions: list[str]

    Returns the account names for the current system user

    Returns:

      list of normalized account_names

    Return type:

      list[str]

  get_session(account_name: str | None, override_class: type | None = None) → Session

    Get the session identified by account_name

    Parameters:

      - account_name (Optional[str]) – account name to use. If None will return an empty session (can be used as a factory)

      - override_class (Optional[type]) – Class to use for the session to be returned, by default will use proton.session.Session

    Returns:

      the Session object. It will be an empty session if there’s no session for account_name

    Return type:

      Session

  get_default_session(override_class: type | None = None) → Session

    Get the default session for the system user. It will always be one valid session if one exists.

    Parameters:

      override_class (Optional[type]) – Class to use for the session to be returned, see `get_session()`.

    Returns:

      the Session object. It will be an empty session if there’s no session at all

    Return type:

      Session

  set_default_account(account_name: str)

    Set the default account for user to be account_name

    Parameters:

      account_name (str) – the account_name to use as default

    Raises:

      KeyError – if the account name is unknown

  _acquire_session_lock(account_name: str, current_data: dict) → None

    Observer pattern for `proton.session.Session` (see `proton.session.Session.register_persistence_observer()`). It is called when the Session object is getting locked, because it’s expected to be changed and we want to avoid race conditions.

    Parameters:

      - account_name (str) – account name of the session

      - current_data (dict) – current session data serialized as a dictionary

  _release_session_lock(account_name: str, new_data: dict) → None

    Observer pattern for `proton.session.Session` (see `proton.session.Session.register_persistence_observer()`). It is called when the Session object is getting unlocked.

    If the data between has changed since `_acquire_session_lock()` was called, it will be persisted in the keyring.

    Parameters:

      - account_name (str) – account name of the session

      - new_data (dict) – current session data serialized as a dictionary