Django & custom auth backend (web service) + no database. How to save stuff in session?
- by Infinity
I've been searching here and there, and based on this answer I've put together what you see below.
It works, but I need to put some stuff in the user's session, right there inside authenticate.
How would I store acme_token in the user's session, so that it will get cleared if they logged out?
class AcmeUserBackend(object):
# Create a User object if not already in the database?
create_unknown_user = False
def get_user(self, username):
return AcmeUser(id=username)
def authenticate(self, username=None, password=None):
""" Check the username/password and return an AcmeUser. """
acme_token = ask_another_site_about_creds(username, password)
if acme_token:
return AcmeUser(id=username)
return None
##################
from django.contrib.auth.models import User
class AcmeUser(User):
objects = None # we cannot really use this w/o local DB
def save(self):
"""saving to DB disabled"""
pass
def get_group_permissions(self):
"""If you don't make your own permissions module,
the default also will use the DB. Throw it away"""
return [] # likewise with the other permission defs
def get_and_delete_messages(self):
"""Messages are stored in the DB. Darn!"""
return []