Changeset 45 for python


Ignore:
Timestamp:
12/30/07 10:40:53 (12 years ago)
Author:
sgk
Message:

ライセンス、コメントの英語化。

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/ac/httpauth.py

    r44 r45  
    1 # vim:fileencoding=utf-8 
    2 # Copyright (c) 2007 Accense Technology, Inc. 
    3 # //sgk 
     1''' 
     2Django middleware for HTTP authentication. 
     3 
     4Copyright (c) 2007, Accense Technology, Inc. 
     5 
     6All rights reserved. 
     7 
     8Redistribution and use in source and binary forms, with or without 
     9modification, 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 
     20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
     21"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
     22LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
     23A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
     24CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
     25EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
     26PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
     27PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
     28LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
     29NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
     30SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
     31''' 
    432 
    533from django.contrib.auth.models import User, AnonymousUser 
     
    735import base64 
    836 
    9 # django.contrib.auth.middleware.AuthenticationMiddlewareの実装、 
    10 # request._cached_userに依存。 
     37# Written by sgk. 
    1138 
    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 
    1843class 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 
    1954  def process_request(self, request): 
    2055    if hasattr(request, '_cached_user'): 
     
    2459      user = User.objects.get(username=username) 
    2560    except (KeyError, User.DoesNotExist): 
    26       return None                       # 他の認証ミドルウェアに期待する。 
     61      # Fallback to other authentication middleware. 
     62      return None 
    2763    request._cached_user = user 
    2864    return None 
    2965 
    3066 
    31 # 
    32 # 自らHTTP認証を行う。 
    33 # 
    3467class 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  ''' 
    3579  def process_request(self, request): 
    3680    if hasattr(request, '_cached_user'): 
     
    4589        user = AnonymousUser() 
    4690    except (KeyError, TypeError, User.DoesNotExist): 
    47       return None                       # 他の認証ミドルウェアに期待する。 
     91      # Fallback to other authentication middleware. 
     92      return None 
    4893    request._cached_user = user 
    4994    return None 
    5095 
    5196 
    52 # 
    53 # 上記httpauth.Middlewareで認証を行う場合にビューにつけるデコレータ。 
    54 # 
    5597def 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  ''' 
    56105  def decorator(func): 
    57106    def handler(request, *args, **kw): 
Note: See TracChangeset for help on using the changeset viewer.