示例#1
0
    def authenticate(self, environ, classification, identities):
        """Override authenticate to return as soon as
        first authenticator passes
        """
        candidates = self.registry.get(IAuthenticator, [])
        self.log('authenticator plugins registered %s' %
                                    candidates, 'info')
        plugins = match_classification(IAuthenticator, candidates,
                                       classification)
        self.log('authenticator plugins matched for '
            'classification "%s": %s' % (classification, plugins), 'info')

        # 'preauthenticated' identities are considered best-ranking
        identities, results, id_rank_start = self._filter_preauthenticated(
            identities)

        # Stop wasting CPU circles return early we are already authenticated
        if results:
            self.log('userid: %s preauthenticated' % results[0][4])
            return results

        auth_rank = 0

        for plugin in plugins:
            if results:
                # Authenticated breakout
                self.log(
                    'userid: %s authenticated using %s '
                    'discontinuing futher plugin checks' %
                    (results[0][4], results[0][1]))
                break
            identifier_rank = id_rank_start
            for identifier, identity in identities:
                userid = plugin.authenticate(environ, identity)
                if userid is not None:
                    self.log('userid returned from %s: "%s"' %
                        (plugin, userid))

                    # stamp the identity with the userid
                    identity['repoze.who.userid'] = userid
                    user_dn = ''
                    try:
                        plugin_name = plugin.name
                        user_dn = identity['userdata']
                    except AttributeError:
                        plugin_name = str(plugin)
                    except KeyError:
                        pass
                    identity['tokens'] = [plugin_name, user_dn]
                    rank = (auth_rank, identifier_rank)
                    results.append(
                        (rank, plugin, identifier, identity, userid))
                else:
                    self.log('no userid returned from %s: (%s)' %
                        (plugin, userid))
                identifier_rank += 1
            auth_rank += 1

        self.log('identities authenticated: %s' % (results,))
        return results
 def test_dont_match_default_classification(self):
     identifier = X509Identifier("Test")
     assert identifier not in match_classification(IIdentifier, (identifier,), "other")
 def test_dont_match_custom_classification(self):
     identifier = X509Identifier("Test", classifications=["ios", "browser"])
     assert identifier not in match_classification(IIdentifier, (identifier,), "other")
 def test_match_default_classification(self):
     identifier = X509Identifier("Test")
     assert identifier in match_classification(IIdentifier, (identifier,), "browser")