チェンジセット 45: python/ac/httpauth.py
- コミット日時:
- 2007/12/30 19:40:53 (1 年前)
- ファイル:
-
- python/ac/httpauth.py (更新) (4 diffs)
凡例:
- 変更無し
- 追加
- 削除
- 更新
- コピー
- 移動
python/ac/httpauth.py
r44 r45 1 # vim:fileencoding=utf-8 2 # Copyright (c) 2007 Accense Technology, Inc. 3 # //sgk 1 ''' 2 Django middleware for HTTP authentication. 3 4 Copyright (c) 2007, Accense Technology, Inc. 5 6 All rights reserved. 7 8 Redistribution and use in source and binary forms, with or without 9 modification, are permitted provided that the following conditions are met: 10 11 * Redistributions of source code must retain the above copyright notice, 12 this list of conditions and the following disclaimer. 13 * Redistributions in binary form must reproduce the above copyright notice, 14 this list of conditions and the following disclaimer in the documentation 15 and/or other materials provided with the distribution. 16 * Neither the name of the Accense Technology nor the names of its 17 contributors may be used to endorse or promote products derived from 18 this software without specific prior written permission. 19 20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 ''' 4 32 5 33 from django.contrib.auth.models import User, AnonymousUser … … 7 35 import base64 8 36 9 # django.contrib.auth.middleware.AuthenticationMiddlewareの実装、 10 # request._cached_userに依存。 37 # Written by sgk. 11 38 12 # 13 # DjangoをホストしているHTTPサーバによる認証を信じる。 14 # 15 # 上記middlwareよりも前に置くことによって、 16 # Userオブジェクトを先にキャッシュさせている。 17 # 各ビューには@login_requiredデコレータを置く。LOGIN_URLは適切に設定すること。 39 # This code depends on the implementation internals of the Django builtin 40 # 'django.contrib.auth.middleware.AuthenticationMiddleware' authentication 41 # middleware, specifically the 'request._cached_user' member. 42 18 43 class ByHttpServerMiddleware(object): 44 ''' 45 Reflect the authentication result by HTTP server which hosts Django. 46 47 This middleware must be placed in the 'settings.py' MIDDLEWARE_CLASSES 48 definition before the above Django builtin 'AuthenticationMiddleware'. 49 You can use the ordinaly '@login_required' decorator to restrict views 50 to authenticated users. Set the 'settings.py' LOGIN_URL definition 51 appropriately if required. 52 ''' 53 19 54 def process_request(self, request): 20 55 if hasattr(request, '_cached_user'): … … 24 59 user = User.objects.get(username=username) 25 60 except (KeyError, User.DoesNotExist): 26 return None # 他の認証ミドルウェアに期待する。 61 # Fallback to other authentication middleware. 62 return None 27 63 request._cached_user = user 28 64 return None 29 65 30 66 31 #32 # 自らHTTP認証を行う。33 #34 67 class Middleware(object): 68 ''' 69 Django implementation of the HTTP basic authentication. 70 71 This middleware must be placed in the 'settings.py' MIDDLEWARE_CLASSES 72 definition before the above Django builtin 'AuthenticationMiddleware'. 73 Set the 'settings.py' LOGIN_URL definition appropriately if required. 74 75 To show the browser generated login dialog to user, you have to use the 76 following '@http_login_required(realm=realm)' decorator instead of the 77 ordinaly '@login_required' decorator. 78 ''' 35 79 def process_request(self, request): 36 80 if hasattr(request, '_cached_user'): … … 45 89 user = AnonymousUser() 46 90 except (KeyError, TypeError, User.DoesNotExist): 47 return None # 他の認証ミドルウェアに期待する。 91 # Fallback to other authentication middleware. 92 return None 48 93 request._cached_user = user 49 94 return None 50 95 51 96 52 #53 # 上記httpauth.Middlewareで認証を行う場合にビューにつけるデコレータ。54 #55 97 def http_login_required(realm=None): 98 ''' 99 Decorator factory to restrict views to authenticated user and show the 100 browser generated login dialog if the user is not authenticated. 101 102 This is the function that returns a decorator. To use the decorator, 103 use '@http_login_required()' or '@http_login_required(realm='...')'. 104 ''' 56 105 def decorator(func): 57 106 def handler(request, *args, **kw):
