# Path: tokenserver/verifiers.py
# class RemoteBrowserIdVerifier(object):
#     implements(IBrowserIdVerifier)
# 
#     def __init__(self, audiences=None, trusted_issuers=None,
#                  allowed_issuers=None, verifier_url=None, timeout=None):
#         # Since we don't parse the assertion locally, we cannot support
#         # list- or pattern-based audience strings.
#         if audiences is not None:
#             assert isinstance(audiences, basestring)
#         self.audiences = audiences
#         if isinstance(trusted_issuers, basestring):
#             trusted_issuers = trusted_issuers.split()
#         self.trusted_issuers = trusted_issuers
#         if isinstance(allowed_issuers, basestring):
#             allowed_issuers = allowed_issuers.split()
#         self.allowed_issuers = allowed_issuers
#         if verifier_url is None:
#             verifier_url = "https://verifier.accounts.firefox.com/v2"
#         self.verifier_url = verifier_url
#         if timeout is None:
#             timeout = 30
#         self.timeout = timeout
#         self.session = requests.Session()
#         self.session.verify = True
# 
#     def verify(self, assertion, audience=None):
#         if audience is None:
#             audience = self.audiences
# 
#         body = {'assertion': assertion, 'audience': audience}
#         if self.trusted_issuers is not None:
#             body['trustedIssuers'] = self.trusted_issuers
#         headers = {'content-type': 'application/json'}
#         try:
#             response = self.session.post(self.verifier_url,
#                                          data=json.dumps(body),
#                                          headers=headers,
#                                          timeout=self.timeout)
#         except (socket.error, requests.RequestException), e:
#             msg = "Failed to POST %s. Reason: %s" % (self.verifier_url, str(e))
#             raise ConnectionError(msg)
# 
#         if response.status_code != 200:
#             raise ConnectionError('server returned invalid response code')
#         try:
#             data = json.loads(response.text)
#         except ValueError:
#             raise ConnectionError("server returned invalid response body")
# 
#         if data.get('status') != "okay":
#             reason = data.get('reason', 'unknown error')
#             if "audience mismatch" in reason:
#                 raise AudienceMismatchError(data.get("audience"), audience)
#             if "expired" in reason or "issued later than" in reason:
#                 raise ExpiredSignatureError(reason)
#             raise InvalidSignatureError(reason)
#         if self.allowed_issuers is not None:
#             issuer = data.get('issuer')
#             if issuer not in self.allowed_issuers:
#                 raise InvalidIssuerError("Issuer not allowed: %s" % (issuer,))
#         return data
# 
# class IBrowserIdVerifier(Interface):
#     pass


import json
import contextlib
import unittest
import browserid.errors
from pyramid.config import Configurator
from tokenserver.verifiers import RemoteBrowserIdVerifier, IBrowserIdVerifier
from browserid.tests.support import make_assertion
            response.__dict__.update(response_attrs)
            return response
        orig_post_method = verifier.session.post
        verifier.session.post = replacement_post_method
        try:
            yield None
        finally:
            verifier.session.post = orig_post_method

    def test_verifier_config_loading_defaults(self):
        config = self._make_config()
        verifier = config.registry.getUtility(IBrowserIdVerifier)
        self.assertTrue(isinstance(verifier, RemoteBrowserIdVerifier))
        self.assertEquals(verifier.verifier_url,
                          "https://verifier.accounts.firefox.com/v2")
        self.assertEquals(verifier.audiences, None)
        self.assertEquals(verifier.trusted_issuers, None)
        self.assertEquals(verifier.allowed_issuers, None)

    def test_verifier_config_loading_values(self):
        config = self._make_config({  # noqa; indentation below is non-standard
            "browserid.verifier_url":
                "https://trustyverifier.notascam.com/endpoint/path",
            "browserid.audiences":
                "https://testmytoken.com",
            "browserid.trusted_issuers":
                "example.com trustyidp.org",
            "browserid.allowed_issuers":
                "example.com trustyidp.org\nmockmyid.com",
        })

# Path: tokenserver/verifiers.py
# class RemoteBrowserIdVerifier(object):
#     implements(IBrowserIdVerifier)
# 
#     def __init__(self, audiences=None, trusted_issuers=None,
#                  allowed_issuers=None, verifier_url=None, timeout=None):
#         # Since we don't parse the assertion locally, we cannot support
#         # list- or pattern-based audience strings.
#         if audiences is not None:
#             assert isinstance(audiences, basestring)
#         self.audiences = audiences
#         if isinstance(trusted_issuers, basestring):
#             trusted_issuers = trusted_issuers.split()
#         self.trusted_issuers = trusted_issuers
#         if isinstance(allowed_issuers, basestring):
#             allowed_issuers = allowed_issuers.split()
#         self.allowed_issuers = allowed_issuers
#         if verifier_url is None:
#             verifier_url = "https://verifier.accounts.firefox.com/v2"
#         self.verifier_url = verifier_url
#         if timeout is None:
#             timeout = 30
#         self.timeout = timeout
#         self.session = requests.Session()
#         self.session.verify = True
# 
#     def verify(self, assertion, audience=None):
#         if audience is None:
#             audience = self.audiences
# 
#         body = {'assertion': assertion, 'audience': audience}
#         if self.trusted_issuers is not None:
#             body['trustedIssuers'] = self.trusted_issuers
#         headers = {'content-type': 'application/json'}
#         try:
#             response = self.session.post(self.verifier_url,
#                                          data=json.dumps(body),
#                                          headers=headers,
#                                          timeout=self.timeout)
#         except (socket.error, requests.RequestException), e:
#             msg = "Failed to POST %s. Reason: %s" % (self.verifier_url, str(e))
#             raise ConnectionError(msg)
# 
#         if response.status_code != 200:
#             raise ConnectionError('server returned invalid response code')
#         try:
#             data = json.loads(response.text)
#         except ValueError:
#             raise ConnectionError("server returned invalid response body")
# 
#         if data.get('status') != "okay":
#             reason = data.get('reason', 'unknown error')
#             if "audience mismatch" in reason:
#                 raise AudienceMismatchError(data.get("audience"), audience)
#             if "expired" in reason or "issued later than" in reason:
#                 raise ExpiredSignatureError(reason)
#             raise InvalidSignatureError(reason)
#         if self.allowed_issuers is not None:
#             issuer = data.get('issuer')
#             if issuer not in self.allowed_issuers:
#                 raise InvalidIssuerError("Issuer not allowed: %s" % (issuer,))
#         return data
# 
# class IBrowserIdVerifier(Interface):
#     pass


# Path: tokenserver/tests/test_remote_browserid_verifier.py
import json
import contextlib
import unittest
import browserid.errors
from pyramid.config import Configurator
from tokenserver.verifiers import RemoteBrowserIdVerifier, IBrowserIdVerifier
from browserid.tests.support import make_assertion

            response.__dict__.update(response_attrs)
            return response
        orig_post_method = verifier.session.post
        verifier.session.post = replacement_post_method
        try:
            yield None
        finally:
            verifier.session.post = orig_post_method

    def test_verifier_config_loading_defaults(self):
        config = self._make_config()
        verifier = config.registry.getUtility(IBrowserIdVerifier)
        self.assertTrue(isinstance(verifier, RemoteBrowserIdVerifier))
        self.assertEquals(verifier.verifier_url,
                          "https://verifier.accounts.firefox.com/v2")
        self.assertEquals(verifier.audiences, None)
        self.assertEquals(verifier.trusted_issuers, None)
        self.assertEquals(verifier.allowed_issuers, None)

    def test_verifier_config_loading_values(self):
        config = self._make_config({  # noqa; indentation below is non-standard
            "browserid.verifier_url":
                "https://trustyverifier.notascam.com/endpoint/path",
            "browserid.audiences":
                "https://testmytoken.com",
            "browserid.trusted_issuers":
                "example.com trustyidp.org",
            "browserid.allowed_issuers":
                "example.com trustyidp.org\nmockmyid.com",
        })

        verifier = config.registry.getUtility(IBrowserIdVerifier)