Back to index

moin  1.9.0~rc2
http.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - http authentication
00004 
00005     HTTPAuth
00006     ========
00007 
00008     HTTPAuth is just a dummy redirecting to MoinMoin.auth.GivenAuth for backwards
00009     compatibility.
00010 
00011     Please fix your setup, this dummy will be removed soon:
00012 
00013     Old (1.8.x):
00014     ------------
00015     from MoinMoin.auth.http import HTTPAuth
00016     auth = [HTTPAuth(autocreate=True)]
00017     # any presence (or absence) of 'http' auth name, e.g.:
00018     auth_methods_trusted = ['http', 'xmlrpc_applytoken']
00019 
00020     New (1.9.x):
00021     ------------
00022     from MoinMoin.auth import GivenAuth
00023     auth = [GivenAuth(autocreate=True)]
00024     # presence (or absence) of 'given' auth name, e.g.:
00025     auth_methods_trusted = ['given', 'xmlrpc_applytoken']
00026 
00027     HTTPAuthMoin
00028     ============
00029 
00030     HTTPAuthMoin is HTTP auth done by moin (not by your web server).
00031 
00032     Moin will request HTTP Basic Auth and use the HTTP Basic Auth header it
00033     receives to authenticate username/password against the moin user profiles.
00034 
00035     from MoinMoin.auth.http import HTTPAuthMoin
00036     auth = [HTTPAuthMoin()]
00037     # check if you want 'http' auth name in there:
00038     auth_methods_trusted = ['http', 'xmlrpc_applytoken']
00039 
00040     @copyright: 2009 MoinMoin:ThomasWaldmann
00041     @license: GNU GPL, see COPYING for details.
00042 """
00043 
00044 from MoinMoin import log
00045 logging = log.getLogger(__name__)
00046 
00047 from MoinMoin import config, user
00048 from MoinMoin.auth import BaseAuth, GivenAuth
00049 
00050 
00051 class HTTPAuth(GivenAuth):
00052     name = 'http'  # GivenAuth uses 'given'
00053     logging.warning("DEPRECATED use of MoinMoin.auth.http.HTTPAuth, please read instructions there or docs/CHANGES!")
00054 
00055 
00056 class HTTPAuthMoin(BaseAuth):
00057     """ authenticate via http (basic) auth """
00058     name = 'http'
00059 
00060     def __init__(self, autocreate=False, realm='MoinMoin', coding='iso-8859-1'):
00061         self.autocreate = autocreate
00062         self.realm = realm
00063         self.coding = coding
00064         BaseAuth.__init__(self)
00065 
00066     def request(self, request, user_obj, **kw):
00067         u = None
00068         _ = request.getText
00069         # always revalidate auth
00070         if user_obj and user_obj.auth_method == self.name:
00071             user_obj = None
00072         # something else authenticated before us
00073         if user_obj:
00074             return user_obj, True
00075 
00076         auth = request.authorization
00077         if auth and auth.username and auth.password is not None:
00078             logging.debug("http basic auth, received username: %r password: %r" % (
00079                           auth.username, auth.password))
00080             u = user.User(request,
00081                           name=auth.username.decode(self.coding),
00082                           password=auth.password.decode(self.coding),
00083                           auth_method=self.name, auth_attribs=[])
00084             logging.debug("user: %r" % u)
00085 
00086         if not u or not u.valid:
00087             from werkzeug import Response, abort
00088             response = Response(_('Please log in first.'), 401,
00089                                 {'WWW-Authenticate': 'Basic realm="%s"' % self.realm})
00090             abort(response)
00091 
00092         logging.debug("u: %r" % u)
00093         if u and self.autocreate:
00094             logging.debug("autocreating user")
00095             u.create_or_update()
00096         if u and u.valid:
00097             logging.debug("returning valid user %r" % u)
00098             return u, True # True to get other methods called, too
00099         else:
00100             logging.debug("returning %r" % user_obj)
00101             return user_obj, True
00102