1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
5 This example shows how to make simple web authentication.
10 When you visit http://127.0.0.1:8889/, the page will ask for an username &
11 password. See the code in main() to get the correct username & password!
16 from zope.interface import implements
18 from twisted.python import log
19 from twisted.internet import reactor
20 from twisted.web import server, resource, guard
21 from twisted.cred.portal import IRealm, Portal
22 from twisted.cred.checkers import InMemoryUsernamePasswordDatabaseDontUse
25 class GuardedResource(resource.Resource):
27 A resource which is protected by guard and requires authentication in order
30 def getChild(self, path, request):
34 def render(self, request):
39 class SimpleRealm(object):
41 A realm which gives out L{GuardedResource} instances for authenticated
46 def requestAvatar(self, avatarId, mind, *interfaces):
47 if resource.IResource in interfaces:
48 return resource.IResource, GuardedResource(), lambda: None
49 raise NotImplementedError()
54 log.startLogging(sys.stdout)
55 checkers = [InMemoryUsernamePasswordDatabaseDontUse(joe='blow')]
56 wrapper = guard.HTTPAuthSessionWrapper(
57 Portal(SimpleRealm(), checkers),
58 [guard.DigestCredentialFactory('md5', 'example.com')])
59 reactor.listenTCP(8889, server.Site(
63 if __name__ == '__main__':