Package couchdbkit :: Package ext :: Package pylons :: Package auth :: Module basic
[hide private]
[frames] | no frames]

Source Code for Module couchdbkit.ext.pylons.auth.basic

  1  # -*- coding: utf-8 - 
  2  # 
  3  # This file is part of couchdbkit released under the MIT license.  
  4  # See the NOTICE for more information. 
  5   
  6  import logging 
  7  from paste.request import parse_dict_querystring, parse_formvars 
  8  from paste.httpexceptions import HTTPUnauthorized 
  9  from paste.httpheaders import CONTENT_LENGTH, CONTENT_TYPE 
 10  from repoze.what.middleware import setup_auth 
 11  from repoze.who.plugins.auth_tkt import AuthTktCookiePlugin 
 12  from repoze.who.interfaces import IChallenger, IIdentifier 
 13   
 14  import sys 
 15  from zope.interface import implements 
 16   
 17  from .adapters import GroupAdapter, PermissionAdapter, \ 
 18  Authenticator, MDPlugin 
 19   
 20   
21 -class BasicAuth(object):
22 """A basic challenger and identifier""" 23 implements(IChallenger, IIdentifier) 24
25 - def __init__(self, login_url="/user/login", logout_url="/user/logout"):
26 self._login_url = login_url 27 self._logout_url = logout_url
28
29 - def identify(self, environ):
30 path_info = environ['PATH_INFO'] 31 query = parse_dict_querystring(environ) 32 33 # This will handle the logout request. 34 if path_info == self._logout_url: 35 # set in environ for self.challenge() to find later 36 environ['repoze.who.application'] = HTTPUnauthorized() 37 return None 38 elif path_info == self._login_url: 39 form = parse_formvars(environ) 40 form.update(query) 41 try: 42 credentials = { 43 'login': form['login'], 44 'password': form['password'] 45 } 46 except KeyError: 47 credentials = None 48 49 def auth_resp(environ, start_response): 50 import json 51 resp = { 52 "success": True 53 } 54 55 resp_str = json.dumps(resp) 56 57 content_length = CONTENT_LENGTH.tuples(str(len(resp_str))) 58 content_type = CONTENT_TYPE.tuples('application/json') 59 headers = content_length + content_type 60 start_response('200 OK', headers) 61 return [resp_str]
62 63 environ['repoze.who.application'] = auth_resp 64 return credentials
65
66 - def challenge(self, environ, status, app_headers, forget_headers):
67 cookies = [(h,v) for (h,v) in app_headers if h.lower() == 'set-cookie'] 68 if not forget_headers: 69 return HTTPUnauthorized() 70 71 def auth_form(environ, start_response): 72 towrite = "Challenging this" 73 content_length = CONTENT_LENGTH.tuples(str(len(towrite))) 74 content_type = CONTENT_TYPE.tuples('text/html') 75 headers = content_length + content_type + forget_headers 76 start_response('200 OK', headers) 77 return [towrite]
78 return auth_form 79
80 - def remember(self, environ, identity):
81 return environ['repoze.who.plugins']['cookie'].remember(environ, identity)
82
83 - def forget(self, environ, identity):
84 return environ['repoze.who.plugins']['cookie'].forget(environ, identity)
85
86 -def AuthBasicMiddleware(app, conf, user_class):
87 groups = GroupAdapter(user_class) 88 groups = {'all_groups': groups} 89 permissions = {'all_perms': PermissionAdapter(conf["couchdb.db"])} 90 91 basicauth = BasicAuth() 92 cookie = AuthTktCookiePlugin(conf['cookies.secret']) 93 94 who_args = {} 95 who_args['authenticators'] = [('accounts', Authenticator(user_class))] 96 who_args['challengers'] = [('basicauth', basicauth)] 97 who_args['identifiers'] = [('basicauth', basicauth), ('cookie', cookie)] 98 who_args['mdproviders'] = [('accounts', MDPlugin(user_class))] 99 who_args['log_stream'] = sys.stdout 100 who_args['log_level'] = logging.DEBUG 101 102 return setup_auth(app, groups, permissions, **who_args)
103