diff --git a/examples/xandikos.nginx.conf b/examples/xandikos.nginx.conf index 97acc423..c1fa50a8 100644 --- a/examples/xandikos.nginx.conf +++ b/examples/xandikos.nginx.conf @@ -18,6 +18,7 @@ server { location / { proxy_set_header Host $http_host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Remote-User $remote_user; proxy_redirect off; proxy_buffering off; proxy_pass http://xandikos; diff --git a/notes/api-stability.rst b/notes/api-stability.rst index 54b097b5..1167dfa2 100644 --- a/notes/api-stability.rst +++ b/notes/api-stability.rst @@ -4,7 +4,7 @@ API Stability There are currently no guarantees about Xandikos Python APIs staying the same across different versions, except the following APIs: -xandikos.web.XandikosBackend(path) +xandikos.web.XandikosBackend(path, autocreate) xandikos.web.XandikosBackend.create_principal(principal, create_defaults=False) xandikos.web.XandikosApp(backend, current_user_principal) xandikos.web.WellknownRedirector(app, path) diff --git a/xandikos/tests/test_api.py b/xandikos/tests/test_api.py index 6c515d80..b44d6999 100644 --- a/xandikos/tests/test_api.py +++ b/xandikos/tests/test_api.py @@ -35,7 +35,7 @@ class WebTests(unittest.TestCase): def test_backend(self): path = tempfile.mkdtemp() try: - backend = XandikosBackend(path) + backend = XandikosBackend(path=path, autocreate=True) backend.create_principal("foo", create_defaults=True) XandikosApp(backend, "foo") finally: diff --git a/xandikos/tests/test_web.py b/xandikos/tests/test_web.py index 6348bd26..eaec8e9d 100644 --- a/xandikos/tests/test_web.py +++ b/xandikos/tests/test_web.py @@ -57,7 +57,7 @@ def setUp(self): self.store = VdirStore.create(os.path.join(self.tempdir, "c")) self.store.load_extra_file_handler(ICalendarFile) - self.backend = XandikosBackend(self.tempdir) + self.backend = XandikosBackend(path=self.tempdir, autocreate=False) self.cal = CalendarCollection(self.backend, "c", self.store) diff --git a/xandikos/web.py b/xandikos/web.py index ed8700d0..f8269726 100644 --- a/xandikos/web.py +++ b/xandikos/web.py @@ -977,8 +977,9 @@ def open_store_from_path(path: str): class XandikosBackend(webdav.Backend): - def __init__(self, path): + def __init__(self, path, autocreate): self.path = path + self.autocreate = autocreate self._user_principals = set() def _map_to_file_path(self, relpath): @@ -1040,6 +1041,15 @@ def get_resource(self, relpath): except KeyError: return None + def set_principal(self, user): + principal = "/%s/" % user + if not self.get_resource(principal): + if self.autocreate: + self.create_principal( + principal, create_defaults=True + ) + self._mark_as_principal(principal) + class XandikosApp(webdav.WebDAVApp): """A wsgi App that provides a Xandikos web server.""" @@ -1252,7 +1262,7 @@ def run_simple_server( port: TCP Port to listen on (None to disable) socket_path: Unix domain socket path to listen on (None to disable) """ - backend = XandikosBackend(directory) + backend = XandikosBackend(directory, autocreate) backend._mark_as_principal(current_user_principal) if autocreate or defaults: @@ -1415,7 +1425,8 @@ def main(argv=None): # noqa: C901 logging.basicConfig(level=logging.INFO, format='%(message)s') - backend = XandikosBackend(os.path.abspath(options.directory)) + backend = XandikosBackend(os.path.abspath(options.directory), + options.autocreate) backend._mark_as_principal(options.current_user_principal) if options.autocreate or options.defaults: diff --git a/xandikos/webdav.py b/xandikos/webdav.py index 7e69c4a3..d3f504bd 100644 --- a/xandikos/webdav.py +++ b/xandikos/webdav.py @@ -2101,7 +2101,12 @@ def handle_wsgi_request(self, environ, start_response): logging.debug('SCRIPT_NAME not set; assuming "".') environ["SCRIPT_NAME"] = "" request = WSGIRequest(environ) + remote_user = environ.get("HTTP_X_REMOTE_USER") environ = {"SCRIPT_NAME": environ["SCRIPT_NAME"]} + if remote_user: + environ["REMOTE_USER"] = remote_user + self.backend.set_principal(remote_user) + try: loop = asyncio.get_event_loop() except RuntimeError: diff --git a/xandikos/wsgi.py b/xandikos/wsgi.py index 5b4b1d89..bcd5203b 100644 --- a/xandikos/wsgi.py +++ b/xandikos/wsgi.py @@ -29,7 +29,8 @@ ) -backend = XandikosBackend(path=os.environ["XANDIKOSPATH"]) +backend = XandikosBackend(path=os.environ["XANDIKOSPATH"], + autocreate=os.getenv("AUTOCREATE")) if not os.path.isdir(backend.path): if os.getenv("AUTOCREATE"): os.makedirs(os.environ["XANDIKOSPATH"])