def authenticate(self, request): authenticatorPlugins = [p for n, p in self.getAuthenticatorPlugins()] for name, credplugin in self.getCredentialsPlugins(): credentials = credplugin.extractCredentials(request) if credentials is None: # do not invoke the auth plugin without credentials continue for authplugin in authenticatorPlugins: if authplugin is None: continue principal = authplugin.authenticateCredentials(credentials) if principal is None: continue # create authenticated principal authenticated = interfaces.IAuthenticatedPrincipal(principal) # send the IAuthenticatedPrincipalCreated event zope.event.notify(event.AuthenticatedPrincipalCreated( self, authenticated, request)) return authenticated if self.includeNextUtilityForAuthenticate: next = queryNextUtility(self, IAuthentication, None) if next is not None: principal = next.authenticate(request) if principal is not None: return principal return None
def _utilities_up_tree(data): context = data while context is not None: manager = component.queryNextUtility(context, IWebhookSubscriptionManager) context = manager if manager is not None: yield '<NA>', manager
def getQueriables(self): """Returns an iteratable of queriables. Queriables are responsible for providing interfaces to search for principals by a set of given parameters (can be different for the various queriables). This method will walk up through all of the authentication utilities to look for queriables. >>> from zope.schema.interfaces import ISourceQueriables >>> @implementer(IAuthentication) ... class DummyUtility1(object): ... __parent__ = None ... def __repr__(self): return 'dummy1' >>> dummy1 = DummyUtility1() >>> @implementer(ISourceQueriables, IAuthentication) ... class DummyUtility2(object): ... __parent__ = None ... def getQueriables(self): ... return ('1', 1), ('2', 2), ('3', 3) >>> dummy2 = DummyUtility2() >>> @implementer(IAuthentication) ... class DummyUtility3(DummyUtility2): ... def getQueriables(self): ... return ('4', 4), >>> dummy3 = DummyUtility3() >>> from zope.authentication.tests.utils import testingNextUtility >>> testingNextUtility(dummy1, dummy2, IAuthentication) >>> testingNextUtility(dummy2, dummy3, IAuthentication) >>> from zope.component import provideUtility >>> provideUtility(dummy1) >>> from zope.authentication.principal import PrincipalSource >>> source = PrincipalSource() >>> list(source.getQueriables()) [(u'0', dummy1), (u'1.1', 1), (u'1.2', 2), (u'1.3', 3), (u'2.4', 4)] """ i = 0 auth = getUtility(IAuthentication) yielded = [] while True: queriables = ISourceQueriables(auth, None) if queriables is None: yield unicode(i), auth else: for qid, queriable in queriables.getQueriables(): # ensure that we dont return same yielded utility more # then once if queriable not in yielded: yield unicode(i)+'.'+unicode(qid), queriable yielded.append(queriable) auth = queryNextUtility(auth, IAuthentication) if auth is None: break i += 1
def __bool__(self): nz = bool(self.data) if not nz: # maybe higher-level utility's annotations will be non-zero next = queryNextUtility(self, IPrincipalAnnotationUtility) if next is not None: annotations = next.getAnnotationsById(self.principalId) return bool(annotations) return nz
def getQueriables(self): """Returns an iteratable of queriables. Queriables are responsible for providing interfaces to search for principals by a set of given parameters (can be different for the various queriables). This method will walk up through all of the authentication utilities to look for queriables. >>> class DummyUtility1: ... implements(IAuthentication) ... __parent__ = None ... def __repr__(self): return 'dummy1' >>> dummy1 = DummyUtility1() >>> class DummyUtility2: ... implements(ISourceQueriables, IAuthentication) ... __parent__ = None ... def getQueriables(self): ... return ('1', 1), ('2', 2), ('3', 3) >>> dummy2 = DummyUtility2() >>> class DummyUtility3(DummyUtility2): ... implements(IAuthentication) ... def getQueriables(self): ... return ('4', 4), >>> dummy3 = DummyUtility3() >>> from zope.component.nexttesting import testingNextUtility >>> testingNextUtility(dummy1, dummy2, IAuthentication) >>> testingNextUtility(dummy2, dummy3, IAuthentication) >>> from zope.component import provideUtility >>> provideUtility(dummy1) >>> source = PrincipalSource() >>> list(source.getQueriables()) [(u'0', dummy1), (u'1.1', 1), (u'1.2', 2), (u'1.3', 3), (u'2.4', 4)] """ i = 0 auth = getUtility(IAuthentication) yielded = [] while True: queriables = ISourceQueriables(auth, None) if queriables is None: yield unicode(i), auth else: for qid, queriable in queriables.getQueriables(): # ensure that we dont return same yielded utility more # then once if queriable not in yielded: yield unicode(i) + '.' + unicode(qid), queriable yielded.append(queriable) auth = queryNextUtility(auth, IAuthentication) if auth is None: break i += 1
def __getitem__(self, key): try: return self.data[key] except KeyError: # We failed locally: delegate to a higher-level utility. next = queryNextUtility(self, IPrincipalAnnotationUtility) if next is not None: annotations = next.getAnnotationsById(self.principalId) return annotations[key] raise
def getPrincipal(self, id): if not id.startswith(self.prefix): next = queryNextUtility(self, IAuthentication) if next is None: raise PrincipalLookupError(id) return next.getPrincipal(id) id = id[len(self.prefix):] for name, authplugin in self.getAuthenticatorPlugins(): info = authplugin.principalInfo(id) if info is None: continue info.credentialsPlugin = None info.authenticatorPlugin = authplugin principal = interfaces.IFoundPrincipalFactory(info)(self) principal.id = self.prefix + info.id return principal next = queryNextUtility(self, IAuthentication) if next is not None: return next.getPrincipal(self.prefix + id) raise PrincipalLookupError(id)
def logout(self, request): challengeProtocol = None for name, credplugin in self.getCredentialsPlugins(): protocol = getattr(credplugin, 'challengeProtocol', None) if challengeProtocol is None or protocol == challengeProtocol: if credplugin.logout(request): if protocol is None: return elif challengeProtocol is None: challengeProtocol = protocol if challengeProtocol is None: next = queryNextUtility(self, IAuthentication) if next is not None: next.logout(request)
def getPrincipal(self, id): for name, authplugin in self.getAuthenticatorPlugins(): principal = authplugin.queryPrincipal(id) if principal is None: continue # create found principal found = interfaces.IFoundPrincipal(principal) # send the IFoundPrincipalCreated event zope.event.notify(event.FoundPrincipalCreated(self, found)) return found next = queryNextUtility(self, IAuthentication) if next is not None: return next.getPrincipal(id) raise PrincipalLookupError(id)
def _callFUT(self, *args, **kw): from zope.component import queryNextUtility return queryNextUtility(*args, **kw)