From 2b086b68c9d4a1e5596bf8f7d90a51f8ccf9e467 Mon Sep 17 00:00:00 2001 From: "Andriy Kushnir (Orhideous)" Date: Fri, 1 Mar 2024 21:11:03 +0200 Subject: [PATCH 1/3] Add API to obtain credentials from cloud --- poetry.lock | 175 ++++++++++++++++++++++++++++++++++++- pyproject.toml | 2 + roombapy/cloud/__init__.py | 1 + roombapy/cloud/login.py | 59 +++++++++++++ roombapy/cloud/models.py | 97 ++++++++++++++++++++ 5 files changed, 333 insertions(+), 1 deletion(-) create mode 100644 roombapy/cloud/__init__.py create mode 100644 roombapy/cloud/login.py create mode 100644 roombapy/cloud/models.py diff --git a/poetry.lock b/poetry.lock index 56e1707..6850ac0 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,5 +1,16 @@ # This file is automatically @generated by Poetry 1.7.1 and should not be changed by hand. +[[package]] +name = "certifi" +version = "2024.2.2" +description = "Python package for providing Mozilla's CA Bundle." +optional = false +python-versions = ">=3.6" +files = [ + {file = "certifi-2024.2.2-py3-none-any.whl", hash = "sha256:dc383c07b76109f368f6106eee2b593b04a011ea4d55f652c6ca24a754d1cdd1"}, + {file = "certifi-2024.2.2.tar.gz", hash = "sha256:0569859f95fc761b18b45ef421b1290a0f65f147e92a1e5eb3e635f9a5e4e66f"}, +] + [[package]] name = "cfgv" version = "3.4.0" @@ -11,6 +22,105 @@ files = [ {file = "cfgv-3.4.0.tar.gz", hash = "sha256:e52591d4c5f5dead8e0f673fb16db7949d2cfb3f7da4582893288f0ded8fe560"}, ] +[[package]] +name = "charset-normalizer" +version = "3.3.2" +description = "The Real First Universal Charset Detector. Open, modern and actively maintained alternative to Chardet." +optional = false +python-versions = ">=3.7.0" +files = [ + {file = "charset-normalizer-3.3.2.tar.gz", hash = "sha256:f30c3cb33b24454a82faecaf01b19c18562b1e89558fb6c56de4d9118a032fd5"}, + {file = "charset_normalizer-3.3.2-cp310-cp310-macosx_10_9_universal2.whl", hash = "sha256:25baf083bf6f6b341f4121c2f3c548875ee6f5339300e08be3f2b2ba1721cdd3"}, + {file = "charset_normalizer-3.3.2-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:06435b539f889b1f6f4ac1758871aae42dc3a8c0e24ac9e60c2384973ad73027"}, + {file = "charset_normalizer-3.3.2-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:9063e24fdb1e498ab71cb7419e24622516c4a04476b17a2dab57e8baa30d6e03"}, + {file = "charset_normalizer-3.3.2-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:6897af51655e3691ff853668779c7bad41579facacf5fd7253b0133308cf000d"}, + {file = "charset_normalizer-3.3.2-cp310-cp310-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:1d3193f4a680c64b4b6a9115943538edb896edc190f0b222e73761716519268e"}, + {file = "charset_normalizer-3.3.2-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:cd70574b12bb8a4d2aaa0094515df2463cb429d8536cfb6c7ce983246983e5a6"}, + {file = "charset_normalizer-3.3.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:8465322196c8b4d7ab6d1e049e4c5cb460d0394da4a27d23cc242fbf0034b6b5"}, + {file = "charset_normalizer-3.3.2-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:a9a8e9031d613fd2009c182b69c7b2c1ef8239a0efb1df3f7c8da66d5dd3d537"}, + {file = "charset_normalizer-3.3.2-cp310-cp310-musllinux_1_1_aarch64.whl", hash = "sha256:beb58fe5cdb101e3a055192ac291b7a21e3b7ef4f67fa1d74e331a7f2124341c"}, + {file = "charset_normalizer-3.3.2-cp310-cp310-musllinux_1_1_i686.whl", hash = "sha256:e06ed3eb3218bc64786f7db41917d4e686cc4856944f53d5bdf83a6884432e12"}, + {file = "charset_normalizer-3.3.2-cp310-cp310-musllinux_1_1_ppc64le.whl", hash = "sha256:2e81c7b9c8979ce92ed306c249d46894776a909505d8f5a4ba55b14206e3222f"}, + {file = "charset_normalizer-3.3.2-cp310-cp310-musllinux_1_1_s390x.whl", hash = "sha256:572c3763a264ba47b3cf708a44ce965d98555f618ca42c926a9c1616d8f34269"}, + {file = "charset_normalizer-3.3.2-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:fd1abc0d89e30cc4e02e4064dc67fcc51bd941eb395c502aac3ec19fab46b519"}, + {file = "charset_normalizer-3.3.2-cp310-cp310-win32.whl", hash = "sha256:3d47fa203a7bd9c5b6cee4736ee84ca03b8ef23193c0d1ca99b5089f72645c73"}, + {file = "charset_normalizer-3.3.2-cp310-cp310-win_amd64.whl", hash = "sha256:10955842570876604d404661fbccbc9c7e684caf432c09c715ec38fbae45ae09"}, + {file = "charset_normalizer-3.3.2-cp311-cp311-macosx_10_9_universal2.whl", hash = "sha256:802fe99cca7457642125a8a88a084cef28ff0cf9407060f7b93dca5aa25480db"}, + {file = "charset_normalizer-3.3.2-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:573f6eac48f4769d667c4442081b1794f52919e7edada77495aaed9236d13a96"}, + {file = "charset_normalizer-3.3.2-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:549a3a73da901d5bc3ce8d24e0600d1fa85524c10287f6004fbab87672bf3e1e"}, + {file = "charset_normalizer-3.3.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:f27273b60488abe721a075bcca6d7f3964f9f6f067c8c4c605743023d7d3944f"}, + {file = "charset_normalizer-3.3.2-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:1ceae2f17a9c33cb48e3263960dc5fc8005351ee19db217e9b1bb15d28c02574"}, + {file = "charset_normalizer-3.3.2-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:65f6f63034100ead094b8744b3b97965785388f308a64cf8d7c34f2f2e5be0c4"}, + {file = "charset_normalizer-3.3.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:753f10e867343b4511128c6ed8c82f7bec3bd026875576dfd88483c5c73b2fd8"}, + {file = "charset_normalizer-3.3.2-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:4a78b2b446bd7c934f5dcedc588903fb2f5eec172f3d29e52a9096a43722adfc"}, + {file = "charset_normalizer-3.3.2-cp311-cp311-musllinux_1_1_aarch64.whl", hash = "sha256:e537484df0d8f426ce2afb2d0f8e1c3d0b114b83f8850e5f2fbea0e797bd82ae"}, + {file = "charset_normalizer-3.3.2-cp311-cp311-musllinux_1_1_i686.whl", hash = "sha256:eb6904c354526e758fda7167b33005998fb68c46fbc10e013ca97f21ca5c8887"}, + {file = "charset_normalizer-3.3.2-cp311-cp311-musllinux_1_1_ppc64le.whl", hash = "sha256:deb6be0ac38ece9ba87dea880e438f25ca3eddfac8b002a2ec3d9183a454e8ae"}, + {file = "charset_normalizer-3.3.2-cp311-cp311-musllinux_1_1_s390x.whl", hash = "sha256:4ab2fe47fae9e0f9dee8c04187ce5d09f48eabe611be8259444906793ab7cbce"}, + {file = "charset_normalizer-3.3.2-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:80402cd6ee291dcb72644d6eac93785fe2c8b9cb30893c1af5b8fdd753b9d40f"}, + {file = "charset_normalizer-3.3.2-cp311-cp311-win32.whl", hash = "sha256:7cd13a2e3ddeed6913a65e66e94b51d80a041145a026c27e6bb76c31a853c6ab"}, + {file = "charset_normalizer-3.3.2-cp311-cp311-win_amd64.whl", hash = "sha256:663946639d296df6a2bb2aa51b60a2454ca1cb29835324c640dafb5ff2131a77"}, + {file = "charset_normalizer-3.3.2-cp312-cp312-macosx_10_9_universal2.whl", hash = "sha256:0b2b64d2bb6d3fb9112bafa732def486049e63de9618b5843bcdd081d8144cd8"}, + {file = "charset_normalizer-3.3.2-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:ddbb2551d7e0102e7252db79ba445cdab71b26640817ab1e3e3648dad515003b"}, + {file = "charset_normalizer-3.3.2-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:55086ee1064215781fff39a1af09518bc9255b50d6333f2e4c74ca09fac6a8f6"}, + {file = "charset_normalizer-3.3.2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:8f4a014bc36d3c57402e2977dada34f9c12300af536839dc38c0beab8878f38a"}, + {file = "charset_normalizer-3.3.2-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:a10af20b82360ab00827f916a6058451b723b4e65030c5a18577c8b2de5b3389"}, + {file = "charset_normalizer-3.3.2-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:8d756e44e94489e49571086ef83b2bb8ce311e730092d2c34ca8f7d925cb20aa"}, + {file = "charset_normalizer-3.3.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:90d558489962fd4918143277a773316e56c72da56ec7aa3dc3dbbe20fdfed15b"}, + {file = "charset_normalizer-3.3.2-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:6ac7ffc7ad6d040517be39eb591cac5ff87416c2537df6ba3cba3bae290c0fed"}, + {file = "charset_normalizer-3.3.2-cp312-cp312-musllinux_1_1_aarch64.whl", hash = "sha256:7ed9e526742851e8d5cc9e6cf41427dfc6068d4f5a3bb03659444b4cabf6bc26"}, + {file = "charset_normalizer-3.3.2-cp312-cp312-musllinux_1_1_i686.whl", hash = "sha256:8bdb58ff7ba23002a4c5808d608e4e6c687175724f54a5dade5fa8c67b604e4d"}, + {file = "charset_normalizer-3.3.2-cp312-cp312-musllinux_1_1_ppc64le.whl", hash = "sha256:6b3251890fff30ee142c44144871185dbe13b11bab478a88887a639655be1068"}, + {file = "charset_normalizer-3.3.2-cp312-cp312-musllinux_1_1_s390x.whl", hash = "sha256:b4a23f61ce87adf89be746c8a8974fe1c823c891d8f86eb218bb957c924bb143"}, + {file = "charset_normalizer-3.3.2-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:efcb3f6676480691518c177e3b465bcddf57cea040302f9f4e6e191af91174d4"}, + {file = "charset_normalizer-3.3.2-cp312-cp312-win32.whl", hash = "sha256:d965bba47ddeec8cd560687584e88cf699fd28f192ceb452d1d7ee807c5597b7"}, + {file = "charset_normalizer-3.3.2-cp312-cp312-win_amd64.whl", hash = "sha256:96b02a3dc4381e5494fad39be677abcb5e6634bf7b4fa83a6dd3112607547001"}, + {file = "charset_normalizer-3.3.2-cp37-cp37m-macosx_10_9_x86_64.whl", hash = "sha256:95f2a5796329323b8f0512e09dbb7a1860c46a39da62ecb2324f116fa8fdc85c"}, + {file = "charset_normalizer-3.3.2-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:c002b4ffc0be611f0d9da932eb0f704fe2602a9a949d1f738e4c34c75b0863d5"}, + {file = "charset_normalizer-3.3.2-cp37-cp37m-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:a981a536974bbc7a512cf44ed14938cf01030a99e9b3a06dd59578882f06f985"}, + {file = "charset_normalizer-3.3.2-cp37-cp37m-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:3287761bc4ee9e33561a7e058c72ac0938c4f57fe49a09eae428fd88aafe7bb6"}, + {file = "charset_normalizer-3.3.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:42cb296636fcc8b0644486d15c12376cb9fa75443e00fb25de0b8602e64c1714"}, + {file = "charset_normalizer-3.3.2-cp37-cp37m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:0a55554a2fa0d408816b3b5cedf0045f4b8e1a6065aec45849de2d6f3f8e9786"}, + {file = "charset_normalizer-3.3.2-cp37-cp37m-musllinux_1_1_aarch64.whl", hash = "sha256:c083af607d2515612056a31f0a8d9e0fcb5876b7bfc0abad3ecd275bc4ebc2d5"}, + {file = "charset_normalizer-3.3.2-cp37-cp37m-musllinux_1_1_i686.whl", hash = "sha256:87d1351268731db79e0f8e745d92493ee2841c974128ef629dc518b937d9194c"}, + {file = "charset_normalizer-3.3.2-cp37-cp37m-musllinux_1_1_ppc64le.whl", hash = "sha256:bd8f7df7d12c2db9fab40bdd87a7c09b1530128315d047a086fa3ae3435cb3a8"}, + {file = "charset_normalizer-3.3.2-cp37-cp37m-musllinux_1_1_s390x.whl", hash = "sha256:c180f51afb394e165eafe4ac2936a14bee3eb10debc9d9e4db8958fe36afe711"}, + {file = "charset_normalizer-3.3.2-cp37-cp37m-musllinux_1_1_x86_64.whl", hash = "sha256:8c622a5fe39a48f78944a87d4fb8a53ee07344641b0562c540d840748571b811"}, + {file = "charset_normalizer-3.3.2-cp37-cp37m-win32.whl", hash = "sha256:db364eca23f876da6f9e16c9da0df51aa4f104a972735574842618b8c6d999d4"}, + {file = "charset_normalizer-3.3.2-cp37-cp37m-win_amd64.whl", hash = "sha256:86216b5cee4b06df986d214f664305142d9c76df9b6512be2738aa72a2048f99"}, + {file = "charset_normalizer-3.3.2-cp38-cp38-macosx_10_9_universal2.whl", hash = "sha256:6463effa3186ea09411d50efc7d85360b38d5f09b870c48e4600f63af490e56a"}, + {file = "charset_normalizer-3.3.2-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:6c4caeef8fa63d06bd437cd4bdcf3ffefe6738fb1b25951440d80dc7df8c03ac"}, + {file = "charset_normalizer-3.3.2-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:37e55c8e51c236f95b033f6fb391d7d7970ba5fe7ff453dad675e88cf303377a"}, + {file = "charset_normalizer-3.3.2-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:fb69256e180cb6c8a894fee62b3afebae785babc1ee98b81cdf68bbca1987f33"}, + {file = "charset_normalizer-3.3.2-cp38-cp38-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:ae5f4161f18c61806f411a13b0310bea87f987c7d2ecdbdaad0e94eb2e404238"}, + {file = "charset_normalizer-3.3.2-cp38-cp38-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:b2b0a0c0517616b6869869f8c581d4eb2dd83a4d79e0ebcb7d373ef9956aeb0a"}, + {file = "charset_normalizer-3.3.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:45485e01ff4d3630ec0d9617310448a8702f70e9c01906b0d0118bdf9d124cf2"}, + {file = "charset_normalizer-3.3.2-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:eb00ed941194665c332bf8e078baf037d6c35d7c4f3102ea2d4f16ca94a26dc8"}, + {file = "charset_normalizer-3.3.2-cp38-cp38-musllinux_1_1_aarch64.whl", hash = "sha256:2127566c664442652f024c837091890cb1942c30937add288223dc895793f898"}, + {file = "charset_normalizer-3.3.2-cp38-cp38-musllinux_1_1_i686.whl", hash = "sha256:a50aebfa173e157099939b17f18600f72f84eed3049e743b68ad15bd69b6bf99"}, + {file = "charset_normalizer-3.3.2-cp38-cp38-musllinux_1_1_ppc64le.whl", hash = "sha256:4d0d1650369165a14e14e1e47b372cfcb31d6ab44e6e33cb2d4e57265290044d"}, + {file = "charset_normalizer-3.3.2-cp38-cp38-musllinux_1_1_s390x.whl", hash = "sha256:923c0c831b7cfcb071580d3f46c4baf50f174be571576556269530f4bbd79d04"}, + {file = "charset_normalizer-3.3.2-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:06a81e93cd441c56a9b65d8e1d043daeb97a3d0856d177d5c90ba85acb3db087"}, + {file = "charset_normalizer-3.3.2-cp38-cp38-win32.whl", hash = "sha256:6ef1d82a3af9d3eecdba2321dc1b3c238245d890843e040e41e470ffa64c3e25"}, + {file = "charset_normalizer-3.3.2-cp38-cp38-win_amd64.whl", hash = "sha256:eb8821e09e916165e160797a6c17edda0679379a4be5c716c260e836e122f54b"}, + {file = "charset_normalizer-3.3.2-cp39-cp39-macosx_10_9_universal2.whl", hash = "sha256:c235ebd9baae02f1b77bcea61bce332cb4331dc3617d254df3323aa01ab47bd4"}, + {file = "charset_normalizer-3.3.2-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:5b4c145409bef602a690e7cfad0a15a55c13320ff7a3ad7ca59c13bb8ba4d45d"}, + {file = "charset_normalizer-3.3.2-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:68d1f8a9e9e37c1223b656399be5d6b448dea850bed7d0f87a8311f1ff3dabb0"}, + {file = "charset_normalizer-3.3.2-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:22afcb9f253dac0696b5a4be4a1c0f8762f8239e21b99680099abd9b2b1b2269"}, + {file = "charset_normalizer-3.3.2-cp39-cp39-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:e27ad930a842b4c5eb8ac0016b0a54f5aebbe679340c26101df33424142c143c"}, + {file = "charset_normalizer-3.3.2-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:1f79682fbe303db92bc2b1136016a38a42e835d932bab5b3b1bfcfbf0640e519"}, + {file = "charset_normalizer-3.3.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:b261ccdec7821281dade748d088bb6e9b69e6d15b30652b74cbbac25e280b796"}, + {file = "charset_normalizer-3.3.2-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:122c7fa62b130ed55f8f285bfd56d5f4b4a5b503609d181f9ad85e55c89f4185"}, + {file = "charset_normalizer-3.3.2-cp39-cp39-musllinux_1_1_aarch64.whl", hash = "sha256:d0eccceffcb53201b5bfebb52600a5fb483a20b61da9dbc885f8b103cbe7598c"}, + {file = "charset_normalizer-3.3.2-cp39-cp39-musllinux_1_1_i686.whl", hash = "sha256:9f96df6923e21816da7e0ad3fd47dd8f94b2a5ce594e00677c0013018b813458"}, + {file = "charset_normalizer-3.3.2-cp39-cp39-musllinux_1_1_ppc64le.whl", hash = "sha256:7f04c839ed0b6b98b1a7501a002144b76c18fb1c1850c8b98d458ac269e26ed2"}, + {file = "charset_normalizer-3.3.2-cp39-cp39-musllinux_1_1_s390x.whl", hash = "sha256:34d1c8da1e78d2e001f363791c98a272bb734000fcef47a491c1e3b0505657a8"}, + {file = "charset_normalizer-3.3.2-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:ff8fa367d09b717b2a17a052544193ad76cd49979c805768879cb63d9ca50561"}, + {file = "charset_normalizer-3.3.2-cp39-cp39-win32.whl", hash = "sha256:aed38f6e4fb3f5d6bf81bfa990a07806be9d83cf7bacef998ab1a9bd660a581f"}, + {file = "charset_normalizer-3.3.2-cp39-cp39-win_amd64.whl", hash = "sha256:b01b88d45a6fcb69667cd6d2f7a9aeb4bf53760d7fc536bf679ec94fe9f3ff3d"}, + {file = "charset_normalizer-3.3.2-py3-none-any.whl", hash = "sha256:3e4d1f6587322d2788836a99c69062fbb091331ec940e02d12d179c1d53e25fc"}, +] + [[package]] name = "click" version = "8.1.7" @@ -108,6 +218,17 @@ files = [ [package.extras] license = ["ukkonen"] +[[package]] +name = "idna" +version = "3.6" +description = "Internationalized Domain Names in Applications (IDNA)" +optional = false +python-versions = ">=3.5" +files = [ + {file = "idna-3.6-py3-none-any.whl", hash = "sha256:c05567e9c24a6b9faaa835c4821bad0590fbb9d5779e7caa6e1cc4978e7eb24f"}, + {file = "idna-3.6.tar.gz", hash = "sha256:9ecdbbd083b06798ae1e86adcbfe8ab1479cf864e4ee30fe4e46a003d12491ca"}, +] + [[package]] name = "iniconfig" version = "2.0.0" @@ -457,6 +578,27 @@ files = [ {file = "PyYAML-6.0.1.tar.gz", hash = "sha256:bfdf460b1736c775f2ba9f6a92bca30bc2095067b8a9d77876d1fad6cc3b4a43"}, ] +[[package]] +name = "requests" +version = "2.31.0" +description = "Python HTTP for Humans." +optional = false +python-versions = ">=3.7" +files = [ + {file = "requests-2.31.0-py3-none-any.whl", hash = "sha256:58cd2187c01e70e6e26505bca751777aa9f2ee0b7f4300988b709f44e013003f"}, + {file = "requests-2.31.0.tar.gz", hash = "sha256:942c5a758f98d790eaed1a29cb6eefc7ffb0d1cf7af05c3d2791656dbd6ad1e1"}, +] + +[package.dependencies] +certifi = ">=2017.4.17" +charset-normalizer = ">=2,<4" +idna = ">=2.5,<4" +urllib3 = ">=1.21.1,<3" + +[package.extras] +socks = ["PySocks (>=1.5.6,!=1.5.7)"] +use-chardet-on-py3 = ["chardet (>=3.0.2,<6)"] + [[package]] name = "ruamel-yaml" version = "0.18.6" @@ -612,6 +754,20 @@ files = [ {file = "types_paho_mqtt-1.6.0.20240106-py3-none-any.whl", hash = "sha256:1d233c2c017a512ebbec24d6a90d94302767c75a33a7c2584a660eac7fade248"}, ] +[[package]] +name = "types-requests" +version = "2.31.0.20240218" +description = "Typing stubs for requests" +optional = false +python-versions = ">=3.8" +files = [ + {file = "types-requests-2.31.0.20240218.tar.gz", hash = "sha256:f1721dba8385958f504a5386240b92de4734e047a08a40751c1654d1ac3349c5"}, + {file = "types_requests-2.31.0.20240218-py3-none-any.whl", hash = "sha256:a82807ec6ddce8f00fe0e949da6d6bc1fbf1715420218a9640d695f70a9e5a9b"}, +] + +[package.dependencies] +urllib3 = ">=2" + [[package]] name = "types-tabulate" version = "0.9.0.20240106" @@ -634,6 +790,23 @@ files = [ {file = "typing_extensions-4.10.0.tar.gz", hash = "sha256:b0abd7c89e8fb96f98db18d86106ff1d90ab692004eb746cf6eda2682f91b3cb"}, ] +[[package]] +name = "urllib3" +version = "2.2.1" +description = "HTTP library with thread-safe connection pooling, file post, and more." +optional = false +python-versions = ">=3.8" +files = [ + {file = "urllib3-2.2.1-py3-none-any.whl", hash = "sha256:450b20ec296a467077128bff42b73080516e71b56ff59a60a02bef2232c4fa9d"}, + {file = "urllib3-2.2.1.tar.gz", hash = "sha256:d0570876c61ab9e520d776c38acbbb5b05a776d3f9ff98a5c8fd5162a444cf19"}, +] + +[package.extras] +brotli = ["brotli (>=1.0.9)", "brotlicffi (>=0.8.0)"] +h2 = ["h2 (>=4,<5)"] +socks = ["pysocks (>=1.5.6,!=1.5.7,<2.0)"] +zstd = ["zstandard (>=0.18.0)"] + [[package]] name = "virtualenv" version = "20.25.1" @@ -660,4 +833,4 @@ cli = ["click", "tabulate"] [metadata] lock-version = "2.0" python-versions = ">=3.10,<4.0" -content-hash = "82c3185a6043d5b18fe25232a9c49b894c64b67f34ea2e539c3a1c9283cc4cd4" +content-hash = "5e7a89d6d02d92e7b730794bffec1eb183d5f3e7105ffcd80424abf8a052abed" diff --git a/pyproject.toml b/pyproject.toml index 4cb9468..9390c88 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,6 +27,7 @@ paho-mqtt = "~1.6.1" mashumaro = {version = "^3.12"} click = { version = "^8.1", optional = true } tabulate = { version = "^0.9", optional = true } +requests = "^2.31.0" [tool.poetry.extras] cli = ["click", "tabulate"] @@ -41,6 +42,7 @@ codespell = "^2.2.6" mypy = "^1.8" types-paho-mqtt = "~1.6.0" types-tabulate = "~0.9.0" +types-requests = "^2.31.0" [build-system] requires = ["poetry-core>=1.0.0"] diff --git a/roombapy/cloud/__init__.py b/roombapy/cloud/__init__.py new file mode 100644 index 0000000..0a768f8 --- /dev/null +++ b/roombapy/cloud/__init__.py @@ -0,0 +1 @@ +"""Remote robot control.""" diff --git a/roombapy/cloud/login.py b/roombapy/cloud/login.py new file mode 100644 index 0000000..a6fb5f6 --- /dev/null +++ b/roombapy/cloud/login.py @@ -0,0 +1,59 @@ +"""Authentication flow for iRobot cloud.""" + +from __future__ import annotations + +import logging + +import requests + +from roombapy.cloud import models + +logger = logging.getLogger(__name__) + +TIMEOUT = 10 +APPLICATION_ID = "ANDROID-C7FB240E-DF34-42D7-AE4E-A8C17079A294" +DISCOVERY_URL = ( + "https://disc-prod.iot.irobotapi.com/v1/discover/endpoints?country_code=US" +) +GIGYA_LOGIN_URL_TEMPLATE = "https://accounts.%s/accounts.login" +IROBOT_LOGIN_ENDPOINT = "/v2/login" + +LoginResponse = tuple[models.Deployment, models.IRobotLoginResponse] + + +def login(username: str, password: str) -> LoginResponse: + """Obtain access credentials and robots' details from cloud.""" + response = requests.get(DISCOVERY_URL, timeout=TIMEOUT) + deployments = models.DeploymentsResponse.from_json(response.text) + deployment = deployments.deployments[deployments.current_deployment] + gigya = deployments.gigya + + gigya_login_url = GIGYA_LOGIN_URL_TEMPLATE % gigya.datacenter_domain + gigya_login_payload = { + "apiKey": gigya.api_key, + "loginID": username, + "password": password, + "format": "json", + "targetEnv": "mobile", + } + response = requests.post( + gigya_login_url, data=gigya_login_payload, timeout=TIMEOUT + ) + gigya_response = models.GigyaLoginResponse.from_json(response.text) + + irobot_login_url = f"{deployment.http_base}{IROBOT_LOGIN_ENDPOINT}" + irobot_login_payload = { + "app_id": APPLICATION_ID, + "assume_robot_ownership": "0", + "gigya": { + "signature": gigya_response.signature, + "timestamp": gigya_response.signature_timestamp, + "uid": gigya_response.user_id, + }, + } + response = requests.post( + irobot_login_url, json=irobot_login_payload, timeout=TIMEOUT + ) + irobot_response = models.IRobotLoginResponse.from_json(response.text) + + return deployment, irobot_response diff --git a/roombapy/cloud/models.py b/roombapy/cloud/models.py new file mode 100644 index 0000000..4b8dea6 --- /dev/null +++ b/roombapy/cloud/models.py @@ -0,0 +1,97 @@ +"""DTOs for cloud services.""" + +from __future__ import annotations + +from dataclasses import dataclass, field +from datetime import datetime # noqa: TCH003 — mashumaro need this in runtime + +from mashumaro import field_options +from mashumaro.config import BaseConfig +from mashumaro.mixins.orjson import DataClassORJSONMixin + + +@dataclass +class AWSCredentials(DataClassORJSONMixin): + """AWS credentials to query iRobot cloud API.""" + + access_key: str = field(metadata=field_options(alias="AccessKeyId")) + secret_key: str = field(metadata=field_options(alias="SecretKey")) + session_token: str = field(metadata=field_options(alias="SessionToken")) + expiration: datetime = field(metadata=field_options(alias="Expiration")) + + +@dataclass +class Deployment(DataClassORJSONMixin): + """Active deployment of iRobot services.""" + + aws_region: str = field(metadata=field_options(alias="awsRegion")) + http_base: str = field(metadata=field_options(alias="httpBase")) + http_base_auth: str = field(metadata=field_options(alias="httpBaseAuth")) + + +@dataclass +class GigyaData(DataClassORJSONMixin): + """Gigya service discovery.""" + + api_key: str + datacenter_domain: str + + +@dataclass +class DeploymentsResponse(DataClassORJSONMixin): + """iRobot service discovery.""" + + deployments: dict[str, Deployment] + current_deployment: str + gigya: GigyaData + + +@dataclass +class GigyaLoginResponse(DataClassORJSONMixin): + """User profile.""" + + error_code: int + api_version: int + status_code: int + status_reason: str + user_id: str + signature: str + signature_timestamp: str + is_active: bool + is_registered: bool + is_verified: bool + + class Config(BaseConfig): + """Camel-case all fields.""" + + aliases: dict[str, str] = { # noqa: RUF012 — conflicts with mypy + "error_code": "errorCode", + "api_version": "apiVersion", + "status_code": "statusCode", + "status_reason": "statusReason", + "user_id": "UID", + "signature": "UIDSignature", + "signature_timestamp": "signatureTimestamp", + "is_active": "isActive", + "is_registered": "isRegistered", + "is_verified": "isVerified", + } + + +@dataclass +class CloudRobotInfo(DataClassORJSONMixin): + """Connected iRobot device.""" + + firmware: str = field(metadata=field_options(alias="softwareVer")) + robot_name: str = field(metadata=field_options(alias="name")) + sku: str + capabilities: dict[str, int] = field(metadata=field_options(alias="cap")) + password: str + + +@dataclass +class IRobotLoginResponse(DataClassORJSONMixin): + """Access credentials and connected devices.""" + + credentials: AWSCredentials + robots: dict[str, CloudRobotInfo] From f917340abcecf7536026ffc36531397a57e39592 Mon Sep 17 00:00:00 2001 From: "Andriy Kushnir (Orhideous)" Date: Mon, 4 Mar 2024 13:21:23 +0200 Subject: [PATCH 2/3] Add full domain model for maps --- pyproject.toml | 14 + roombapy/cloud/login.py | 2 +- roombapy/cloud/models/__init__.py | 1 + roombapy/cloud/{models.py => models/login.py} | 2 +- roombapy/cloud/models/map/__init__.py | 1 + roombapy/cloud/models/map/full.py | 305 ++++++++++++++++++ roombapy/cloud/models/serialization.py | 59 ++++ 7 files changed, 382 insertions(+), 2 deletions(-) create mode 100644 roombapy/cloud/models/__init__.py rename roombapy/cloud/{models.py => models/login.py} (98%) create mode 100644 roombapy/cloud/models/map/__init__.py create mode 100644 roombapy/cloud/models/map/full.py create mode 100644 roombapy/cloud/models/serialization.py diff --git a/pyproject.toml b/pyproject.toml index 9390c88..eedf35b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -68,6 +68,13 @@ ignore = [ "PLR0912", ] select = ["ALL"] +[tool.ruff.lint.per-file-ignores] +# There are reverse-engineered classes +"roombapy/cloud/models/map/full.py" = [ + # Ruff is too dumb to realize that function calls return proper Field instance + "RUF009", + "D101" +] [tool.ruff.format] docstring-code-format = true @@ -78,6 +85,13 @@ strict_optional = true strict = true packages = ["roombapy", "tests"] +[[tool.mypy.overrides]] +module = "roombapy.cloud.models.map.full" +disable_error_code = "assignment" +[[tool.mypy.overrides]] +module = "roombapy.cloud.models.serialization" +disable_error_code = "no-any-return,type-arg,call-overload" + [tool.pydantic-mypy] init_forbid_extra = true init_typed = true diff --git a/roombapy/cloud/login.py b/roombapy/cloud/login.py index a6fb5f6..6a68df0 100644 --- a/roombapy/cloud/login.py +++ b/roombapy/cloud/login.py @@ -6,7 +6,7 @@ import requests -from roombapy.cloud import models +from roombapy.cloud.models import login as models logger = logging.getLogger(__name__) diff --git a/roombapy/cloud/models/__init__.py b/roombapy/cloud/models/__init__.py new file mode 100644 index 0000000..3850857 --- /dev/null +++ b/roombapy/cloud/models/__init__.py @@ -0,0 +1 @@ +"""DTOs for cloud APIs.""" diff --git a/roombapy/cloud/models.py b/roombapy/cloud/models/login.py similarity index 98% rename from roombapy/cloud/models.py rename to roombapy/cloud/models/login.py index 4b8dea6..947e2ef 100644 --- a/roombapy/cloud/models.py +++ b/roombapy/cloud/models/login.py @@ -1,4 +1,4 @@ -"""DTOs for cloud services.""" +"""DTOs for login.""" from __future__ import annotations diff --git a/roombapy/cloud/models/map/__init__.py b/roombapy/cloud/models/map/__init__.py new file mode 100644 index 0000000..4a77c36 --- /dev/null +++ b/roombapy/cloud/models/map/__init__.py @@ -0,0 +1 @@ +"""DTOs for iRobot maps.""" diff --git a/roombapy/cloud/models/map/full.py b/roombapy/cloud/models/map/full.py new file mode 100644 index 0000000..2504da8 --- /dev/null +++ b/roombapy/cloud/models/map/full.py @@ -0,0 +1,305 @@ +"""Full DTOs for map responses. + +NOTE: DTO below are still incomplete in a subtle ways + and may not work with some robots. +""" + +from __future__ import annotations + +from dataclasses import dataclass, field +from datetime import datetime # noqa: TCH003 — mashumaro need this in runtime +from enum import IntEnum, StrEnum +from typing import Any + +from mashumaro.config import BaseConfig +from mashumaro.mixins.orjson import DataClassORJSONMixin + +from roombapy.cloud.models.serialization import ( + alias, + irobot_dt_field, + irobot_ts_field, +) + + +class Confidence(StrEnum): + POOR_CONFIDENCE = "POOR_CONFIDENCE" + PARTIAL_CONFIDENCE = "PARTIAL_CONFIDENCE" + GOOD_CONFIDENCE = "GOOD_CONFIDENCE" + INVALID = "INVALID" + + +class MapAuthor(StrEnum): + ROBOT = "robot" + USER = "user" + + +class TimeEstimateUnit(StrEnum): + SECONDS = "seconds" + MINUTES = "minutes" + + +class RegionType(StrEnum): + BASEMENT = "basement" + BATHROOM = "bathroom" + BEDROOM = "bedroom" + BREAKFAST_ROOM = "breakfast_room" + CLOSET = "closet" + CUSTOM = "custom" + DEN = "den" + DINING_ROOM = "dining_room" + ENTRYWAY = "entryway" + FAMILY_ROOM = "family_room" + FOYER = "foyer" + GARAGE = "garage" + GUEST_BATHROOM = "guest_bathroom" + GUEST_BEDROOM = "guest_bedroom" + HALLWAY = "hallway" + KIDS_ROOM = "kids_room" + KITCHEN = "kitchen" + LAUNDRY_ROOM = "laundry_room" + LIVING_ROOM = "living_room" + LOUNGE = "lounge" + MEDIA_ROOM = "media_room" + MUD_ROOM = "mud_room" + OFFICE = "office" + OUTSIDE = "outside" + PANTRY = "pantry" + PLAYROOM = "playroom" + PRIMARY_BATHROOM = "primary_bathroom" + PRIMARY_BEDROOM = "primary_bedroom" + RECREATION_ROOM = "recreation_room" + STORAGE_ROOM = "storage_room" + STUDY = "study" + SUN_ROOM = "sun_room" + UNFINISHED_BASEMENT = "unfinished_basement" + UNKNOWN = "unknown" + WORKSHOP = "workshop" + MASTER_BEDROOM = "master_bedroom" + + +class ZoneType(StrEnum): + APPLIANCE = "appliance" + BUILT_IN = "built_in" + CHILDREN = "children" + FLOORING = "flooring" + FURNITURE = "furniture" + PET = "pet" + SEASONAL = "seasonal" + OTHER = "other" + UNSPECIFIED = "unspecified" + + +class ZoneExtentType(StrEnum): + AROUND_BED = "around_bed" + AROUND_SOFA = "around_sofa" + AROUND_COFFEE_TABLE = "around_coffee_table" + AROUND_DINING_TABLE = "around_dining_table" + AROUND_TOILET = "around_toilet" + AROUND_KITCHEN_ISLAND = "around_kitchen_island" + AROUND_LITTER_BOX = "around_litter_box" + AROUND_PET_BOWL = "around_pet_bowl" + FRONT_OF_KITCHEN_COUNTER = "front_of_kitchen_counter" + FRONT_OF_REFRIGERATOR = "front_of_refrigerator" + FRONT_OF_OVEN = "front_of_oven" + FRONT_OF_DISHWASHER = "front_of_dishwasher" + RUG = "rug" + AROUND_CHRISTMAS_TREE = "around_christmas_tree" + UNSPECIFIED = "unspecified" + + +class PadType(StrEnum): + DISPOSABLE = "disposable" + REUSABLE = "reusable" + + +class OdoaMode(IntEnum): + Unknown = 0 + EnabledForAllFeatures = 1 + EnabledForDocksOnly = 2 + Disabled = 3 + + +class PadWetnessLevel(IntEnum): + DAMP = 0 + MODERATE = 1 + WET = 2 + INVALID = 3 + + +class OperatingMode(IntEnum): + TRAVELING = 0 + VACUUMING = 1 + MOPPING = 2 + VIDEOSTREAMING = 3 + AIR_PURIFYING = 4 + COMBO_BIN_CLEANING = 5 + SCRUBBING = 6 + + +@dataclass +class TimeEstimateParams(DataClassORJSONMixin): + no_auto_passes: bool | None = None + two_pass: bool | None = None + no_koz: int | None = None + carpet_boost: bool | None = None + vac_high: bool | None = None + scrub: int | None = None + pad_wetness: dict[PadType, PadWetnessLevel] | None = None + operating_mode: OperatingMode | None = None + + class Config(BaseConfig): + """Camel-case all fields.""" + + aliases: dict[str, str] = { # noqa: RUF012 — conflicts with mypy + "no_auto_passes": "noAutoPasses", + "two_pass": "twoPass", + "no_koz": "noKOZ", + "carpet_boost": "carpetBoost", + "vac_high": "vacHigh", + "pad_wetness": "padWetness", + "operating_mode": "operatingMode", + } + + +@dataclass +class TimeEstimateInfo(DataClassORJSONMixin): + confidence: Confidence + estimate: int + unit: TimeEstimateUnit + params: TimeEstimateParams | None = None + + +@dataclass +class Region(DataClassORJSONMixin): + """Mapped rooms.""" + + id: str + name: str + region_type: RegionType + policies: ZonePolicy + time_estimates: list[TimeEstimateInfo] = field(default_factory=list) + + +@dataclass +class ZonePolicy(DataClassORJSONMixin): + disabled_operating_modes: int + odoa_mode: OdoaMode + override_operating_modes: int + odoa_feats: dict[str, int] + + +@dataclass +class ZoneDetailInfo(DataClassORJSONMixin): + extent_type: ZoneExtentType + id: str + name: str + policies: ZonePolicy + recommend_id: str + related_objects: list[str] + tags: list[str] + time_estimates: list[TimeEstimateInfo] + zone_name_resourceid: str + zone_type: ZoneType + + +@dataclass +class ObservedZoneQuality(DataClassORJSONMixin): + confidence: int + + +Geometry = dict[str, Any] # NOTE: all that extends SpatialGeometry class + + +@dataclass +class ObservedZoneInfo(DataClassORJSONMixin): + extent_type: ZoneExtentType + id: str + quality: ObservedZoneQuality + geometry: Geometry | None = None + related_objects: list[str] = field(default_factory=list) + + +@dataclass +class MapHeader(DataClassORJSONMixin): + id: str + learning_percentage: int + name: str + resolution: float + robot_orientation_rad: float + user_orientation_rad: float + create_time: datetime = irobot_ts_field("create_time") + version: datetime = irobot_dt_field() + mission_count: int | None = alias("nmssn", None) + mission_id: str | None = None + area: float | None = None + + +@dataclass +class KeepOutZone(DataClassORJSONMixin): + id: str + name: str + recommend_id: str + keep_out_zone_type: str = alias("keepoutzone_type") + + +@dataclass +class MapMetadata: + creator: MapAuthor + proc_state: str + create_time: datetime = irobot_ts_field("create_time") + user_version: datetime = irobot_dt_field("last_user_pmapv_id") + user_timestamp: datetime = irobot_ts_field("last_user_ts") + id: str = alias("pmap_id") + map_version: datetime = irobot_dt_field("pmapv_id") + learning_percentage: int | None = None + # NOTE: It's, actually, proper class instance + robot_cap: dict[str, int] | None = None + robot_id: str | None = None + shareability: int | None = None # NOTE: Enum, actually + mission_count: int | None = alias("nMssn", default=None) + + +@dataclass +class CopiedMap(DataClassORJSONMixin): + robot_id: str + map_version: datetime = irobot_dt_field("pmapv_id") + id: str = alias("pmap_id") + + +@dataclass +class MergedMap(DataClassORJSONMixin): + map_version: datetime = irobot_dt_field("pmapv_id") + id: str = alias("pmap_id") + ts: datetime = irobot_ts_field("ts") + + +@dataclass +class MapDetails(DataClassORJSONMixin): + meta: MapMetadata = alias("active_pmapv") + header: MapHeader = alias("map_header") + keep_out_zones: list[KeepOutZone] = alias( + "keepoutzones", default_factory=list + ) + observed_zones: list[ObservedZoneInfo] = field(default_factory=list) + regions: list[Region] = field(default_factory=list) + + +@dataclass +class Map(DataClassORJSONMixin): + shared: int + sku: str + state: str + visible: bool + id: str = alias("pmap_id") + details: MapDetails = alias("active_pmapv_details") + active_version: datetime = irobot_dt_field("active_pmapv_id") + robot_version: datetime = irobot_dt_field("robot_pmapv_id") + user_version: datetime = irobot_dt_field("user_pmapv_id") + last_version: datetime = irobot_ts_field("last_pmapv_ts") + create_time: datetime = irobot_ts_field() + robot_id: str | None = None + robot_ids: list[str] = field(default_factory=list) + copied_from: CopiedMap | None = None + merged_maps: list[MergedMap] = alias( + "merged_pmap_ids", default_factory=list + ) diff --git a/roombapy/cloud/models/serialization.py b/roombapy/cloud/models/serialization.py new file mode 100644 index 0000000..fa1fa6a --- /dev/null +++ b/roombapy/cloud/models/serialization.py @@ -0,0 +1,59 @@ +"""iRobot-specific serialization utilities.""" + +from __future__ import annotations + +from dataclasses import MISSING, Field, field +from datetime import UTC, datetime +from typing import Any + +from mashumaro import field_options +from mashumaro.types import SerializationStrategy + + +class _IRobotDT(SerializationStrategy): + _format = "%y%m%dT%H%M%S" + + def deserialize(self, value: str) -> datetime: + return datetime.strptime(value, self._format).replace(tzinfo=UTC) + + +class _IRobotTS(SerializationStrategy): + def deserialize(self, value: int) -> datetime: + return datetime.fromtimestamp(value, tz=UTC) + + +def irobot_dt_field(name: str | None = None) -> Field: + """Shorthand to deserialize a datetime field.""" + if name is None: + return field(metadata=field_options(serialization_strategy=_IRobotDT())) + return field( + metadata=field_options(alias=name, serialization_strategy=_IRobotDT()) + ) + + +def irobot_ts_field(name: str | None = None) -> Field: + """Shorthand to deserialize a timestamp field.""" + if name is None: + return field(metadata=field_options(serialization_strategy=_IRobotTS())) + return field( + metadata=field_options(alias=name, serialization_strategy=_IRobotTS()) + ) + + +def alias( + name: str, + default: Any = MISSING, + default_factory: Any = MISSING, +) -> Field: + """Shorthand for aliased field.""" + # noinspection PyArgumentList + return field( + metadata=field_options(alias=name), + default=default, + default_factory=default_factory, + ) + + +def check_map_response(payload: dict[str, Any]) -> bool: + """Check whether map is well-formed.""" + return "active_pmapv_details" in payload From dc0e516f6d7760c9d377d9e79f167f0d99f00df0 Mon Sep 17 00:00:00 2001 From: "Andriy Kushnir (Orhideous)" Date: Mon, 4 Mar 2024 20:04:48 +0200 Subject: [PATCH 3/3] Add AWS client --- poetry.lock | 12 +- pyproject.toml | 8 +- roombapy/cloud/aws.py | 273 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 282 insertions(+), 11 deletions(-) create mode 100644 roombapy/cloud/aws.py diff --git a/poetry.lock b/poetry.lock index 6850ac0..195f668 100644 --- a/poetry.lock +++ b/poetry.lock @@ -480,13 +480,13 @@ tomli = {version = ">=1.1.0", markers = "python_version < \"3.11\""} [[package]] name = "pytest" -version = "8.1.0" +version = "8.0.2" description = "pytest: simple powerful testing with Python" optional = false python-versions = ">=3.8" files = [ - {file = "pytest-8.1.0-py3-none-any.whl", hash = "sha256:ee32db7af8de4629a455806befa90559f307424c07b8413ccfc30bf5b221dd7e"}, - {file = "pytest-8.1.0.tar.gz", hash = "sha256:f8fa04ab8f98d185113ae60ea6d79c22f8143b14bc1caeced44a0ab844928323"}, + {file = "pytest-8.0.2-py3-none-any.whl", hash = "sha256:edfaaef32ce5172d5466b5127b42e0d6d35ebbe4453f0e3505d96afd93f6b096"}, + {file = "pytest-8.0.2.tar.gz", hash = "sha256:d4051d623a2e0b7e51960ba963193b09ce6daeb9759a451844a21e4ddedfc1bd"}, ] [package.dependencies] @@ -494,11 +494,11 @@ colorama = {version = "*", markers = "sys_platform == \"win32\""} exceptiongroup = {version = ">=1.0.0rc8", markers = "python_version < \"3.11\""} iniconfig = "*" packaging = "*" -pluggy = ">=1.4,<2.0" -tomli = {version = ">=1", markers = "python_version < \"3.11\""} +pluggy = ">=1.3.0,<2.0" +tomli = {version = ">=1.0.0", markers = "python_version < \"3.11\""} [package.extras] -testing = ["argcomplete", "attrs (>=19.2)", "hypothesis (>=3.56)", "mock", "pygments (>=2.7.2)", "requests", "setuptools", "xmlschema"] +testing = ["argcomplete", "attrs (>=19.2.0)", "hypothesis (>=3.56)", "mock", "nose", "pygments (>=2.7.2)", "requests", "setuptools", "xmlschema"] [[package]] name = "pytest-asyncio" diff --git a/pyproject.toml b/pyproject.toml index eedf35b..2a3a1b6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -70,11 +70,9 @@ ignore = [ select = ["ALL"] [tool.ruff.lint.per-file-ignores] # There are reverse-engineered classes -"roombapy/cloud/models/map/full.py" = [ - # Ruff is too dumb to realize that function calls return proper Field instance - "RUF009", - "D101" -] +"roombapy/cloud/models/map/full.py" = ["RUF009", "D101"] +"roombapy/cloud/aws.py" = ["PLR0913"] + [tool.ruff.format] docstring-code-format = true diff --git a/roombapy/cloud/aws.py b/roombapy/cloud/aws.py new file mode 100644 index 0000000..c9f85d6 --- /dev/null +++ b/roombapy/cloud/aws.py @@ -0,0 +1,273 @@ +"""Generate AWSv4 signature headers. + +Code is partially borrowed from tedder/requests-aws4auth (MIT licensed) +""" + +from __future__ import annotations + +import datetime +import hashlib +import hmac +import posixpath +import re +import shlex +from dataclasses import dataclass +from typing import Iterable +from urllib.parse import parse_qs, quote, unquote, urlparse + +DEFAULT_HEADERS = frozenset({"host", "content-type", "date", "x-amz-*"}) +DEFAULT_IROBOT_HEADERS = frozenset({"host", "date", "x-amz-*"}) + +Headers = dict[str, str] + + +def normalize_whitespace(text: str) -> str: + """Replace runs of whitespace with a single space. + + Ignore text enclosed in quotes. + """ + if re.search(r"\s", text): + return " ".join(shlex.split(text, posix=False)) + return text + + +def _canonicalize_query_string(query_string: str) -> str: + """Parse and format querystring as per AWS4 auth requirements. + + Perform percent quoting as needed. + """ + safe_qs_unresvd = "-_.~" + space = " " + query_string = query_string.split(space)[0] + # prevent parse_qs from interpreting semicolon as an alternative + # delimiter to ampersand + query_string = query_string.replace(";", "%3B") + qs_items = {} + for name, vals in parse_qs(query_string, keep_blank_values=True).items(): + key = quote(name, safe=safe_qs_unresvd) + values = [quote(val, safe=safe_qs_unresvd) for val in vals] + qs_items[key] = values + + return "&".join( + [ + f"{name}={value}" + for name, values in sorted(qs_items.items()) + for value in sorted(values) + ] + ) + + +def _canonicalize_path(request_path: str, service: str) -> str: + """Generate the canonical path as per AWS4 auth requirements. + + Not documented anywhere, determined from aws4_testsuite examples, + problem reports and testing against the live services. + """ + safe_chars = "/~" + qs = "" + fixed_path = request_path + if "?" in fixed_path: + fixed_path, qs = fixed_path.split("?", 1) + fixed_path = posixpath.normpath(fixed_path) + fixed_path = re.sub("/+", "/", fixed_path) + if request_path.endswith("/") and not fixed_path.endswith("/"): + fixed_path += "/" + full_path = fixed_path + # S3 seems to require unquoting first. 'host' service is used in + # amz_testsuite tests + if service in ["s3", "host"]: + full_path = unquote(full_path) + full_path = quote(full_path, safe=safe_chars) + if qs: + qm = "?" + full_path = qm.join((full_path, qs)) + return full_path + + +def _get_canonical_headers( + url: str, headers: Headers, include_header_names: Iterable[str] +) -> tuple[str, str]: + """Generate the Canonical Headers section of the Canonical Request. + + :param url: URL to get Host header + :param headers: Existing request headers + :param include_header_names: List of headers to include in the canonical + and signed headers. It's primarily included to allow testing against + specific examples from Amazon. If omitted or None it includes host, + content-type and any header starting 'x-amz-' except for + x-amz-client context, which appears to break mobile analytics auth + if included. Except for the x-amz-client-context exclusion these + defaults are per the AWS documentation. + + :returns: Canonical Headers and the Signed Headers strs as a tuple + (canonical_headers, signed_headers). + """ + include = [x.lower() for x in include_header_names] + headers = headers.copy() + # Temporarily include the host header - AWS requires it to be included + # in the signed headers, but Requests doesn't include it in a + # PreparedRequest + if "host" not in headers: + headers["host"] = urlparse(str(url)).netloc.split(":")[0] + # Aggregate for upper/lowercase header name collisions in header names, + # AMZ requires values of colliding headers be concatenated into a + # single header with lowercase name. Although this is not possible with + # Requests, since it uses a case-insensitive dict to hold headers, this + # is here just in case you duck type with a regular dict + canonical_headers: dict[str, list[str]] = {} + for header, value in headers.items(): + hdr = header.strip().lower() + val = normalize_whitespace(value).strip() + if ( + hdr in include + or "*" in include + or ( + "x-amz-*" in include + and hdr.startswith("x-amz-") + and hdr != "x-amz-client-context" + ) + ): + vals = canonical_headers.setdefault(hdr, []) + vals.append(val) + # Flatten cano_headers dict to string and generate signed_headers + cano_headers = "" + signed_headers_list = [] + for hdr in sorted(canonical_headers): + vals = canonical_headers[hdr] + val = ",".join(sorted(vals)) + cano_headers += f"{hdr}:{val}\n" + signed_headers_list.append(hdr) + signed_headers = ";".join(signed_headers_list) + return cano_headers, signed_headers + + +def _get_signature(amz_date: str, canonical_request: str, scope: str) -> bytes: + """Generate the AWS4 auth signature to sign for the request. + + :param amz_date: Date this request is valid for + :param canonical_request: The Canonical Request + :param scope: Request scope: + :returns: Signature + """ + hsh = hashlib.sha256(canonical_request.encode()) + sig_items = ["AWS4-HMAC-SHA256", amz_date, scope, hsh.hexdigest()] + return "\n".join(sig_items).encode("utf-8") + + +def _get_canonical_request( + canonical_headers: str, + signed_headers: str, + service: str, + raw_url: str, + method: str, + payload_hash: str, +) -> str: + """Create the AWS authentication Canonical Request string.""" + url = urlparse(raw_url) + path = _canonicalize_path(url.path, service) + # AWS handles "extreme" query strings differently to urlparse + # (see post-vanilla-query-nonunreserved test in aws_testsuite) + split = raw_url.split("?", 1) + query_string = split[1] if len(split) == 2 else "" + query_string = _canonicalize_query_string(query_string) + request_parts = [ + method.upper(), + path, + query_string, + canonical_headers, + signed_headers, + payload_hash, + ] + return "\n".join(request_parts) + + +@dataclass +class SigningKey: + """AWS signing key. Used to sign AWS authentication strings.""" + + scope: str + key: bytes + + @classmethod + def from_credentials( + cls, + *, + secret_key: str, + region: str, + service: str, + ) -> SigningKey: + """Construct signing key from credentials.""" + + def _sign(k: bytes, msg: str) -> bytes: + """Generate an SHA256 HMAC, encoding msg to UTF-8.""" + return hmac.new(k, msg.encode("utf-8"), hashlib.sha256).digest() + + aws_dt = datetime.datetime.now(tz=datetime.UTC).strftime("%Y%m%d") + + init_key = ("AWS4" + secret_key).encode("utf-8") + date_key = _sign(init_key, aws_dt) + region_key = _sign(date_key, region) + service_key = _sign(region_key, service) + key = _sign(service_key, "aws4_request") + + scope = f"{aws_dt}/{region}/{service}/aws4_request" + + return SigningKey(scope=scope, key=key) + + +def generate_aws_headers( + *, + url: str, + method: str, + request_headers: Headers, + service: str, + access_id: str, + signing_key: SigningKey, + session_token: str | None, + payload: str | None = None, + include_headers: Iterable[str] = DEFAULT_IROBOT_HEADERS, +) -> Headers: + """Client-agnostic helper to generate AWSv4 signature. + + :param url: Full request URL + :param method: Request method + :param service: AWS service the key is scoped for + :param access_id: AWS access ID + :param session_token: STS temporary credentials + :param signing_key: An SigningKey instance. + :param request_headers: Headers for this request + :param payload: Payload to be signed + :param include_headers: Headers to be signed + :return: Headers for HTTP client + """ + aws_headers = {} + + now = datetime.datetime.now(tz=datetime.UTC) + amz_date = now.strftime("%Y%m%dT%H%M%SZ") + aws_headers["x-amz-date"] = amz_date + + if payload is not None: + content_hash = hashlib.sha256(payload.encode()) + else: + content_hash = hashlib.sha256(b"") + payload_hash = content_hash.hexdigest() + + aws_headers["x-amz-content-sha256"] = payload_hash + if session_token: + aws_headers["x-amz-security-token"] = session_token + + # generate signature + result = _get_canonical_headers(url, request_headers, include_headers) + canonical_headers, signed_headers = result + cano_req = _get_canonical_request( + canonical_headers, signed_headers, service, url, method, payload_hash + ) + signature = _get_signature(amz_date, cano_req, signing_key.scope) + hsh = hmac.new(signing_key.key, signature, hashlib.sha256) + sig = hsh.hexdigest() + auth_str = "AWS4-HMAC-SHA256 " + auth_str += f"Credential={access_id}/{signing_key.scope}, " + auth_str += f"SignedHeaders={signed_headers}, " + auth_str += f"Signature={sig}" + aws_headers["Authorization"] = auth_str + return {**request_headers, **aws_headers}