1
from zope.interface import Interface, implements
4
from twisted import web
8
from twisted.cred.portal import IRealm, Portal
11
class HTTPAuthRealm(object):
15
def __init__(self, resource):
16
self.resource = resource
21
def requestAvatar(self, avatarId, mind, *interfaces):
22
if web.resource.IResource in interfaces:
23
return web.resource.IResource, self.resource, self.logout
24
raise NotImplementedError()
27
def _wrapTwistedWebResource(resource, checkers, credFactories=[],
30
raise ImportError("twisted.web does not seem to be installed.")
31
from twisted.web import guard
33
defaultCredFactory = guard.BasicCredentialFactory(realmName)
34
credFactories.insert(0, defaultCredFactory)
35
realm = HTTPAuthRealm(resource)
36
portal = Portal(realm, checkers)
37
return guard.HTTPAuthSessionWrapper(portal, credFactories)
40
def wrapResource(resource, *args, **kwargs):
41
if web.resource.IResource.providedBy(resource):
42
return _wrapTwistedWebResource(resource, *args, **kwargs)
43
elif web2.iweb.IResource.providedBy(resource):
44
return _wrapTwistedWeb2Resource(resource, *args, **kwargs)