def handleChallenge_withCompletionHandler_()

in chef/cookbooks/cpe_nudge/files/nudge-python/resources/gurl.py [0:0]


    def handleChallenge_withCompletionHandler_(
            self, challenge, completionHandler):
        '''Handle an authentication challenge'''
        protectionSpace = challenge.protectionSpace()
        host = protectionSpace.host()
        realm = protectionSpace.realm()
        authenticationMethod = protectionSpace.authenticationMethod()
        self.log(
            'Authentication challenge for Host: %s Realm: %s AuthMethod: %s'
            % (host, realm, authenticationMethod))
        if challenge.previousFailureCount() > 0:
            # we have the wrong credentials. just fail
            self.log('Previous authentication attempt failed.')
            if completionHandler:
                completionHandler(
                    NSURLSessionAuthChallengeCancelAuthenticationChallenge,
                    None)
            else:
                challenge.sender().cancelAuthenticationChallenge_(challenge)
        if self.username and self.password and authenticationMethod in [
                'NSURLAuthenticationMethodDefault',
                'NSURLAuthenticationMethodHTTPBasic',
                'NSURLAuthenticationMethodHTTPDigest']:
            self.log('Will attempt to authenticate.')
            self.log('Username: %s Password: %s'
                     % (self.username, ('*' * len(self.password or ''))))
            credential = (
                NSURLCredential.credentialWithUser_password_persistence_(
                    self.username, self.password,
                    NSURLCredentialPersistenceNone))
            if completionHandler:
                completionHandler(
                    NSURLSessionAuthChallengeUseCredential, credential)
            else:
                challenge.sender().useCredential_forAuthenticationChallenge_(
                    credential, challenge)
        else:
            # fall back to system-provided default behavior
            self.log('Allowing OS to handle authentication request')
            if completionHandler:
                completionHandler(
                    NSURLSessionAuthChallengePerformDefaultHandling, None)
            else:
                if (challenge.sender().respondsToSelector_(
                        'performDefaultHandlingForAuthenticationChallenge:')):
                    self.log('Allowing OS to handle authentication request')
                    challenge.sender(
                        ).performDefaultHandlingForAuthenticationChallenge_(
                            challenge)
                else:
                    # Mac OS X 10.6 doesn't support
                    # performDefaultHandlingForAuthenticationChallenge:
                    self.log('Continuing without credential.')
                    challenge.sender(
                        ).continueWithoutCredentialForAuthenticationChallenge_(
                            challenge)