# HG changeset patch # User Vincent Hatakeyama <vincent.hatakeyama@xcg-consulting.fr> # Date 1698070164 -7200 # Mon Oct 23 16:09:24 2023 +0200 # Node ID 191bbd187e9f5fdf68444898538ee39d30042a09 # Parent 41776ed1756ef49d27726b6396edcb845bed40e0 ✨ Add option to install store api token in storage diff --git a/NEWS.rst b/NEWS.rst --- a/NEWS.rst +++ b/NEWS.rst @@ -2,6 +2,11 @@ History ======= +20.12.0 +------- + +Use SecretStorage to store Orus API token. + 20.11.0 ------- diff --git a/odoo_scripts/docker_build_copy.py b/odoo_scripts/docker_build_copy.py --- a/odoo_scripts/docker_build_copy.py +++ b/odoo_scripts/docker_build_copy.py @@ -17,6 +17,7 @@ from .config import Config from .list_modules import list_modules from .parsing import apply, basic_parser +from .storage import get_orus_api_token from .toml import load _logger = logging.getLogger(__name__) @@ -52,7 +53,11 @@ class Configuration(Mapping): def __init__(self, namespace): super().__init__() - self.orus_api_token = namespace.orus_api_token + self.orus_api_token = ( + namespace.orus_api_token + if namespace.orus_api_token + else get_orus_api_token() + ) def __len__(self): return 1 diff --git a/odoo_scripts/storage.py b/odoo_scripts/storage.py new file mode 100644 --- /dev/null +++ b/odoo_scripts/storage.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +import sys +from contextlib import closing +from getpass import getpass +from typing import Optional + +import secretstorage + +__version__ = "1.0.0" +__date__ = "2023-10-23" +__updated__ = "2023-10-23" + +__orus_api_token_attributes = { + "application": "odoo_scripts", + "server": "orus.io", + "scheme": "https", + "type": "api_token", +} + + +def get_orus_api_token() -> Optional[str]: + """Retrieve Orus API token from secret service. + + :return: None if there is no token""" + with closing(secretstorage.dbus_init()) as conn: + collection = secretstorage.get_default_collection(conn) + for item in collection.search_items(__orus_api_token_attributes): + return item.get_secret().decode("UTF-8") + return None + + +def set_orus_api_token(token: str): + """Store Orus API token from secret service""" + with closing(secretstorage.dbus_init()) as conn: + collection = secretstorage.get_default_collection(conn) + collection.create_item( + "Orus API Token for Odoo Scripts", + __orus_api_token_attributes, + token.encode("UTF-8"), + True, + ) + + +def main() -> int: + """Ask for token and set it""" + token = getpass("Type in token value followed by enter:") + set_orus_api_token(token) + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/pyproject.toml b/pyproject.toml --- a/pyproject.toml +++ b/pyproject.toml @@ -41,6 +41,7 @@ "requests_unixsocket", "requests", "psycopg2", + "secretstorage >=3.3.1", ] import_sql = ["psycopg2"] source_control = [ @@ -71,6 +72,7 @@ list_modules = "odoo_scripts.list_modules:main" update_duplicate_sources = "odoo_scripts.update_duplicate_sources:main [source_control]" docker_redis = "odoo_scripts.docker_redis:main [docker]" +store_orus_api_token = "odoo_scripts.storage:main" [project.urls] repository = "https://orus.io/xcg/odoo_scripts"