From f3108ac3dc4daad82356048fef9c618b68e6d6b5 Mon Sep 17 00:00:00 2001 From: Evilham Date: Tue, 6 Dec 2022 19:24:04 +0100 Subject: [PATCH] [api] Add type hints and cleanup This makes modifying the existing code easier --- dd-sso/admin/src/admin/lib/legal.py | 7 + dd-sso/admin/src/admin/views/WebViews.py | 6 + dd-sso/docker/api/Pipfile | 21 + dd-sso/docker/api/Pipfile.lock | 550 ++++++++++++++++++ dd-sso/docker/api/mypy.ini | 22 + dd-sso/docker/api/pyproject.toml | 2 + dd-sso/docker/api/src/api/__init__.py | 50 +- dd-sso/docker/api/src/api/flaskapp.py | 63 ++ dd-sso/docker/api/src/api/lib/avatars.py | 21 +- dd-sso/docker/api/src/api/lib/load_config.py | 37 -- dd-sso/docker/api/src/api/lib/menu.py | 22 +- .../docker/api/src/api/views/AvatarsViews.py | 21 +- .../docker/api/src/api/views/InternalViews.py | 17 +- dd-sso/docker/api/src/api/views/MenuViews.py | 52 +- dd-sso/docker/api/src/api/views/decorators.py | 7 +- dd-sso/docker/api/src/start.py | 12 +- 16 files changed, 773 insertions(+), 137 deletions(-) create mode 100644 dd-sso/docker/api/Pipfile create mode 100644 dd-sso/docker/api/Pipfile.lock create mode 100644 dd-sso/docker/api/mypy.ini create mode 100644 dd-sso/docker/api/pyproject.toml create mode 100644 dd-sso/docker/api/src/api/flaskapp.py delete mode 100644 dd-sso/docker/api/src/api/lib/load_config.py diff --git a/dd-sso/admin/src/admin/lib/legal.py b/dd-sso/admin/src/admin/lib/legal.py index 4e7ce80..1aab3d9 100644 --- a/dd-sso/admin/src/admin/lib/legal.py +++ b/dd-sso/admin/src/admin/lib/legal.py @@ -36,15 +36,22 @@ if TYPE_CHECKING: # TODO: Fix all this def get_legal(app : "AdminFlaskApp", lang : str) -> str: + # Used in: + # - views/AppViews with open(app.legal_path+lang, "r") as languagefile: return languagefile.read() def gen_legal_if_not_exists(app : "AdminFlaskApp", lang : str) -> None: + # Used in: + # - views/AppViews + # - views/WebViews if not os.path.isfile(app.legal_path+lang): log.debug("Creating new language file") with open(app.legal_path+lang, "w") as languagefile: languagefile.write("Legal
This is the default legal page for language " + lang) def new_legal(app : "AdminFlaskApp", lang : str, html : str) -> None: + # Used in: + # - views/AppViews with open(app.legal_path+lang, "w") as languagefile: languagefile.write(html) diff --git a/dd-sso/admin/src/admin/views/WebViews.py b/dd-sso/admin/src/admin/views/WebViews.py index 9cdde3c..9057be4 100644 --- a/dd-sso/admin/src/admin/views/WebViews.py +++ b/dd-sso/admin/src/admin/views/WebViews.py @@ -91,11 +91,17 @@ def setup_web_views(app : "AdminFlaskApp") -> None: @app.route("/legal") @login_required def legal() -> str: + """ + This is the admin-view to edit legal texts. + """ # data = json.loads(requests.get("http://dd-sso-api/json").text) return render_template("pages/legal.html", title="Legal", nav="Legal", data="") @app.route("/legal_text") def legal_text() -> str: + """ + This is what gets linked as *the* legal text. + """ lang = request.args.get("lang") if not lang or lang not in ["ca","es","en","fr"]: lang="ca" diff --git a/dd-sso/docker/api/Pipfile b/dd-sso/docker/api/Pipfile new file mode 100644 index 0000000..25c70d3 --- /dev/null +++ b/dd-sso/docker/api/Pipfile @@ -0,0 +1,21 @@ +[[source]] +url = "https://pypi.org/simple" +verify_ssl = true +name = "pypi" + +[packages] +flask = "*" +pyyaml = "*" +eventlet = "*" +flask-socketio = "*" +python-keycloak = "*" + +[dev-packages] +mypy = "*" +black = "*" +isort = "*" +types-flask = "*" +types-pyyaml = "*" + +[requires] +python_version = "3.9" diff --git a/dd-sso/docker/api/Pipfile.lock b/dd-sso/docker/api/Pipfile.lock new file mode 100644 index 0000000..a8936d6 --- /dev/null +++ b/dd-sso/docker/api/Pipfile.lock @@ -0,0 +1,550 @@ +{ + "_meta": { + "hash": { + "sha256": "3bf42d60b0fd94f021ff1b9302d949f2b81d3e13e5dc708b13a8c706a14312fc" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.9" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": { + "bidict": { + "hashes": [ + "sha256:415126d23a0c81e1a8c584a8fb1f6905ea090c772571803aeee0a2242e8e7ba0", + "sha256:5c826b3e15e97cc6e615de295756847c282a79b79c5430d3bfc909b1ac9f5bd8" + ], + "markers": "python_version >= '3.7'", + "version": "==0.22.0" + }, + "certifi": { + "hashes": [ + "sha256:0d9c601124e5a6ba9712dbc60d9c53c21e34f5f641fe83002317394311bdce14", + "sha256:90c1a32f1d68f940488354e36370f6cca89f0f106db09518524c88d6ed83f382" + ], + "markers": "python_version >= '3.6'", + "version": "==2022.9.24" + }, + "charset-normalizer": { + "hashes": [ + "sha256:5a3d016c7c547f69d6f81fb0db9449ce888b418b5b9952cc5e6e66843e9dd845", + "sha256:83e9a75d1911279afd89352c68b45348559d1fc0506b054b346651b5e7fee29f" + ], + "markers": "python_version >= '3.6'", + "version": "==2.1.1" + }, + "click": { + "hashes": [ + "sha256:7682dc8afb30297001674575ea00d1814d808d6a36af415a82bd481d37ba7b8e", + "sha256:bb4d8133cb15a609f44e8213d9b391b0809795062913b383c62be0ee95b1db48" + ], + "markers": "python_version >= '3.7'", + "version": "==8.1.3" + }, + "dnspython": { + "hashes": [ + "sha256:0f7569a4a6ff151958b64304071d370daa3243d15941a7beedf0c9fe5105603e", + "sha256:a851e51367fb93e9e1361732c1d60dab63eff98712e503ea7d92e6eccb109b4f" + ], + "markers": "python_version >= '3.6' and python_version < '4.0'", + "version": "==2.2.1" + }, + "ecdsa": { + "hashes": [ + "sha256:190348041559e21b22a1d65cee485282ca11a6f81d503fddb84d5017e9ed1e49", + "sha256:80600258e7ed2f16b9aa1d7c295bd70194109ad5a30fdee0eaeefef1d4c559dd" + ], + "markers": "python_version >= '2.6' and python_version not in '3.0, 3.1, 3.2, 3.3'", + "version": "==0.18.0" + }, + "eventlet": { + "hashes": [ + "sha256:82c382c2a2c712f1a8320378a9120ac9589d9f1131c36a63780f0b8504afa5bc", + "sha256:96039b9389dbb4431b1c0a6e42ea1326628cc7ad63a6280b02947f111d3d8e04" + ], + "index": "pypi", + "version": "==0.33.2" + }, + "flask": { + "hashes": [ + "sha256:642c450d19c4ad482f96729bd2a8f6d32554aa1e231f4f6b4e7e5264b16cca2b", + "sha256:b9c46cc36662a7949f34b52d8ec7bb59c0d74ba08ba6cb9ce9adc1d8676d9526" + ], + "index": "pypi", + "version": "==2.2.2" + }, + "flask-socketio": { + "hashes": [ + "sha256:04093e3ca144467f9731c376796aff105d182ac4cccfcb056d05d567a675f306", + "sha256:11d1d78b8805cda351b27828a110b88c74a573be62b07f7f5a519fb67fae0a58" + ], + "index": "pypi", + "version": "==5.3.2" + }, + "greenlet": { + "hashes": [ + "sha256:0109af1138afbfb8ae647e31a2b1ab030f58b21dd8528c27beaeb0093b7938a9", + "sha256:0459d94f73265744fee4c2d5ec44c6f34aa8a31017e6e9de770f7bcf29710be9", + "sha256:04957dc96669be041e0c260964cfef4c77287f07c40452e61abe19d647505581", + "sha256:0722c9be0797f544a3ed212569ca3fe3d9d1a1b13942d10dd6f0e8601e484d26", + "sha256:097e3dae69321e9100202fc62977f687454cd0ea147d0fd5a766e57450c569fd", + "sha256:0b493db84d124805865adc587532ebad30efa68f79ad68f11b336e0a51ec86c2", + "sha256:13ba6e8e326e2116c954074c994da14954982ba2795aebb881c07ac5d093a58a", + "sha256:13ebf93c343dd8bd010cd98e617cb4c1c1f352a0cf2524c82d3814154116aa82", + "sha256:1407fe45246632d0ffb7a3f4a520ba4e6051fc2cbd61ba1f806900c27f47706a", + "sha256:1bf633a50cc93ed17e494015897361010fc08700d92676c87931d3ea464123ce", + "sha256:2d0bac0385d2b43a7bd1d651621a4e0f1380abc63d6fb1012213a401cbd5bf8f", + "sha256:3001d00eba6bbf084ae60ec7f4bb8ed375748f53aeaefaf2a37d9f0370558524", + "sha256:356e4519d4dfa766d50ecc498544b44c0249b6de66426041d7f8b751de4d6b48", + "sha256:38255a3f1e8942573b067510f9611fc9e38196077b0c8eb7a8c795e105f9ce77", + "sha256:3d75b8d013086b08e801fbbb896f7d5c9e6ccd44f13a9241d2bf7c0df9eda928", + "sha256:41b825d65f31e394b523c84db84f9383a2f7eefc13d987f308f4663794d2687e", + "sha256:42e602564460da0e8ee67cb6d7236363ee5e131aa15943b6670e44e5c2ed0f67", + "sha256:4aeaebcd91d9fee9aa768c1b39cb12214b30bf36d2b7370505a9f2165fedd8d9", + "sha256:4c8b1c43e75c42a6cafcc71defa9e01ead39ae80bd733a2608b297412beede68", + "sha256:4d37990425b4687ade27810e3b1a1c37825d242ebc275066cfee8cb6b8829ccd", + "sha256:4f09b0010e55bec3239278f642a8a506b91034f03a4fb28289a7d448a67f1515", + "sha256:505138d4fa69462447a562a7c2ef723c6025ba12ac04478bc1ce2fcc279a2db5", + "sha256:5067920de254f1a2dee8d3d9d7e4e03718e8fd2d2d9db962c8c9fa781ae82a39", + "sha256:56961cfca7da2fdd178f95ca407fa330c64f33289e1804b592a77d5593d9bd94", + "sha256:5a8e05057fab2a365c81abc696cb753da7549d20266e8511eb6c9d9f72fe3e92", + "sha256:659f167f419a4609bc0516fb18ea69ed39dbb25594934bd2dd4d0401660e8a1e", + "sha256:662e8f7cad915ba75d8017b3e601afc01ef20deeeabf281bd00369de196d7726", + "sha256:6f61d71bbc9b4a3de768371b210d906726535d6ca43506737682caa754b956cd", + "sha256:72b00a8e7c25dcea5946692a2485b1a0c0661ed93ecfedfa9b6687bd89a24ef5", + "sha256:811e1d37d60b47cb8126e0a929b58c046251f28117cb16fcd371eed61f66b764", + "sha256:81b0ea3715bf6a848d6f7149d25bf018fd24554a4be01fcbbe3fdc78e890b955", + "sha256:88c8d517e78acdf7df8a2134a3c4b964415b575d2840a2746ddb1cc6175f8608", + "sha256:8dca09dedf1bd8684767bc736cc20c97c29bc0c04c413e3276e0962cd7aeb148", + "sha256:974a39bdb8c90a85982cdb78a103a32e0b1be986d411303064b28a80611f6e51", + "sha256:9e112e03d37987d7b90c1e98ba5e1b59e1645226d78d73282f45b326f7bddcb9", + "sha256:9e9744c657d896c7b580455e739899e492a4a452e2dd4d2b3e459f6b244a638d", + "sha256:9ed358312e63bf683b9ef22c8e442ef6c5c02973f0c2a939ec1d7b50c974015c", + "sha256:9f2c221eecb7ead00b8e3ddb913c67f75cba078fd1d326053225a3f59d850d72", + "sha256:a20d33124935d27b80e6fdacbd34205732660e0a1d35d8b10b3328179a2b51a1", + "sha256:a4c0757db9bd08470ff8277791795e70d0bf035a011a528ee9a5ce9454b6cba2", + "sha256:afe07421c969e259e9403c3bb658968702bc3b78ec0b6fde3ae1e73440529c23", + "sha256:b1992ba9d4780d9af9726bbcef6a1db12d9ab1ccc35e5773685a24b7fb2758eb", + "sha256:b23d2a46d53210b498e5b701a1913697671988f4bf8e10f935433f6e7c332fb6", + "sha256:b5e83e4de81dcc9425598d9469a624826a0b1211380ac444c7c791d4a2137c19", + "sha256:be35822f35f99dcc48152c9839d0171a06186f2d71ef76dc57fa556cc9bf6b45", + "sha256:be9e0fb2ada7e5124f5282d6381903183ecc73ea019568d6d63d33f25b2a9000", + "sha256:c140e7eb5ce47249668056edf3b7e9900c6a2e22fb0eaf0513f18a1b2c14e1da", + "sha256:c6a08799e9e88052221adca55741bf106ec7ea0710bca635c208b751f0d5b617", + "sha256:cb242fc2cda5a307a7698c93173d3627a2a90d00507bccf5bc228851e8304963", + "sha256:cce1e90dd302f45716a7715517c6aa0468af0bf38e814ad4eab58e88fc09f7f7", + "sha256:cd4ccc364cf75d1422e66e247e52a93da6a9b73cefa8cad696f3cbbb75af179d", + "sha256:d21681f09e297a5adaa73060737e3aa1279a13ecdcfcc6ef66c292cb25125b2d", + "sha256:d38ffd0e81ba8ef347d2be0772e899c289b59ff150ebbbbe05dc61b1246eb4e0", + "sha256:d566b82e92ff2e09dd6342df7e0eb4ff6275a3f08db284888dcd98134dbd4243", + "sha256:d5b0ff9878333823226d270417f24f4d06f235cb3e54d1103b71ea537a6a86ce", + "sha256:d6ee1aa7ab36475035eb48c01efae87d37936a8173fc4d7b10bb02c2d75dd8f6", + "sha256:db38f80540083ea33bdab614a9d28bcec4b54daa5aff1668d7827a9fc769ae0a", + "sha256:ea688d11707d30e212e0110a1aac7f7f3f542a259235d396f88be68b649e47d1", + "sha256:f6327b6907b4cb72f650a5b7b1be23a2aab395017aa6f1adb13069d66360eb3f", + "sha256:fb412b7db83fe56847df9c47b6fe3f13911b06339c2aa02dcc09dce8bbf582cd" + ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'", + "version": "==2.0.1" + }, + "idna": { + "hashes": [ + "sha256:814f528e8dead7d329833b91c5faa87d60bf71824cd12a7530b5526063d02cb4", + "sha256:90b77e79eaa3eba6de819a0c442c0b4ceefc341a7a2ab77d7562bf49f425c5c2" + ], + "markers": "python_version >= '3.5'", + "version": "==3.4" + }, + "importlib-metadata": { + "hashes": [ + "sha256:d5059f9f1e8e41f80e9c56c2ee58811450c31984dfa625329ffd7c0dad88a73b", + "sha256:d84d17e21670ec07990e1044a99efe8d615d860fd176fc29ef5c306068fda313" + ], + "markers": "python_version < '3.10'", + "version": "==5.1.0" + }, + "itsdangerous": { + "hashes": [ + "sha256:2c2349112351b88699d8d4b6b075022c0808887cb7ad10069318a8b0bc88db44", + "sha256:5dbbc68b317e5e42f327f9021763545dc3fc3bfe22e6deb96aaf1fc38874156a" + ], + "markers": "python_version >= '3.7'", + "version": "==2.1.2" + }, + "jinja2": { + "hashes": [ + "sha256:31351a702a408a9e7595a8fc6150fc3f43bb6bf7e319770cbc0db9df9437e852", + "sha256:6088930bfe239f0e6710546ab9c19c9ef35e29792895fed6e6e31a023a182a61" + ], + "markers": "python_version >= '3.7'", + "version": "==3.1.2" + }, + "markupsafe": { + "hashes": [ + "sha256:0212a68688482dc52b2d45013df70d169f542b7394fc744c02a57374a4207003", + "sha256:089cf3dbf0cd6c100f02945abeb18484bd1ee57a079aefd52cffd17fba910b88", + "sha256:10c1bfff05d95783da83491be968e8fe789263689c02724e0c691933c52994f5", + "sha256:33b74d289bd2f5e527beadcaa3f401e0df0a89927c1559c8566c066fa4248ab7", + "sha256:3799351e2336dc91ea70b034983ee71cf2f9533cdff7c14c90ea126bfd95d65a", + "sha256:3ce11ee3f23f79dbd06fb3d63e2f6af7b12db1d46932fe7bd8afa259a5996603", + "sha256:421be9fbf0ffe9ffd7a378aafebbf6f4602d564d34be190fc19a193232fd12b1", + "sha256:43093fb83d8343aac0b1baa75516da6092f58f41200907ef92448ecab8825135", + "sha256:46d00d6cfecdde84d40e572d63735ef81423ad31184100411e6e3388d405e247", + "sha256:4a33dea2b688b3190ee12bd7cfa29d39c9ed176bda40bfa11099a3ce5d3a7ac6", + "sha256:4b9fe39a2ccc108a4accc2676e77da025ce383c108593d65cc909add5c3bd601", + "sha256:56442863ed2b06d19c37f94d999035e15ee982988920e12a5b4ba29b62ad1f77", + "sha256:671cd1187ed5e62818414afe79ed29da836dde67166a9fac6d435873c44fdd02", + "sha256:694deca8d702d5db21ec83983ce0bb4b26a578e71fbdbd4fdcd387daa90e4d5e", + "sha256:6a074d34ee7a5ce3effbc526b7083ec9731bb3cbf921bbe1d3005d4d2bdb3a63", + "sha256:6d0072fea50feec76a4c418096652f2c3238eaa014b2f94aeb1d56a66b41403f", + "sha256:6fbf47b5d3728c6aea2abb0589b5d30459e369baa772e0f37a0320185e87c980", + "sha256:7f91197cc9e48f989d12e4e6fbc46495c446636dfc81b9ccf50bb0ec74b91d4b", + "sha256:86b1f75c4e7c2ac2ccdaec2b9022845dbb81880ca318bb7a0a01fbf7813e3812", + "sha256:8dc1c72a69aa7e082593c4a203dcf94ddb74bb5c8a731e4e1eb68d031e8498ff", + "sha256:8e3dcf21f367459434c18e71b2a9532d96547aef8a871872a5bd69a715c15f96", + "sha256:8e576a51ad59e4bfaac456023a78f6b5e6e7651dcd383bcc3e18d06f9b55d6d1", + "sha256:96e37a3dc86e80bf81758c152fe66dbf60ed5eca3d26305edf01892257049925", + "sha256:97a68e6ada378df82bc9f16b800ab77cbf4b2fada0081794318520138c088e4a", + "sha256:99a2a507ed3ac881b975a2976d59f38c19386d128e7a9a18b7df6fff1fd4c1d6", + "sha256:a49907dd8420c5685cfa064a1335b6754b74541bbb3706c259c02ed65b644b3e", + "sha256:b09bf97215625a311f669476f44b8b318b075847b49316d3e28c08e41a7a573f", + "sha256:b7bd98b796e2b6553da7225aeb61f447f80a1ca64f41d83612e6139ca5213aa4", + "sha256:b87db4360013327109564f0e591bd2a3b318547bcef31b468a92ee504d07ae4f", + "sha256:bcb3ed405ed3222f9904899563d6fc492ff75cce56cba05e32eff40e6acbeaa3", + "sha256:d4306c36ca495956b6d568d276ac11fdd9c30a36f1b6eb928070dc5360b22e1c", + "sha256:d5ee4f386140395a2c818d149221149c54849dfcfcb9f1debfe07a8b8bd63f9a", + "sha256:dda30ba7e87fbbb7eab1ec9f58678558fd9a6b8b853530e176eabd064da81417", + "sha256:e04e26803c9c3851c931eac40c695602c6295b8d432cbe78609649ad9bd2da8a", + "sha256:e1c0b87e09fa55a220f058d1d49d3fb8df88fbfab58558f1198e08c1e1de842a", + "sha256:e72591e9ecd94d7feb70c1cbd7be7b3ebea3f548870aa91e2732960fa4d57a37", + "sha256:e8c843bbcda3a2f1e3c2ab25913c80a3c5376cd00c6e8c4a86a89a28c8dc5452", + "sha256:efc1913fd2ca4f334418481c7e595c00aad186563bbc1ec76067848c7ca0a933", + "sha256:f121a1420d4e173a5d96e47e9a0c0dcff965afdf1626d28de1460815f7c4ee7a", + "sha256:fc7b548b17d238737688817ab67deebb30e8073c95749d55538ed473130ec0c7" + ], + "markers": "python_version >= '3.7'", + "version": "==2.1.1" + }, + "pyasn1": { + "hashes": [ + "sha256:014c0e9976956a08139dc0712ae195324a75e142284d5f87f1a87ee1b068a359", + "sha256:03840c999ba71680a131cfaee6fab142e1ed9bbd9c693e285cc6aca0d555e576", + "sha256:0458773cfe65b153891ac249bcf1b5f8f320b7c2ce462151f8fa74de8934becf", + "sha256:08c3c53b75eaa48d71cf8c710312316392ed40899cb34710d092e96745a358b7", + "sha256:39c7e2ec30515947ff4e87fb6f456dfc6e84857d34be479c9d4a4ba4bf46aa5d", + "sha256:5c9414dcfede6e441f7e8f81b43b34e834731003427e5b09e4e00e3172a10f00", + "sha256:6e7545f1a61025a4e58bb336952c5061697da694db1cae97b116e9c46abcf7c8", + "sha256:78fa6da68ed2727915c4767bb386ab32cdba863caa7dbe473eaae45f9959da86", + "sha256:7ab8a544af125fb704feadb008c99a88805126fb525280b2270bb25cc1d78a12", + "sha256:99fcc3c8d804d1bc6d9a099921e39d827026409a58f2a720dcdb89374ea0c776", + "sha256:aef77c9fb94a3ac588e87841208bdec464471d9871bd5050a287cc9a475cd0ba", + "sha256:e89bf84b5437b532b0803ba5c9a5e054d21fec423a89952a74f87fa2c9b7bce2", + "sha256:fec3e9d8e36808a28efb59b489e4528c10ad0f480e57dcc32b4de5c9d8c9fdf3" + ], + "version": "==0.4.8" + }, + "python-engineio": { + "hashes": [ + "sha256:7454314a529bba20e745928601ffeaf101c1b5aca9a6c4e48ad397803d10ea0c", + "sha256:d8d8b072799c36cadcdcc2b40d2a560ce09797ab3d2d596b2ad519a5e4df19ae" + ], + "markers": "python_version >= '3.6'", + "version": "==4.3.4" + }, + "python-jose": { + "hashes": [ + "sha256:55779b5e6ad599c6336191246e95eb2293a9ddebd555f796a65f838f07e5d78a", + "sha256:9b1376b023f8b298536eedd47ae1089bcdb848f1535ab30555cd92002d78923a" + ], + "version": "==3.3.0" + }, + "python-keycloak": { + "hashes": [ + "sha256:08c530ff86f631faccb8033d9d9345cc3148cb2cf132ff7564f025292e4dbd96", + "sha256:a1ce102b978beb56d385319b3ca20992b915c2c12d15a2d0c23f1104882f3fb6" + ], + "index": "pypi", + "version": "==2.6.0" + }, + "python-socketio": { + "hashes": [ + "sha256:92395062d9db3c13d30e7cdedaa0e1330bba78505645db695415f9a3c628d097", + "sha256:d9a9f047e6fdd306c852fbac36516f4b495c2096f8ad9ceb8803b8e5ff5622e3" + ], + "markers": "python_version >= '3.6'", + "version": "==5.7.2" + }, + "pyyaml": { + "hashes": [ + "sha256:01b45c0191e6d66c470b6cf1b9531a771a83c1c4208272ead47a3ae4f2f603bf", + "sha256:0283c35a6a9fbf047493e3a0ce8d79ef5030852c51e9d911a27badfde0605293", + "sha256:055d937d65826939cb044fc8c9b08889e8c743fdc6a32b33e2390f66013e449b", + "sha256:07751360502caac1c067a8132d150cf3d61339af5691fe9e87803040dbc5db57", + "sha256:0b4624f379dab24d3725ffde76559cff63d9ec94e1736b556dacdfebe5ab6d4b", + "sha256:0ce82d761c532fe4ec3f87fc45688bdd3a4c1dc5e0b4a19814b9009a29baefd4", + "sha256:1e4747bc279b4f613a09eb64bba2ba602d8a6664c6ce6396a4d0cd413a50ce07", + "sha256:213c60cd50106436cc818accf5baa1aba61c0189ff610f64f4a3e8c6726218ba", + "sha256:231710d57adfd809ef5d34183b8ed1eeae3f76459c18fb4a0b373ad56bedcdd9", + "sha256:277a0ef2981ca40581a47093e9e2d13b3f1fbbeffae064c1d21bfceba2030287", + "sha256:2cd5df3de48857ed0544b34e2d40e9fac445930039f3cfe4bcc592a1f836d513", + "sha256:40527857252b61eacd1d9af500c3337ba8deb8fc298940291486c465c8b46ec0", + "sha256:432557aa2c09802be39460360ddffd48156e30721f5e8d917f01d31694216782", + "sha256:473f9edb243cb1935ab5a084eb238d842fb8f404ed2193a915d1784b5a6b5fc0", + "sha256:48c346915c114f5fdb3ead70312bd042a953a8ce5c7106d5bfb1a5254e47da92", + "sha256:50602afada6d6cbfad699b0c7bb50d5ccffa7e46a3d738092afddc1f9758427f", + "sha256:68fb519c14306fec9720a2a5b45bc9f0c8d1b9c72adf45c37baedfcd949c35a2", + "sha256:77f396e6ef4c73fdc33a9157446466f1cff553d979bd00ecb64385760c6babdc", + "sha256:81957921f441d50af23654aa6c5e5eaf9b06aba7f0a19c18a538dc7ef291c5a1", + "sha256:819b3830a1543db06c4d4b865e70ded25be52a2e0631ccd2f6a47a2822f2fd7c", + "sha256:897b80890765f037df3403d22bab41627ca8811ae55e9a722fd0392850ec4d86", + "sha256:98c4d36e99714e55cfbaaee6dd5badbc9a1ec339ebfc3b1f52e293aee6bb71a4", + "sha256:9df7ed3b3d2e0ecfe09e14741b857df43adb5a3ddadc919a2d94fbdf78fea53c", + "sha256:9fa600030013c4de8165339db93d182b9431076eb98eb40ee068700c9c813e34", + "sha256:a80a78046a72361de73f8f395f1f1e49f956c6be882eed58505a15f3e430962b", + "sha256:afa17f5bc4d1b10afd4466fd3a44dc0e245382deca5b3c353d8b757f9e3ecb8d", + "sha256:b3d267842bf12586ba6c734f89d1f5b871df0273157918b0ccefa29deb05c21c", + "sha256:b5b9eccad747aabaaffbc6064800670f0c297e52c12754eb1d976c57e4f74dcb", + "sha256:bfaef573a63ba8923503d27530362590ff4f576c626d86a9fed95822a8255fd7", + "sha256:c5687b8d43cf58545ade1fe3e055f70eac7a5a1a0bf42824308d868289a95737", + "sha256:cba8c411ef271aa037d7357a2bc8f9ee8b58b9965831d9e51baf703280dc73d3", + "sha256:d15a181d1ecd0d4270dc32edb46f7cb7733c7c508857278d3d378d14d606db2d", + "sha256:d4b0ba9512519522b118090257be113b9468d804b19d63c71dbcf4a48fa32358", + "sha256:d4db7c7aef085872ef65a8fd7d6d09a14ae91f691dec3e87ee5ee0539d516f53", + "sha256:d4eccecf9adf6fbcc6861a38015c2a64f38b9d94838ac1810a9023a0609e1b78", + "sha256:d67d839ede4ed1b28a4e8909735fc992a923cdb84e618544973d7dfc71540803", + "sha256:daf496c58a8c52083df09b80c860005194014c3698698d1a57cbcfa182142a3a", + "sha256:dbad0e9d368bb989f4515da330b88a057617d16b6a8245084f1b05400f24609f", + "sha256:e61ceaab6f49fb8bdfaa0f92c4b57bcfbea54c09277b1b4f7ac376bfb7a7c174", + "sha256:f84fbc98b019fef2ee9a1cb3ce93e3187a6df0b2538a651bfb890254ba9f90b5" + ], + "index": "pypi", + "version": "==6.0" + }, + "requests": { + "hashes": [ + "sha256:7c5599b102feddaa661c826c56ab4fee28bfd17f5abca1ebbe3e7f19d7c97983", + "sha256:8fefa2a1a1365bf5520aac41836fbee479da67864514bdb821f31ce07ce65349" + ], + "markers": "python_version >= '3.7' and python_version < '4.0'", + "version": "==2.28.1" + }, + "requests-toolbelt": { + "hashes": [ + "sha256:380606e1d10dc85c3bd47bf5a6095f815ec007be7a8b69c878507068df059e6f", + "sha256:968089d4584ad4ad7c171454f0a5c6dac23971e9472521ea3b6d49d610aa6fc0" + ], + "version": "==0.9.1" + }, + "rsa": { + "hashes": [ + "sha256:90260d9058e514786967344d0ef75fa8727eed8a7d2e43ce9f4bcf1b536174f7", + "sha256:e38464a49c6c85d7f1351b0126661487a7e0a14a50f1675ec50eb34d4f20ef21" + ], + "markers": "python_version >= '3.6' and python_version < '4.0'", + "version": "==4.9" + }, + "six": { + "hashes": [ + "sha256:1e61c37477a1626458e36f7b1d82aa5c9b094fa4802892072e49de9c60c4c926", + "sha256:8abb2f1d86890a2dfb989f9a77cfcfd3e47c2a354b01111771326f8aa26e0254" + ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", + "version": "==1.16.0" + }, + "urllib3": { + "hashes": [ + "sha256:47cc05d99aaa09c9e72ed5809b60e7ba354e64b59c9c173ac3018642d8bb41fc", + "sha256:c083dd0dce68dbfbe1129d5271cb90f9447dea7d52097c6e0126120c521ddea8" + ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4, 3.5'", + "version": "==1.26.13" + }, + "werkzeug": { + "hashes": [ + "sha256:7ea2d48322cc7c0f8b3a215ed73eabd7b5d75d0b50e31ab006286ccff9e00b8f", + "sha256:f979ab81f58d7318e064e99c4506445d60135ac5cd2e177a2de0089bfd4c9bd5" + ], + "markers": "python_version >= '3.7'", + "version": "==2.2.2" + }, + "zipp": { + "hashes": [ + "sha256:83a28fcb75844b5c0cdaf5aa4003c2d728c77e05f5aeabe8e95e56727005fbaa", + "sha256:a7a22e05929290a67401440b39690ae6563279bced5f314609d9d03798f56766" + ], + "markers": "python_version >= '3.7'", + "version": "==3.11.0" + } + }, + "develop": { + "black": { + "hashes": [ + "sha256:14ff67aec0a47c424bc99b71005202045dc09270da44a27848d534600ac64fc7", + "sha256:197df8509263b0b8614e1df1756b1dd41be6738eed2ba9e9769f3880c2b9d7b6", + "sha256:1e464456d24e23d11fced2bc8c47ef66d471f845c7b7a42f3bd77bf3d1789650", + "sha256:2039230db3c6c639bd84efe3292ec7b06e9214a2992cd9beb293d639c6402edb", + "sha256:21199526696b8f09c3997e2b4db8d0b108d801a348414264d2eb8eb2532e540d", + "sha256:2644b5d63633702bc2c5f3754b1b475378fbbfb481f62319388235d0cd104c2d", + "sha256:432247333090c8c5366e69627ccb363bc58514ae3e63f7fc75c54b1ea80fa7de", + "sha256:444ebfb4e441254e87bad00c661fe32df9969b2bf224373a448d8aca2132b395", + "sha256:5b9b29da4f564ba8787c119f37d174f2b69cdfdf9015b7d8c5c16121ddc054ae", + "sha256:5cc42ca67989e9c3cf859e84c2bf014f6633db63d1cbdf8fdb666dcd9e77e3fa", + "sha256:5d8f74030e67087b219b032aa33a919fae8806d49c867846bfacde57f43972ef", + "sha256:72ef3925f30e12a184889aac03d77d031056860ccae8a1e519f6cbb742736383", + "sha256:819dc789f4498ecc91438a7de64427c73b45035e2e3680c92e18795a839ebb66", + "sha256:915ace4ff03fdfff953962fa672d44be269deb2eaf88499a0f8805221bc68c87", + "sha256:9311e99228ae10023300ecac05be5a296f60d2fd10fff31cf5c1fa4ca4b1988d", + "sha256:974308c58d057a651d182208a484ce80a26dac0caef2895836a92dd6ebd725e0", + "sha256:b8b49776299fece66bffaafe357d929ca9451450f5466e997a7285ab0fe28e3b", + "sha256:c957b2b4ea88587b46cf49d1dc17681c1e672864fd7af32fc1e9664d572b3458", + "sha256:e41a86c6c650bcecc6633ee3180d80a025db041a8e2398dcc059b3afa8382cd4", + "sha256:f513588da599943e0cde4e32cc9879e825d58720d6557062d1098c5ad80080e1", + "sha256:fba8a281e570adafb79f7755ac8721b6cf1bbf691186a287e990c7929c7692ff" + ], + "index": "pypi", + "version": "==22.10.0" + }, + "click": { + "hashes": [ + "sha256:7682dc8afb30297001674575ea00d1814d808d6a36af415a82bd481d37ba7b8e", + "sha256:bb4d8133cb15a609f44e8213d9b391b0809795062913b383c62be0ee95b1db48" + ], + "markers": "python_version >= '3.7'", + "version": "==8.1.3" + }, + "isort": { + "hashes": [ + "sha256:6f62d78e2f89b4500b080fe3a81690850cd254227f27f75c3a0c491a1f351ba7", + "sha256:e8443a5e7a020e9d7f97f1d7d9cd17c88bcb3bc7e218bf9cf5095fe550be2951" + ], + "index": "pypi", + "version": "==5.10.1" + }, + "mypy": { + "hashes": [ + "sha256:0714258640194d75677e86c786e80ccf294972cc76885d3ebbb560f11db0003d", + "sha256:0c8f3be99e8a8bd403caa8c03be619544bc2c77a7093685dcf308c6b109426c6", + "sha256:0cca5adf694af539aeaa6ac633a7afe9bbd760df9d31be55ab780b77ab5ae8bf", + "sha256:1c8cd4fb70e8584ca1ed5805cbc7c017a3d1a29fb450621089ffed3e99d1857f", + "sha256:1f7d1a520373e2272b10796c3ff721ea1a0712288cafaa95931e66aa15798813", + "sha256:209ee89fbb0deed518605edddd234af80506aec932ad28d73c08f1400ef80a33", + "sha256:26efb2fcc6b67e4d5a55561f39176821d2adf88f2745ddc72751b7890f3194ad", + "sha256:37bd02ebf9d10e05b00d71302d2c2e6ca333e6c2a8584a98c00e038db8121f05", + "sha256:3a700330b567114b673cf8ee7388e949f843b356a73b5ab22dd7cff4742a5297", + "sha256:3c0165ba8f354a6d9881809ef29f1a9318a236a6d81c690094c5df32107bde06", + "sha256:3d80e36b7d7a9259b740be6d8d906221789b0d836201af4234093cae89ced0cd", + "sha256:4175593dc25d9da12f7de8de873a33f9b2b8bdb4e827a7cae952e5b1a342e243", + "sha256:4307270436fd7694b41f913eb09210faff27ea4979ecbcd849e57d2da2f65305", + "sha256:5e80e758243b97b618cdf22004beb09e8a2de1af481382e4d84bc52152d1c476", + "sha256:641411733b127c3e0dab94c45af15fea99e4468f99ac88b39efb1ad677da5711", + "sha256:652b651d42f155033a1967739788c436491b577b6a44e4c39fb340d0ee7f0d70", + "sha256:6d7464bac72a85cb3491c7e92b5b62f3dcccb8af26826257760a552a5e244aa5", + "sha256:74e259b5c19f70d35fcc1ad3d56499065c601dfe94ff67ae48b85596b9ec1461", + "sha256:7d17e0a9707d0772f4a7b878f04b4fd11f6f5bcb9b3813975a9b13c9332153ab", + "sha256:901c2c269c616e6cb0998b33d4adbb4a6af0ac4ce5cd078afd7bc95830e62c1c", + "sha256:98e781cd35c0acf33eb0295e8b9c55cdbef64fcb35f6d3aa2186f289bed6e80d", + "sha256:a12c56bf73cdab116df96e4ff39610b92a348cc99a1307e1da3c3768bbb5b135", + "sha256:ac6e503823143464538efda0e8e356d871557ef60ccd38f8824a4257acc18d93", + "sha256:b8472f736a5bfb159a5e36740847808f6f5b659960115ff29c7cecec1741c648", + "sha256:b86ce2c1866a748c0f6faca5232059f881cda6dda2a893b9a8373353cfe3715a", + "sha256:bc9ec663ed6c8f15f4ae9d3c04c989b744436c16d26580eaa760ae9dd5d662eb", + "sha256:c9166b3f81a10cdf9b49f2d594b21b31adadb3d5e9db9b834866c3258b695be3", + "sha256:d13674f3fb73805ba0c45eb6c0c3053d218aa1f7abead6e446d474529aafc372", + "sha256:de32edc9b0a7e67c2775e574cb061a537660e51210fbf6006b0b36ea695ae9bb", + "sha256:e62ebaad93be3ad1a828a11e90f0e76f15449371ffeecca4a0a0b9adc99abcef" + ], + "index": "pypi", + "version": "==0.991" + }, + "mypy-extensions": { + "hashes": [ + "sha256:090fedd75945a69ae91ce1303b5824f428daf5a028d2f6ab8a299250a846f15d", + "sha256:2d82818f5bb3e369420cb3c4060a7970edba416647068eb4c5343488a6c604a8" + ], + "version": "==0.4.3" + }, + "pathspec": { + "hashes": [ + "sha256:88c2606f2c1e818b978540f73ecc908e13999c6c3a383daf3705652ae79807a5", + "sha256:8f6bf73e5758fd365ef5d58ce09ac7c27d2833a8d7da51712eac6e27e35141b0" + ], + "markers": "python_version >= '3.7'", + "version": "==0.10.2" + }, + "platformdirs": { + "hashes": [ + "sha256:1006647646d80f16130f052404c6b901e80ee4ed6bef6792e1f238a8969106f7", + "sha256:af0276409f9a02373d540bf8480021a048711d572745aef4b7842dad245eba10" + ], + "markers": "python_version >= '3.7'", + "version": "==2.5.4" + }, + "tomli": { + "hashes": [ + "sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc", + "sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f" + ], + "markers": "python_version < '3.11'", + "version": "==2.0.1" + }, + "types-click": { + "hashes": [ + "sha256:8cb030a669e2e927461be9827375f83c16b8178c365852c060a34e24871e7e81", + "sha256:b6604968be6401dc516311ca50708a0a28baa7a0cb840efd7412f0dbbff4e092" + ], + "version": "==7.1.8" + }, + "types-flask": { + "hashes": [ + "sha256:6ab8a9a5e258b76539d652f6341408867298550b19b81f0e41e916825fc39087", + "sha256:aac777b3abfff9436e6b01f6d08171cf23ea6e5be71cbf773aaabb1c5763e9cf" + ], + "index": "pypi", + "version": "==1.1.6" + }, + "types-jinja2": { + "hashes": [ + "sha256:60a1e21e8296979db32f9374d8a239af4cb541ff66447bb915d8ad398f9c63b2", + "sha256:dbdc74a40aba7aed520b7e4d89e8f0fe4286518494208b35123bcf084d4b8c81" + ], + "version": "==2.11.9" + }, + "types-markupsafe": { + "hashes": [ + "sha256:85b3a872683d02aea3a5ac2a8ef590193c344092032f58457287fbf8e06711b1", + "sha256:ca2bee0f4faafc45250602567ef38d533e877d2ddca13003b319c551ff5b3cc5" + ], + "version": "==1.1.10" + }, + "types-pyyaml": { + "hashes": [ + "sha256:1e94e80aafee07a7e798addb2a320e32956a373f376655128ae20637adb2655b", + "sha256:6840819871c92deebe6a2067fb800c11b8a063632eb4e3e755914e7ab3604e83" + ], + "index": "pypi", + "version": "==6.0.12.2" + }, + "types-werkzeug": { + "hashes": [ + "sha256:194bd5715a13c598f05c63e8a739328657590943bce941e8a3619a6b5d4a54ec", + "sha256:5cc269604c400133d452a40cee6397655f878fc460e03fde291b9e3a5eaa518c" + ], + "version": "==1.0.9" + }, + "typing-extensions": { + "hashes": [ + "sha256:1511434bb92bf8dd198c12b1cc812e800d4181cfcb867674e0f8279cc93087aa", + "sha256:16fa4864408f655d35ec496218b85f79b3437c829e93320c7c9215ccfd92489e" + ], + "markers": "python_version >= '3.7'", + "version": "==4.4.0" + } + } +} diff --git a/dd-sso/docker/api/mypy.ini b/dd-sso/docker/api/mypy.ini new file mode 100644 index 0000000..8d9c987 --- /dev/null +++ b/dd-sso/docker/api/mypy.ini @@ -0,0 +1,22 @@ +[mypy] +namespace_packages=True +#explicit_package_bases=True +#TODO: progress so we can enable this +#strict=True +# +check_untyped_defs=True +disallow_untyped_defs=True +warn_redundant_casts=True +warn_unused_configs= True +warn_unused_ignores = True +warn_no_return=True +warn_return_any=True +warn_unreachable=True +enable_error_code=unused-awaitable + +[mypy-eventlet.*] +ignore_missing_imports=True +[mypy-flask_socketio.*] +ignore_missing_imports=True +[mypy-keycloak.*] +ignore_missing_imports=True diff --git a/dd-sso/docker/api/pyproject.toml b/dd-sso/docker/api/pyproject.toml new file mode 100644 index 0000000..5d7bf33 --- /dev/null +++ b/dd-sso/docker/api/pyproject.toml @@ -0,0 +1,2 @@ +[tool.isort] +profile = "black" diff --git a/dd-sso/docker/api/src/api/__init__.py b/dd-sso/docker/api/src/api/__init__.py index 83a6d92..b3808fa 100644 --- a/dd-sso/docker/api/src/api/__init__.py +++ b/dd-sso/docker/api/src/api/__init__.py @@ -19,49 +19,21 @@ # SPDX-License-Identifier: AGPL-3.0-or-later import logging as log -import os -from flask import Flask, render_template, send_from_directory - -app = Flask(__name__, static_url_path="") -app = Flask(__name__, template_folder="static/templates") -app.url_map.strict_slashes = False - -""" -App secret key for encrypting cookies -You can generate one with: - import os - os.urandom(24) -And paste it here. -""" -app.secret_key = "Change this key!//\xf7\x83\xbe\x17\xfa\xa3zT\n\\]m\xa6\x8bF\xdd\r\xf7\x9e\x1d\x1f\x14'" - -print("Starting dd-sso api...") - -from api.lib.load_config import loadConfig - -try: - loadConfig(app) -except: - print("Could not get environment variables...") +from api.flaskapp import ApiFlaskApp -""" -Debug should be removed on production! -""" -if app.debug: - log.warning("Debug mode: {}".format(app.debug)) -else: - log.info("Debug mode: {}".format(app.debug)) +def get_app() -> ApiFlaskApp: + app = ApiFlaskApp(__name__, template_folder="static/templates") + """ + Debug should be removed on production! + """ + if app.debug: + log.warning("Debug mode: {}".format(app.debug)) + else: + log.info("Debug mode: {}".format(app.debug)) -""" -Serve static files -""" - - -@app.route("/templates/") -def send_templates(path): - return send_from_directory(os.path.join(app.root_path, "static/templates"), path) + return app """ diff --git a/dd-sso/docker/api/src/api/flaskapp.py b/dd-sso/docker/api/src/api/flaskapp.py new file mode 100644 index 0000000..e671629 --- /dev/null +++ b/dd-sso/docker/api/src/api/flaskapp.py @@ -0,0 +1,63 @@ +import logging as log +import os +import secrets +import traceback +from typing import Any + +from flask import Flask, Response, render_template, send_from_directory + +from api.views.AvatarsViews import setup_avatar_views +from api.views.InternalViews import setup_internal_views +from api.views.MenuViews import setup_menu_views + + +class ApiFlaskApp(Flask): + """ + Subclass Flask app to ease customisation and type checking. + + In order for an instance of this class to be useful, + the setup method should be called after instantiating. + """ + + def __init__(self, *args: Any, **kwargs: Any): + super().__init__(*args, **kwargs) + self.domain = os.environ["DOMAIN"] + self.url_map.strict_slashes = False + self._load_config() + # Minor setup tasks + self._setup_routes() + setup_internal_views(self) + setup_avatar_views(self) + setup_menu_views(self) + + @property + def secrets_dir(self) -> str: + return os.path.join("/tmp", "secrets") + + def setup(self) -> None: + pass + + def _load_config(self) -> None: + try: + # This is not being used anywhere + self.secret_key = secrets.token_hex() + # Settings from the environment + self.config.update( + { + "DOMAIN": self.domain, + } + ) + except Exception as e: + log.error(traceback.format_exc()) + raise + + def _setup_routes(self) -> None: + """ + Setup routes to Serve static files + """ + + @self.route("/templates/") + def send_templates(path: str) -> Response: + return send_from_directory( + os.path.join(self.root_path, "static/templates"), path + ) diff --git a/dd-sso/docker/api/src/api/lib/avatars.py b/dd-sso/docker/api/src/api/lib/avatars.py index 40f3583..40705bb 100644 --- a/dd-sso/docker/api/src/api/lib/avatars.py +++ b/dd-sso/docker/api/src/api/lib/avatars.py @@ -27,26 +27,27 @@ import traceback from datetime import datetime, timedelta import yaml -from api import app as application from keycloak import KeycloakAdmin +from typing import cast + class Avatars: def __init__( self, - url="http://dd-sso-keycloak:8080/auth/", - username=os.environ["KEYCLOAK_USER"], - password=os.environ["KEYCLOAK_PASSWORD"], - realm="master", - verify=True, - ): + url: str="http://dd-sso-keycloak:8080/auth/", + username: str=os.environ["KEYCLOAK_USER"], + password: str=os.environ["KEYCLOAK_PASSWORD"], + realm: str="master", + verify: bool=True, + ) -> None: self.url = url self.username = username self.password = password self.realm = realm self.verify = verify - def connect(self): + def connect(self) -> None: self.keycloak_admin = KeycloakAdmin( server_url=self.url, username=self.username, @@ -55,9 +56,9 @@ class Avatars: verify=self.verify, ) - def get_user_avatar(self, username): + def get_user_avatar(self, username: str) -> str: self.connect() - return self.keycloak_admin.get_user_id(username) + return cast(str, self.keycloak_admin.get_user_id(username)) # # Add user diff --git a/dd-sso/docker/api/src/api/lib/load_config.py b/dd-sso/docker/api/src/api/lib/load_config.py deleted file mode 100644 index f2aea9a..0000000 --- a/dd-sso/docker/api/src/api/lib/load_config.py +++ /dev/null @@ -1,37 +0,0 @@ -# -# Copyright © 2021,2022 IsardVDI S.L. -# -# This file is part of DD -# -# DD is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as published by -# the Free Software Foundation, either version 3 of the License, or (at your -# option) any later version. -# -# DD is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more -# details. -# -# You should have received a copy of the GNU Affero General Public License -# along with DD. If not, see . -# -# SPDX-License-Identifier: AGPL-3.0-or-later - - -import logging as log -import os -import sys -import traceback - -from api import app - - -class loadConfig: - def __init__(self, app=None): - try: - app.config.setdefault("DOMAIN", os.environ["DOMAIN"]) - - except Exception as e: - log.error(traceback.format_exc()) - raise diff --git a/dd-sso/docker/api/src/api/lib/menu.py b/dd-sso/docker/api/src/api/lib/menu.py index 3135b18..2144bcb 100644 --- a/dd-sso/docker/api/src/api/lib/menu.py +++ b/dd-sso/docker/api/src/api/lib/menu.py @@ -24,13 +24,16 @@ import pprint import time import traceback from datetime import datetime, timedelta +from typing import TYPE_CHECKING, Dict import yaml -from api import app as application from jinja2 import Environment, FileSystemLoader +if TYPE_CHECKING: + from api.flaskapp import ApiFlaskApp -def write_css(): + +def write_css() -> None: env = Environment(loader=FileSystemLoader("api/static/_templates")) css_template = env.get_template("dd.css") with open("menu/custom.yaml", "r") as menu_custom_file: @@ -42,7 +45,10 @@ def write_css(): class Menu: - def __init__(self): + app: "ApiFlaskApp" + + def __init__(self, app: "ApiFlaskApp") -> None: + self.app = app # self.user_menudict=self.gen_user_menu() # pprint.pprint(self.user_menudict) # self.write_user_menu() @@ -77,8 +83,8 @@ class Menu: """ HEADER & APP MENU """ - def gen_header(self): - domain = application.config["DOMAIN"] + def gen_header(self) -> Dict: + domain = self.app.config["DOMAIN"] with open(r"menu/system.yaml") as yml: system = yaml.load(yml, Loader=yaml.FullLoader) @@ -109,7 +115,7 @@ class Menu: "api", domain, custom.get("product_logo", "/img/product-logo.svg") ) - menudict = custom + menudict: Dict = custom menudict["user"] = { "account": self.patch_url( "sso", domain, system.get("user", {}).get("account", "") @@ -127,7 +133,7 @@ class Menu: ) return menudict - def write_headers(self): + def write_headers(self) -> None: env = Environment(loader=FileSystemLoader("api/static/_templates")) template = env.get_template("user_menu.html") @@ -168,7 +174,7 @@ class Menu: with open("api/static/templates/header_sso.html", "w") as fh: fh.write(output_from_parsed_template) - def get_header(self): + def get_header(self) -> Dict: return self.menudict # with open('menu.yaml', 'w') as yml: # print(yaml.dump(header, yml, allow_unicode=True)) diff --git a/dd-sso/docker/api/src/api/views/AvatarsViews.py b/dd-sso/docker/api/src/api/views/AvatarsViews.py index d894997..9c79106 100644 --- a/dd-sso/docker/api/src/api/views/AvatarsViews.py +++ b/dd-sso/docker/api/src/api/views/AvatarsViews.py @@ -24,9 +24,9 @@ import os import sys import time import traceback +from typing import TYPE_CHECKING from uuid import uuid4 -from api import app from flask import ( Response, jsonify, @@ -37,15 +37,20 @@ from flask import ( url_for, ) +if TYPE_CHECKING: + from api.flaskapp import ApiFlaskApp + from ..lib.avatars import Avatars avatars = Avatars() -@app.route("/avatar/", methods=["GET"]) -def avatar(username): - return send_from_directory( - os.path.join(app.root_path, "../avatars/master-avatars/"), - avatars.get_user_avatar(username), - mimetype="image/jpg", - ) +def setup_avatar_views(app: "ApiFlaskApp") -> None: + # TODO: check if this is redundant and remove it + @app.route("/avatar/", methods=["GET"]) + def avatar(username: str) -> Response: + return send_from_directory( + os.path.join(app.root_path, "../avatars/master-avatars/"), + avatars.get_user_avatar(username), + mimetype="image/jpg", + ) diff --git a/dd-sso/docker/api/src/api/views/InternalViews.py b/dd-sso/docker/api/src/api/views/InternalViews.py index 088fba7..2989bde 100644 --- a/dd-sso/docker/api/src/api/views/InternalViews.py +++ b/dd-sso/docker/api/src/api/views/InternalViews.py @@ -19,17 +19,22 @@ # SPDX-License-Identifier: AGPL-3.0-or-later import os +from typing import TYPE_CHECKING -from api import app from flask import Response, jsonify, redirect, render_template, request, url_for -from .decorators import is_internal +from api.views.decorators import is_internal + +if TYPE_CHECKING: + from api.flaskapp import ApiFlaskApp -@app.route("/restart", methods=["GET"]) -@is_internal -def api_restart(): - os.system("kill 1") +def setup_internal_views(app: "ApiFlaskApp") -> None: + # TODO: This should be removed + @app.route("/restart", methods=["GET"]) + @is_internal + def api_restart() -> None: + os.system("kill 1") # @app.route('/user_menu/', methods=['GET']) diff --git a/dd-sso/docker/api/src/api/views/MenuViews.py b/dd-sso/docker/api/src/api/views/MenuViews.py index 4c2a2c6..dde8fa6 100644 --- a/dd-sso/docker/api/src/api/views/MenuViews.py +++ b/dd-sso/docker/api/src/api/views/MenuViews.py @@ -24,40 +24,44 @@ import os import sys import time import traceback +from typing import TYPE_CHECKING, Union from uuid import uuid4 -from api import app from flask import Response, jsonify, redirect, render_template, request, url_for +if TYPE_CHECKING: + from api.flaskapp import ApiFlaskApp + from ..lib.menu import Menu - -menu = Menu() +from .decorators import JsonResponse -@app.route("/header/", methods=["GET"]) -@app.route("/header//", methods=["GET"]) -def api_v2_header(format, application=False): - if application == False: +def setup_menu_views(app: "ApiFlaskApp") -> None: + menu = Menu(app) + + @app.route("/header/", methods=["GET"]) + @app.route("/header//", methods=["GET"]) + def api_v2_header( + format: str, application: Union[str, bool] = False + ) -> Union[str, JsonResponse]: if format == "json": - if application == False: - return ( - json.dumps(menu.get_header()), - 200, - {"Content-Type": "application/json"}, - ) - if format == "html": - if application == False: - return render_template("header.html") + return ( + json.dumps(menu.get_header()), + 200, + {"Content-Type": "application/json"}, + ) + else: # format == "html": if application == "nextcloud": return render_template("header_nextcloud.html") if application == "wordpress": return render_template("header_wordpress.html") + else: # if application == False: + return render_template("header.html") - -# @app.route('/user_menu/', methods=['GET']) -# @app.route('/user_menu//', methods=['GET']) -# def api_v2_user_menu(format,application=False): -# if application == False: -# if format == 'json': -# if application == False: -# return json.dumps(menu.get_user_nenu()), 200, {'Content-Type': 'application/json'} + # @app.route('/user_menu/', methods=['GET']) + # @app.route('/user_menu//', methods=['GET']) + # def api_v2_user_menu(format,application=False): + # if application == False: + # if format == 'json': + # if application == False: + # return json.dumps(menu.get_user_nenu()), 200, {'Content-Type': 'application/json'} diff --git a/dd-sso/docker/api/src/api/views/decorators.py b/dd-sso/docker/api/src/api/views/decorators.py index 2935bfe..fcd7dbe 100644 --- a/dd-sso/docker/api/src/api/views/decorators.py +++ b/dd-sso/docker/api/src/api/views/decorators.py @@ -20,13 +20,16 @@ import socket from functools import wraps +from typing import Dict, Tuple from flask import redirect, request, url_for +JsonResponse = Tuple[str, int, Dict[str, str]] -def is_internal(fn): + +def is_internal(fn): # type: ignore @wraps(fn) - def decorated_view(*args, **kwargs): + def decorated_view(*args, **kwargs): # type: ignore remote_addr = ( request.headers["X-Forwarded-For"].split(",")[0] if "X-Forwarded-For" in request.headers diff --git a/dd-sso/docker/api/src/start.py b/dd-sso/docker/api/src/start.py index 8846fec..eaa1946 100644 --- a/dd-sso/docker/api/src/start.py +++ b/dd-sso/docker/api/src/start.py @@ -22,10 +22,15 @@ from eventlet import monkey_patch monkey_patch() -from api import app from flask_socketio import SocketIO -socketio = SocketIO(app) +from api import get_app + +# Setup the app +app = get_app() +app.setup() + +app.socketio = SocketIO(app) if __name__ == "__main__": import logging @@ -35,4 +40,5 @@ if __name__ == "__main__": engineio_logger = logging.getLogger("engineio") engineio_logger.setLevel("ERROR") - socketio.run(app, host="0.0.0.0", port=7039, debug=False) + print("Starting dd-sso api...") + app.socketio.run(app, host="0.0.0.0", port=7039, debug=False)