django - Allowing login depending on hostname (remote) -
i have django intranet reachable on http(s)://somename/ , http(s)://10.10.0.30/, using allowed_hosts
setting:
allowed_hosts = [u'10.10.0.30', u'somename',]
now i'd allow certain users login website remotely well. first step i'll have add external url (like somename.com
) allowed_hosts
; no problem there. moment on, everyone account able log in, not want.
i thinking in terms of having group called permitremotelogin
- when user part of group, logging in host somename.com
allowed. i'm unsure actual implementation and/or whether doable in first place (?).
when searching e.g. djangopackages, no results found. idea whether has been done before?
i've done similar things in past, it's quite easy actually. need replace normal authentication backend own: https://docs.djangoproject.com/en/1.8/topics/auth/customizing/#writing-an-authentication-backend
the default backend looks this: https://github.com/django/django/blob/master/django/contrib/auth/backends.py#l113-143
class modelbackend(object): ... def authenticate(self, remote_user): """ username passed ``remote_user`` considered trusted. method returns ``user`` object given username, creating new ``user`` object if ``create_unknown_user`` ``true``. returns none if ``create_unknown_user`` ``false`` , ``user`` object given username not found in database. """ if not remote_user: return user = none username = self.clean_username(remote_user) usermodel = get_user_model() # note accomplished in 1 try-except clause, # instead use get_or_create when creating unknown users since has # built-in safeguards multiple threads. if self.create_unknown_user: user, created = usermodel._default_manager.get_or_create(**{ usermodel.username_field: username }) if created: user = self.configure_user(user) else: try: user = usermodel._default_manager.get_by_natural_key(username) except usermodel.doesnotexist: pass return user
what need inherit class , add remote host check it.
something along lines of this:
class hostnameauthenticationbackend(backends.modelbackend): def authenticate(self, username=none, password=none, hostname=none, **kwargs): user = backends.modelbackend.authenticate( username=username, password=password, **kwargs) if user: # check hostname , groups here if hostname_correct: return user
the 1 tiny snag you'll hit default hostname won't available, you'll have pass along login view authentication backend.
Comments
Post a Comment