~justin-fathomdb/nova/justinsb-openstack-api-volumes

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/doc/web/examples/webguard.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2008 Twisted Matrix Laboratories.
 
2
# See LICENSE for details.
 
3
 
 
4
import sys
 
5
 
 
6
from zope.interface import implements
 
7
 
 
8
from twisted.python import log
 
9
from twisted.internet import reactor
 
10
from twisted.web import server, resource, guard
 
11
from twisted.cred.portal import IRealm, Portal
 
12
from twisted.cred.checkers import InMemoryUsernamePasswordDatabaseDontUse
 
13
 
 
14
 
 
15
class GuardedResource(resource.Resource):
 
16
    """
 
17
    A resource which is protected by guard and requires authentication in order
 
18
    to access.
 
19
    """
 
20
    def getChild(self, path, request):
 
21
        return self
 
22
 
 
23
 
 
24
    def render(self, request):
 
25
        return "Authorized!"
 
26
 
 
27
 
 
28
 
 
29
class SimpleRealm(object):
 
30
    """
 
31
    A realm which gives out L{GuardedResource} instances for authenticated
 
32
    users.
 
33
    """
 
34
    implements(IRealm)
 
35
 
 
36
    def requestAvatar(self, avatarId, mind, *interfaces):
 
37
        if resource.IResource in interfaces:
 
38
            return resource.IResource, GuardedResource(), lambda: None
 
39
        raise NotImplementedError()
 
40
 
 
41
 
 
42
 
 
43
def main():
 
44
    log.startLogging(sys.stdout)
 
45
    checkers = [InMemoryUsernamePasswordDatabaseDontUse(joe='blow')]
 
46
    wrapper = guard.HTTPAuthSessionWrapper(
 
47
        Portal(SimpleRealm(), checkers),
 
48
        [guard.DigestCredentialFactory('md5', 'example.com')])
 
49
    reactor.listenTCP(8889, server.Site(
 
50
          resource = wrapper))
 
51
    reactor.run()
 
52
 
 
53
if __name__ == '__main__':
 
54
    main()