# Path: openid/server/trustroot.py
# _LOGGER = logging.getLogger(__name__)
# RP_RETURN_TO_URL_TYPE = 'http://specs.openid.net/auth/2.0/return_to'
# class RealmVerificationRedirected(Exception):
# class TrustRoot(object):
#     def __init__(self, relying_party_url, rp_url_after_redirects):
#     def __str__(self):
# def _parseURL(url):
#     def __init__(self, unparsed, proto, wildcard, host, port, path):
#     def isSane(self):
#     def validateURL(self, url):
#     def parse(cls, trust_root):
#     def checkSanity(cls, trust_root_string):
#     def checkURL(cls, trust_root, url):
#     def buildDiscoveryURL(self):
#     def __repr__(self):
#     def __str__(self):
# def _extractReturnURL(endpoint):
# def returnToMatches(allowed_return_to_urls, return_to):
# def getAllowedReturnURLs(relying_party_url):
# def verifyReturnTo(realm_str, return_to, _vrfy=getAllowedReturnURLs):
# 
# Path: openid/server/trustroot.py
# def getAllowedReturnURLs(relying_party_url):
#     """Given a relying party discovery URL return a list of return_to URLs.
# 
#     @since: 2.1.0
#     """
#     (rp_url_after_redirects, return_to_urls) = services.getServiceEndpoints(
#         relying_party_url, _extractReturnURL)
# 
#     if urinorm.urinorm(rp_url_after_redirects) != urinorm.urinorm(relying_party_url):
#         # Verification caused a redirect
#         raise RealmVerificationRedirected(
#             relying_party_url, rp_url_after_redirects)
# 
#     return return_to_urls
# 
# Path: openid/yadis/services.py
# def getServiceEndpoints(input_url, flt=None):
# def applyFilter(normalized_uri, xrd_data, flt=None):
# 
# Path: openid/yadis/discover.py
# class DiscoveryFailure(Exception):
#     """Raised when a YADIS protocol error occurs in the discovery process"""
#     identity_url = None
# 
#     def __init__(self, message, http_response):
#         Exception.__init__(self, message)
#         self.http_response = http_response
# 
# class DiscoveryResult(object):
#     """Contains the result of performing Yadis discovery on a URI"""
# 
#     # The URI that was passed to the fetcher
#     request_uri = None
# 
#     # The result of following redirects from the request_uri
#     normalized_uri = None
# 
#     # The URI from which the response text was returned (set to
#     # None if there was no XRDS document found)
#     xrds_uri = None
# 
#     # The content-type returned with the response_text
#     content_type = None
# 
#     # The document returned from the xrds_uri
#     response_text = None
# 
#     def __init__(self, request_uri):
#         """Initialize the state of the object
# 
#         sets all attributes to None except the request_uri
#         """
#         self.request_uri = request_uri
# 
#     def usedYadisLocation(self):
#         """Was the Yadis protocol's indirection used?"""
#         if self.xrds_uri is None:
#             return False
#         return self.normalized_uri != self.xrds_uri
# 
#     def isXRDS(self):
#         """Is the response text supposed to be an XRDS document?"""
#         return (self.usedYadisLocation() or self.content_type == YADIS_CONTENT_TYPE)


import unittest
from mock import patch, sentinel
from testfixtures import LogCapture, StringComparison
from openid.server import trustroot
from openid.server.trustroot import getAllowedReturnURLs
from openid.yadis import services
from openid.yadis.discover import DiscoveryFailure, DiscoveryResult

    def test_verifyWithDiscoveryCalled(self):
        realm = 'http://*.example.com/'
        return_to = 'http://www.example.com/foo'

        def vrfy(disco_url):
            self.assertEqual(disco_url, 'http://www.example.com/')
            return [return_to]

        with LogCapture() as logbook:
            self.assertTrue(trustroot.verifyReturnTo(realm, return_to, _vrfy=vrfy))
        self.assertEqual(logbook.records, [])

    def test_verifyFailWithDiscoveryCalled(self):
        realm = 'http://*.example.com/'
        return_to = 'http://www.example.com/foo'

        def vrfy(disco_url):
            self.assertEqual(disco_url, 'http://www.example.com/')
            return ['http://something-else.invalid/']

        with LogCapture() as logbook:
            self.assertFalse(trustroot.verifyReturnTo(realm, return_to, _vrfy=vrfy))
        logbook.check(('openid.server.trustroot', 'INFO', StringComparison('Failed to validate return_to .*')))

    def test_verifyFailIfDiscoveryRedirects(self):
        realm = 'http://*.example.com/'
        return_to = 'http://www.example.com/foo'

        def vrfy(disco_url):

# Path: openid/server/trustroot.py
# _LOGGER = logging.getLogger(__name__)
# RP_RETURN_TO_URL_TYPE = 'http://specs.openid.net/auth/2.0/return_to'
# class RealmVerificationRedirected(Exception):
# class TrustRoot(object):
#     def __init__(self, relying_party_url, rp_url_after_redirects):
#     def __str__(self):
# def _parseURL(url):
#     def __init__(self, unparsed, proto, wildcard, host, port, path):
#     def isSane(self):
#     def validateURL(self, url):
#     def parse(cls, trust_root):
#     def checkSanity(cls, trust_root_string):
#     def checkURL(cls, trust_root, url):
#     def buildDiscoveryURL(self):
#     def __repr__(self):
#     def __str__(self):
# def _extractReturnURL(endpoint):
# def returnToMatches(allowed_return_to_urls, return_to):
# def getAllowedReturnURLs(relying_party_url):
# def verifyReturnTo(realm_str, return_to, _vrfy=getAllowedReturnURLs):
# 
# Path: openid/server/trustroot.py
# def getAllowedReturnURLs(relying_party_url):
#     """Given a relying party discovery URL return a list of return_to URLs.
# 
#     @since: 2.1.0
#     """
#     (rp_url_after_redirects, return_to_urls) = services.getServiceEndpoints(
#         relying_party_url, _extractReturnURL)
# 
#     if urinorm.urinorm(rp_url_after_redirects) != urinorm.urinorm(relying_party_url):
#         # Verification caused a redirect
#         raise RealmVerificationRedirected(
#             relying_party_url, rp_url_after_redirects)
# 
#     return return_to_urls
# 
# Path: openid/yadis/services.py
# def getServiceEndpoints(input_url, flt=None):
# def applyFilter(normalized_uri, xrd_data, flt=None):
# 
# Path: openid/yadis/discover.py
# class DiscoveryFailure(Exception):
#     """Raised when a YADIS protocol error occurs in the discovery process"""
#     identity_url = None
# 
#     def __init__(self, message, http_response):
#         Exception.__init__(self, message)
#         self.http_response = http_response
# 
# class DiscoveryResult(object):
#     """Contains the result of performing Yadis discovery on a URI"""
# 
#     # The URI that was passed to the fetcher
#     request_uri = None
# 
#     # The result of following redirects from the request_uri
#     normalized_uri = None
# 
#     # The URI from which the response text was returned (set to
#     # None if there was no XRDS document found)
#     xrds_uri = None
# 
#     # The content-type returned with the response_text
#     content_type = None
# 
#     # The document returned from the xrds_uri
#     response_text = None
# 
#     def __init__(self, request_uri):
#         """Initialize the state of the object
# 
#         sets all attributes to None except the request_uri
#         """
#         self.request_uri = request_uri
# 
#     def usedYadisLocation(self):
#         """Was the Yadis protocol's indirection used?"""
#         if self.xrds_uri is None:
#             return False
#         return self.normalized_uri != self.xrds_uri
# 
#     def isXRDS(self):
#         """Is the response text supposed to be an XRDS document?"""
#         return (self.usedYadisLocation() or self.content_type == YADIS_CONTENT_TYPE)


# Path: openid/test/test_rpverify.py
import unittest
from mock import patch, sentinel
from testfixtures import LogCapture, StringComparison
from openid.server import trustroot
from openid.server.trustroot import getAllowedReturnURLs
from openid.yadis import services
from openid.yadis.discover import DiscoveryFailure, DiscoveryResult


    def test_verifyWithDiscoveryCalled(self):
        realm = 'http://*.example.com/'
        return_to = 'http://www.example.com/foo'

        def vrfy(disco_url):
            self.assertEqual(disco_url, 'http://www.example.com/')
            return [return_to]

        with LogCapture() as logbook:
            self.assertTrue(trustroot.verifyReturnTo(realm, return_to, _vrfy=vrfy))
        self.assertEqual(logbook.records, [])

    def test_verifyFailWithDiscoveryCalled(self):
        realm = 'http://*.example.com/'
        return_to = 'http://www.example.com/foo'

        def vrfy(disco_url):
            self.assertEqual(disco_url, 'http://www.example.com/')
            return ['http://something-else.invalid/']

        with LogCapture() as logbook:
            self.assertFalse(trustroot.verifyReturnTo(realm, return_to, _vrfy=vrfy))
        logbook.check(('openid.server.trustroot', 'INFO', StringComparison('Failed to validate return_to .*')))

    def test_verifyFailIfDiscoveryRedirects(self):
        realm = 'http://*.example.com/'
        return_to = 'http://www.example.com/foo'

        def vrfy(disco_url):

            raise trustroot.RealmVerificationRedirected(