# 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_garbageMatch(self):
        r = 'http://example.com/return.to'
        realm = 'This is not a URL at all. In fact, it has characters, like "<" that are not allowed in URLs'
        self.assertTrue(trustroot.returnToMatches([realm, r], r))

    def test_descendant(self):
        r = 'http://example.com/return.to'
        self.assertTrue(trustroot.returnToMatches([r], 'http://example.com/return.to/user:joe'))

    def test_wildcard(self):
        self.assertFalse(trustroot.returnToMatches(['http://*.example.com/return.to'], 'http://example.com/return.to'))

    def test_noMatch(self):
        r = 'http://example.com/return.to'
        self.assertFalse(trustroot.returnToMatches([r], 'http://example.com/xss_exploit'))


class TestGetAllowedReturnURLs(unittest.TestCase):

    def test_equal(self):
        with patch('openid.yadis.services.getServiceEndpoints', autospec=True,
                   return_value=('http://example.com/', sentinel.endpoints)):
            endpoints = getAllowedReturnURLs('http://example.com/')

        self.assertEqual(endpoints, sentinel.endpoints)

    def test_normalized(self):
        # Test redirect is not reported when the returned URL is normalized.
        with patch('openid.yadis.services.getServiceEndpoints', autospec=True,
                   return_value=('http://example.com/', sentinel.endpoints)):

# 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_garbageMatch(self):
        r = 'http://example.com/return.to'
        realm = 'This is not a URL at all. In fact, it has characters, like "<" that are not allowed in URLs'
        self.assertTrue(trustroot.returnToMatches([realm, r], r))

    def test_descendant(self):
        r = 'http://example.com/return.to'
        self.assertTrue(trustroot.returnToMatches([r], 'http://example.com/return.to/user:joe'))

    def test_wildcard(self):
        self.assertFalse(trustroot.returnToMatches(['http://*.example.com/return.to'], 'http://example.com/return.to'))

    def test_noMatch(self):
        r = 'http://example.com/return.to'
        self.assertFalse(trustroot.returnToMatches([r], 'http://example.com/xss_exploit'))


class TestGetAllowedReturnURLs(unittest.TestCase):

    def test_equal(self):
        with patch('openid.yadis.services.getServiceEndpoints', autospec=True,
                   return_value=('http://example.com/', sentinel.endpoints)):
            endpoints = getAllowedReturnURLs('http://example.com/')

        self.assertEqual(endpoints, sentinel.endpoints)

    def test_normalized(self):
        # Test redirect is not reported when the returned URL is normalized.
        with patch('openid.yadis.services.getServiceEndpoints', autospec=True,
                   return_value=('http://example.com/', sentinel.endpoints)):

            endpoints = getAllowedReturnURLs('http://example.com:80')