Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create and select user_principal based on web authenticated user. #179

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/xandikos.nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion notes/api-stability.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion xandikos/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion xandikos/tests/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
17 changes: 14 additions & 3 deletions xandikos/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -1040,6 +1041,15 @@ def get_resource(self, relpath):
except KeyError:
return None

def set_principal(self, user):
principal = "/%s/" % user
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally this path would be configurable, but we could leave that for a follow-up PR.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean something like:

def set_principal(self, user, principal_path_prefix="/", principal_path_suffix="/"):
    principal = principal_path_prefix + user + principal_path_suffix

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, exactly. Though we'd probably want those extra paths to come from a command-line argument/configuration file.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain SCRIPT_NAME?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SCRIPT_NAME is a bit of a misnomer, but it's named after a similar variable in the CGI world - it's the bit of the path to Xandikos itself. If Xandikos is under https://foo.example.com/dav/, then SCRIPT_NAME would be /dav/.

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."""
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions xandikos/webdav.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion xandikos/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down