Django的用户和认证从外部源 [英] Django users and authentication from external source

查看:245
本文介绍了Django的用户和认证从外部源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Django应用程序,从外部源(通过HTTP查询)完全得到它的数据。也就是说,我没有对本地数据库的选项。会话数据存储在缓存中(我的开发服务器上我使用SQLite数据库,所以这是没有错误源)。我使用的是尖端的Django 1.1svn。

I have a Django app that gets it's data completely from an external source (queried via HTTP). That is, I don't have the option for a local database. Session data is stored in the cache (on my development server I use a SQLite database, so that is no error source). I'm using bleeding edge Django 1.1svn.

输入问题:我想用Django自己的认证系统,为用户

Enter the problem: I want to use Django's own authentication system for the users.

这似乎写我自己的认证后端相当简单,但始终只是你有一个本地数据库用于保存用户的情况下。如果没有数据库我的主要问题是持久性。

It seems quite simple to write my own Authentication Backend, but always just under the condition that you have a local database where to save the users. Without database my main problem is persistence.

我用下面的试了一下(假设 datasource.get()是返回某种字典的功能):

I tried it with the following (assume that datasource.get() is a function that returns some kind of dict):

class ModelBackend (object):
    """Login backend."""

    def authenticate (self, username=None, password=None):
        """Check, if a given user/password combination is valid"""

        data = datasource.get ('login', username, password)
        if data and data['ok']:
            return MyUser (username=username)
        else:
            raise TypeError
            return None

    def get_user (self, username):
        """get data about a specific user"""

        try:
          data = datasource.get ('userdata', username)
          if data and data['ok']:
              return data.user
        except:
            pass
        return None


class MyUser (User):
    """Django user who isn't saved in DB"""

    def save (self):
        return None

但缺少故意保存()上MYUSER方法似乎打破一个登录的会话存储。

But the intentionally missing save() method on MyUser seems to break the session storage of a login.

应该如何 MYUSER 看起来像没有本地数据库?

How should MyUser look like without a local database?

推荐答案

确定,这要复杂得多比我想象。首先,先从<一个href=\"http://docs.djangoproject.com/en/dev/howto/auth-remote-user/\">http://docs.djangoproject.com/en/dev/howto/auth-remote-user/,但你需要将它与自己的后端和用户拓展。

OK, it's much more complicated than I thought. First, start with http://docs.djangoproject.com/en/dev/howto/auth-remote-user/, but you'll need to extend it with your own backend and user.

from django.contrib.auth.backends import RemoteUserBackend

class MyRemoteUserBackend (RemoteUserBackend):
    # Create a User object if not already in the database?
    create_unknown_user = False

    def get_user (self, user_id):
        user = somehow_create_an_instance_of (MyUser, user_id)
        return user

    def authenticate (self, **credentials):
        check_credentials ()
        user = somehow_create_an_instance_of (MyUser, credentials)
        return user

然后用户:

from django.contrib.auth.models import User

class MyUser (User):

    def save (self):
        """saving to DB disabled"""
        pass

    objects = None # we cannot really use this w/o local DB

    username = ""  # and all the other properties likewise.
                   # They're defined as model.CharField or similar,
                   # and we can't allow that

    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 []

唷! Django的真正没有数据库是不适合使用...

Phew! Django really isn't designed for usage without a database...

这篇关于Django的用户和认证从外部源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆