Jake Howard пре 2 година
родитељ
комит
fb0c1bfad2
2 измењених фајлова са 51 додато и 0 уклоњено
  1. 24 0
      bakerydemo/settings/production.py
  2. 27 0
      bakerydemo/templates/base/basic_auth.html

+ 24 - 0
bakerydemo/settings/production.py

@@ -224,3 +224,27 @@ if (
         WAGTAILFRONTENDCACHE["default"].update(
             {"BEARER_TOKEN": os.environ["FRONTEND_CACHE_CLOUDFLARE_BEARER_TOKEN"]}
         )
+
+# Basic authentication settings
+# These are settings to configure the third-party library:
+# https://gitlab.com/tmkn/django-basic-auth-ip-whitelist
+if os.environ.get("BASIC_AUTH_ENABLED", "false").lower().strip() == "true":
+    # Insert basic auth as a first middleware to be checked first, before
+    # anything else.
+    MIDDLEWARE.insert(0, "baipw.middleware.BasicAuthIPWhitelistMiddleware")
+
+    # This is the credentials users will have to use to access the site.
+    BASIC_AUTH_LOGIN = os.environ.get("BASIC_AUTH_LOGIN", "wagtail")
+    BASIC_AUTH_PASSWORD = os.environ.get("BASIC_AUTH_PASSWORD", "wagtail")
+
+    # Wagtail requires Authorization header to be present for the previews
+    BASIC_AUTH_DISABLE_CONSUMING_AUTHORIZATION_HEADER = True
+
+    # This is the list of hosts that website can be accessed without basic auth
+    # check.
+    if "BASIC_AUTH_WHITELISTED_HTTP_HOSTS" in os.environ:
+        BASIC_AUTH_WHITELISTED_HTTP_HOSTS = os.environ[
+            "BASIC_AUTH_WHITELISTED_HTTP_HOSTS"
+        ].split(",")
+
+    BASIC_AUTH_RESPONSE_TEMPLATE = "base/basic_auth.html"

+ 27 - 0
bakerydemo/templates/base/basic_auth.html

@@ -0,0 +1,27 @@
+<!DOCTYPE html>
+<html lang="en">
+    <head>
+        <meta charset="utf-8">
+        <title>Authentication Required</title>
+        <meta name="viewport" content="width=device-width, initial-scale=1">
+
+        {# Prevent the demo site from being indexed #}
+        <meta name="robots" content="noindex" />
+
+        <style>
+            h1 { font-size: 3rem; }
+            body { font: 1rem sans-serif; color: #333; }
+            article { display: block; text-align: center; margin: 0 auto; }
+            a { color: #2e1f5e; }
+            a:hover { color: #333; }
+        </style>
+    </head>
+
+    <body>
+        <article>
+            <h1>Authentication Required</h1>
+            <p>You must login to view this site</p>
+            <h3>Looking for Wagtail? Visit <a href="https://wagtail.org">wagtail.org</a>.</h3>
+        </article>
+    </body>
+</html>