在搜索代碼之前,一定要閱讀文檔。 http://docs.djangoproject.com/en/1.2/topics/auth/#other-authentication-sources 也讀取提供的Django源代碼。
你想創造三件事情。
捕獲令牌的中間件。這是大部分工作發生的地方。它檢查令牌,驗證它(通過與身份管理器確認),然後登錄用戶。
身份驗證後端查找用戶。這是一個存根。它所做的只是根據需要創建用戶。您的身份管理器有詳細信息。您只是在Django的本地數據庫上緩存當前版本的用戶。
這裏是中間件(編輯)。
from django.contrib.auth import authenticate, login
class CookieMiddleware(object):
"""Authentication Middleware for OpenAM using a cookie with a token.
Backend will get user.
"""
def process_request(self, request):
if not hasattr(request, 'user'):
raise ImproperlyConfigured()
if "thecookiename" not in request.COOKIES:
return
token= request.COOKIES["thecookiename"]
# REST request to OpenAM server for user attributes.
token, attribute, role = identity_manager.get_attributes(token)
user = authenticate(remote_user=attribute['uid'][0])
request.user = user
login(request, user)
的identity_manager.get_attributes
是一個單獨的類,我們寫信給驗證令牌和從IM源獲取用戶的詳細信息。當然,這是爲了測試目的而被嘲笑的。
這裏有一個後端(編輯)
class Backend(RemoteUserBackend):
def authenticate(**credentials):
"""We could authenticate the token by checking with OpenAM
Server. We don't do that here, instead we trust the middleware to do it.
"""
try:
user= User.objects.get(username=credentials['remote_user'])
except User.DoesNotExist:
user= User.objects.create(username=credentials['remote_user'])
# Here is a good place to map roles to Django Group instances or other features.
return user
這不會發生重大變化的認證或授權的裝飾。
爲了確保這一點,我們實際上刷新了我們的 身份管理器中的用戶和組信息。
請注意,中間件會針對每個請求運行。有時候,將令牌傳遞給支持的方法authenticate
是可以的。如果令牌存在於本地用戶數據庫中,則可以在不聯繫身份管理器的情況下繼續進行請求。
然而,我們在身份管理器中有複雜的規則和超時,所以我們必須檢查每個標記以確保它是有效的。一旦中間件確定令牌有效,我們就可以允許後端執行任何額外的處理。
這不是我們生活的代碼(這是一個有點太複雜,使一個很好的例子。)
後端可以簡化用戶,_ = User.objects.get_or_create(用戶名=憑證[「REMOTE_USER」] ) – 2013-03-07 23:43:10