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

Make session tracking work with API Gateway. #481

Merged
merged 3 commits into from
Dec 5, 2024

Conversation

adk-swisstopo
Copy link
Member

As per commits.

The main change in this PR is to ApiGatewayMiddleware which is updated to correctly handle the headers that API Gateway sends. In particular in the case where the user is already authenticated via a Django session cookie but where the API Gateway Geoadmin-Username header is invalid.

This also refactors and adds tests as well as tweaking session cookie parameters.

This only applies to the debug build. I have verified this works in the dev environment.

API Gateway always sets the `Geoadmin-Authenticated` and `Geoadmin-Username`
headers. When it is not able to authenticate the user, the values are "false"
and a single whitespace respectively.

The `PersistentRemoteUserMiddleware` class is designed to keep track of users
that were authenticated through headers but if the header is set with an
invalid value it will log out the user.

This change updates our derivation of `PersistentRemoteUserMiddleware` such that
it ignores the `Geoadmin-Username` header if `Geoadmin-Authenticated` is not set
to "true".

A ticket was opened with upstream to make `PersistentRemoteUserMiddleware`
more flexible in that regard: https://code.djangoproject.com/ticket/35971

This change also add tests exercising this new behaviour.
This shuts up the linter complaining about the module having too many lines.
Brownser-close-based session expiration overrides age-based expiration. We care
more about the age than the browser restart cycle.
Copy link
Contributor

@boecklic boecklic left a comment

Choose a reason for hiding this comment

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

Very nice! And thanks for the proposal to enhance Django.

@adk-swisstopo adk-swisstopo merged commit 776dcc4 into develop Dec 5, 2024
3 checks passed
@adk-swisstopo adk-swisstopo deleted the fix-PB-1009-apigw-header branch December 5, 2024 08:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants