Skip to content

Commit

Permalink
PB-1009: trust API Gateway to authenticate users.
Browse files Browse the repository at this point in the history
We are delegating the authentication to API Gateway which sets the
`Geoadmin-Username` header. However due to how API Gateway and JWT-based
authentication work, the header is only set at login time. It is on the service
to keep track of the user afterwards.

This change updates service-stac in Dev to trust the `Geoadmin-Username` header
if it is present. Then service-stac persists the user across their whole session.

Relevant documentation is at https://docs.djangoproject.com/en/5.1/howto/auth-remote-user/
  • Loading branch information
adk-swisstopo committed Nov 20, 2024
1 parent 5c2bfd8 commit e695ac8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
20 changes: 20 additions & 0 deletions app/config/settings_dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,23 @@
AWS_SETTINGS['managed']['access_type'] = "key"
AWS_SETTINGS['managed']['ACCESS_KEY_ID'] = env("LEGACY_AWS_ACCESS_KEY_ID")
AWS_SETTINGS['managed']['SECRET_ACCESS_KEY'] = env("LEGACY_AWS_SECRET_ACCESS_KEY")

# API Gateway integration PB-1009
AUTHENTICATION_BACKENDS = [
"django.contrib.auth.backends.RemoteUserBackend",
# We keep ModelBackend as fallback until we have moved all users to Cognito.
"django.contrib.auth.backends.ModelBackend",
]
MIDDLEWARE += [
"django.contrib.auth.middleware.AuthenticationMiddleware",
"middleware.apigw.ApiGatewayMiddleware",
]
# By default sessions expire after two weeks.
# Sessions are only useful for user tracking in the admin UI. For security
# reason we should expire these sessions as soon as possible. Given the use
# case, it seems reasonable to log out users after 8h of inactivity or whenever
# they restart their browser.
SESSION_COOKIE_AGE = 60 * 60 * 8
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
SESSION_COOKIE_SAMESITE = "Strict"
SESSION_COOKIE_SECURE = True
4 changes: 4 additions & 0 deletions app/middleware/apigw.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from django.contrib.auth.middleware import PersistentRemoteUserMiddleware

class ApiGatewayMiddleware(PersistentRemoteUserMiddleware):
header = "HTTP_GEOADMIN_USERNAME"

0 comments on commit e695ac8

Please sign in to comment.