summaryrefslogtreecommitdiff
path: root/backend/helpers.py
diff options
context:
space:
mode:
authorDerek J. Clark <derkejohn.clark@gmail.com>2022-08-08 11:32:14 -0700
committerGitHub <noreply@github.com>2022-08-08 11:32:14 -0700
commit20094c5f752046a33287ef372fa973e919e83226 (patch)
treeed298251be183fc5f3151b19a10f7499c7d66e0d /backend/helpers.py
parent198591dbd7b0b6dd311ab9265d4cda4a733ee280 (diff)
downloaddecky-loader-20094c5f752046a33287ef372fa973e919e83226.tar.gz
decky-loader-20094c5f752046a33287ef372fa973e919e83226.zip
Use Environment Variables (#123)
Uses environment variables instead of hard coding the "deck" user/group. This adds support for systems other than the steam deck that are using the DeckUI. * Use Environment Variables * Use method to get USER from a systemd root process * Fix imports. Add get_user and get_user_group methods in helpers.py. Removed duplicated code * Add separate setters/getters for user vars. Ensure sleep prevents race condition of user setter in while loop
Diffstat (limited to 'backend/helpers.py')
-rw-r--r--backend/helpers.py49
1 files changed, 45 insertions, 4 deletions
diff --git a/backend/helpers.py b/backend/helpers.py
index e8c2ce5b..68ac7cb3 100644
--- a/backend/helpers.py
+++ b/backend/helpers.py
@@ -1,11 +1,15 @@
-from aiohttp.web import middleware, Response
-import ssl
import certifi
+import ssl
import uuid
-ssl_ctx = ssl.create_default_context(cafile=certifi.where())
+from subprocess import check_output
+from time import sleep
+# global vars
csrf_token = str(uuid.uuid4())
+ssl_ctx = ssl.create_default_context(cafile=certifi.where())
+user = None
+group = None
def get_ssl_context():
return ssl_ctx
@@ -17,4 +21,41 @@ def get_csrf_token():
async def csrf_middleware(request, handler):
if str(request.method) == "OPTIONS" or request.headers.get('Authentication') == csrf_token or str(request.rel_url) == "/auth/token" or str(request.rel_url).startswith("/plugins/load_main/") or str(request.rel_url).startswith("/static/") or str(request.rel_url).startswith("/legacy/") or str(request.rel_url).startswith("/steam_resource/"):
return await handler(request)
- return Response(text='Forbidden', status='403') \ No newline at end of file
+ return Response(text='Forbidden', status='403')
+
+# Get the user by checking for the first logged in user. As this is run
+# by systemd at startup the process is likely to start before the user
+# logs in, so we will wait here until they are available. Note that
+# other methods such as getenv wont work as there was no $SUDO_USER to
+# start the systemd service.
+def set_user():
+ global user
+ cmd = "who | awk '{print $1}' | sort | head -1"
+ while user == None:
+ name = check_output(cmd, shell=True).decode().strip()
+ if name not in [None, '']:
+ user = name
+ sleep(0.1)
+
+# Get the global user. get_user must be called first.
+def get_user() -> str:
+ global user
+ if user == None:
+ raise ValueError("helpers.get_user method called before user variable was set. Run helpers.set_user first.")
+ return user
+
+# Set the global user group. get_user must be called first
+def set_user_group() -> str:
+ global group
+ global user
+ if user == None:
+ raise ValueError("helpers.set_user_dir method called before user variable was set. Run helpers.set_user first.")
+ if group == None:
+ group = check_output(["id", "-g", "-n", user]).decode().strip()
+
+# Get the group of the global user. set_user_group must be called first.
+def get_user_group() -> str:
+ global group
+ if group == None:
+ raise ValueError("helpers.get_user_group method called before group variable was set. Run helpers.set_user_group first.")
+ return group