~ubuntu-branches/debian/jessie/armory/jessie

« back to all changes in this revision

Viewing changes to txjsonrpc/auth.py

  • Committer: Package Import Robot
  • Author(s): Joseph Bisch
  • Date: 2014-10-07 10:22:45 UTC
  • Revision ID: package-import@ubuntu.com-20141007102245-2s3x3rhjxg689hek
Tags: upstream-0.92.3
ImportĀ upstreamĀ versionĀ 0.92.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from zope.interface import Interface, implements
 
2
 
 
3
try:
 
4
    from twisted import web
 
5
except ImportError:
 
6
    web = None
 
7
 
 
8
from twisted.cred.portal import IRealm, Portal
 
9
 
 
10
 
 
11
class HTTPAuthRealm(object):
 
12
 
 
13
    implements(IRealm)
 
14
 
 
15
    def __init__(self, resource):
 
16
        self.resource = resource
 
17
 
 
18
    def logout(self):
 
19
        pass
 
20
 
 
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()
 
25
 
 
26
 
 
27
def _wrapTwistedWebResource(resource, checkers, credFactories=[],
 
28
                            realmName=""):
 
29
    if not web:
 
30
        raise ImportError("twisted.web does not seem to be installed.")
 
31
    from twisted.web import guard
 
32
 
 
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)
 
38
 
 
39
 
 
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)