Skip to content

Commit

Permalink
Add toggle for automatic creation of remote users
Browse files Browse the repository at this point in the history
Add a new flag to the remote user config that by default is off. If
remote users is enabled and the new flag is toggled on, the username in
REMOTE_USER will be used to automatically create a new user with that
username. Automatic creation regardless used to be the default behavior.
  • Loading branch information
hmpf committed Nov 6, 2023
1 parent c6ed8be commit a2dcea3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
8 changes: 6 additions & 2 deletions python/nav/etc/webfront/webfront.conf
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ server = ldap.example.com
#
basedn = ou=people,dc=example,dc=com

# How to lookup a user object from LDAP.
# How to lookup a user object from LDAP.
# 'direct' binds to <uid_attr>=<login name>,<user_basedn>
# 'search' searches for <uid_attr>=<login name> using basedn as searchbase.
#lookupmethod=direct

# Choose to bind to LDAP as the user with 'suffix' for Active Directory support.
# lookupmethod should be set to search for this option to function.
# lookupmethod should be set to search for this option to function.
#suffix = @ad.example.com

# If the LDAP directory requires an authenticated user to search for a user
Expand Down Expand Up @@ -110,6 +110,10 @@ enabled = no
# authenticated user?
#varname = REMOTE_USER

# Whether usernames set in REMOTE_USER should automatically be created in the
# database if they do not already exist.
# autocreate = off

# If the supplied remote username value needs modification to become more
# "username-like", specify which workaround to use here. Only `feide-oidc` is
# supported, at the moment.
Expand Down
29 changes: 19 additions & 10 deletions python/nav/web/auth/remote_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class RemoteUserConfigParser(NAVConfigParser):
logout-url=
varname=REMOTE_USER
workaround=none
autocreate=off
"""


Expand All @@ -78,16 +79,11 @@ def authenticate(request):
try:
account = Account.objects.get(login=username)
except Account.DoesNotExist:
# Store the remote user in the database and return the new account
account = Account(login=username, name=username, ext_sync='REMOTE_USER')
account.set_password(fake_password(32))
account.save()
_logger.info("Created user %s from header REMOTE_USER", account.login)
template = 'Account "{actor}" created due to REMOTE_USER HTTP header'
LogEntry.add_log_entry(
account, 'create-account', template=template, subsystem='auth'
)
return account
if _config.getboolean('remote-user', 'autocreate', fallback=False):
return autocreate_remote_user(username)

Check warning on line 83 in python/nav/web/auth/remote_user.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/auth/remote_user.py#L81-L83

Added lines #L81 - L83 were not covered by tests
# Bail out!
_logger.info('User creation turned off, did not create "%s"', username)
return False

Check warning on line 86 in python/nav/web/auth/remote_user.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/auth/remote_user.py#L85-L86

Added lines #L85 - L86 were not covered by tests

# Bail out! Potentially evil user
if account.locked:
Expand All @@ -101,6 +97,19 @@ def authenticate(request):
return account


def autocreate_remote_user(username):
# Store the remote user in the database and return the new account
account = Account(login=username, name=username, ext_sync='REMOTE_USER')
account.set_password(fake_password(32))
account.save()
_logger.info("Created user %s from header REMOTE_USER", account.login)
template = 'Account "{actor}" created due to REMOTE_USER HTTP header'
LogEntry.add_log_entry(

Check warning on line 107 in python/nav/web/auth/remote_user.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/auth/remote_user.py#L102-L107

Added lines #L102 - L107 were not covered by tests
account, 'create-account', template=template, subsystem='auth'
)
return account

Check warning on line 110 in python/nav/web/auth/remote_user.py

View check run for this annotation

Codecov / codecov/patch

python/nav/web/auth/remote_user.py#L110

Added line #L110 was not covered by tests


def login(request):
"""Log in the user in REMOTE_USER, if any and enabled
Expand Down

0 comments on commit a2dcea3

Please sign in to comment.