.. include:: ./../defs/external_refs.txt .. _logging_into_sciserver_locally: ============================== Logging into SciServer Locally ============================== After :ref:`installing astropathdb `, create an :class:`~.AstroDB` using an available credential source: .. code-block:: python from astropathdb import AstroDB db = AstroDB() Source selection ================ Outside SciServer Compute, authentication selects the first applicable source: #. Explicit ``username`` and ``password``. #. Explicit ``credential_filename``. #. Explicit ``username`` without a password. A password without a username is invalid. #. ``ASTROPATHDB_TOKEN``. #. The existing SciScript session token, including its ``--ident`` behavior. #. The file configured by ``ASTROPATHDB_CREDENTIALS_FILE``. #. ``ASTROPATHDB_USERNAME`` and ``ASTROPATHDB_PASSWORD``. #. ``scicreds/sciserver_creds.txt`` in a source checkout. Explicit arguments never conflict: a higher-ranked argument is used and the rest are ignored. A selected source is authoritative. Invalid or incomplete data does not fall through to another account. A selected username with a missing password can use that exact account's keyring entry; otherwise authentication fails without prompting. Empty environment values count as absent. No username is combined with a password from another file or environment source. A failed login never falls back to an older token. The only file searched automatically is ``scicreds/sciserver_creds.txt`` at the root of the package's source checkout. It is skipped when missing; a malformed or incomplete file fails at that source. Explicit and environment-configured files must exist and contain usable credentials. Their relative paths use the current working directory, and ``~`` expands to the home directory. Credential file formats ======================= A ``.env`` recognizes these fields; a token can be supplied instead of the pair: .. code-block:: text ASTROPATHDB_USERNAME=account ASTROPATHDB_PASSWORD='password supplied securely' # Alternatively: ASTROPATHDB_TOKEN=token supplied securely Dotenv values do not modify the process environment. Variable interpolation is disabled, so a password containing ``${HOME}`` stays literal. Quoted password whitespace is preserved. Malformed dotenv input fails, even if some fields could be parsed. JSON files contain an object with string ``username``, ``password``, or ``token`` fields; ``null`` means absent. A nonempty token takes priority within the file: .. code-block:: json {"username": "account", "password": "password supplied securely"} Text files contain the username and password on the first two lines. Both lines are stripped of surrounding whitespace; additional lines are ignored. Direct, JSON, and dotenv passwords retain their nonempty contents exactly. Keep credential files private and out of version control. No authentication option automatically writes a file. Optional keyring storage ======================== Install the extra and configure an available operating-system keyring backend: .. code-block:: console uv pip install 'astropathdb[keyring]' Saving is requested on the credential manager rather than on :class:`~.AstroDB`. It writes only after a successful login and replaces that account's password under service ``astropathdb``: .. code-block:: python from getpass import getpass from astropathdb.db_io.credentials import CredentialManager CredentialManager(store="keyring").login("account", getpass()) # A subsequent explicit account selection can use its stored password. db = AstroDB(username="account") Without ``store="keyring"`` or ``store="both"``, no keyring write occurs. Missing or inaccessible keyrings during automatic lookup leave the password unresolved. Saving requires a complete resolved username/password pair before login; token-only sources and Compute sessions cannot be saved. A failed write raises an error stating that the session is already authenticated, and prevents token export. Sharing the process token ========================= Export is opt-in and happens after authentication and any requested keyring write: .. code-block:: python CredentialManager(store="env").login("account", getpass()) Token-only authentication, from ``ASTROPATHDB_TOKEN``, a credential file, or ``CredentialManager().login(token=token)``, registers the token with SciScript without a login request. Other code in the same process can use either SciScript's active session or the exported environment token: .. code-block:: python import os from SciServer import Authentication db = AstroDB() # selects ASTROPATHDB_TOKEN when it is set token = os.environ.get("ASTROPATHDB_TOKEN") active_token = Authentication.getToken() # Do not print or log either token. ``store="env"`` or ``store="both"`` replaces ``ASTROPATHDB_TOKEN``. Without export, the variable remains unchanged. A later automatic ``AstroDB()`` can therefore select an older environment token ahead of the current SciScript session. Authentication is process-wide: instances do not own independent identities. Existing ``db_connection`` objects bypass credential discovery and persistence entirely. Reusing or registering a token does not validate it with the server. After a 401 response, reauthenticate with explicit credentials, or register a replacement token through ``CredentialManager``; a bare :meth:`~.SciBase.sci_login` may reuse the rejected session. Authentication never prompts for input, including in notebooks. Environment export lasts for the current process and can be inherited by subsequently launched child processes. It does not update the parent shell, persist across restarts, or synchronize separate notebook kernels. Tokens can be exposed through debugging, process inspection, inherited environments, and SciScript's existing ``--ident`` handling. Never print or log tokens or passwords.