Add some load-time validation of PGAUTH_KEY in the django sample
authorMagnus Hagander <magnus@hagander.net>
Mon, 28 Aug 2023 18:46:32 +0000 (20:46 +0200)
committerMagnus Hagander <magnus@hagander.net>
Mon, 28 Aug 2023 18:49:33 +0000 (20:49 +0200)
Will result in a nicer error when the key is wrong, and in particular a
startup error instead of a runtime error.

Reviewed-by: Célestin Matte
tools/communityauth/sample/django/auth.py

index 0a075715838f347669fba0c74e3e73d043ac0ade..b6f6067473a8374829412b110c6744aa464045a3 100644 (file)
@@ -374,3 +374,13 @@ def user_import(uid):
     auth_user_created_from_upstream.send(sender=user_import, user=u)
 
     return u
+
+
+# Try to load the key (and throw it away) to verify that the format is
+# correct.
+try:
+    k = base64.b64decode(settings.PGAUTH_KEY)
+    if len(k) != 32:
+        raise Exception("PGAUTH_KEY must be 32 bytes long")
+except Exception as e:
+    raise Exception("Invalid PGAUTH_KEY: {}".format(e))