예제 #1
0
    def __init__(self,
                 provider,
                 client_id,
                 client_secret,
                 url_opener=None,
                 proxy=None,
                 access_token_cache=None,
                 datetime_strategy=datetime.datetime):
        """Creates an OAuth2Client.

    Args:
      provider: The OAuth2Provider provider this client will authenticate
          against.
      client_id: The OAuth2 client ID of this client.
      client_secret: The OAuth2 client secret of this client.
      url_opener: An optinal urllib2.OpenerDirector to use for making HTTP
          requests to the OAuth2 provider's token endpoint.  The provided
          url_opener *must* be configured to validate server SSL certificates
          for requests to https connections, and to correctly handle proxying of
          https requests.  If this argument is omitted or None, a suitable
          opener based on fancy_urllib is used.
      proxy: An optional string specifying a HTTP proxy to be used, in the form
          '<proxy>:<port>'.  This option is only effective if the url_opener has
          been configured with a fancy_urllib.FancyProxyHandler (this is the
          case for the default url_opener).
      access_token_cache: An optional instance of a TokenCache. If omitted or
          None, an InMemoryTokenCache is used.
      datetime_strategy: datetime module strategy to use.
    """
        self.provider = provider
        self.client_id = client_id
        self.client_secret = client_secret
        # datetime_strategy is used to invoke utcnow() on; it is injected into the
        # constructor for unit testing purposes.
        self.datetime_strategy = datetime_strategy
        self._proxy = proxy

        self.access_token_cache = access_token_cache or InMemoryTokenCache()

        self.ca_certs_file = os.path.join(
            os.path.dirname(os.path.abspath(cacerts.__file__)), 'cacerts.txt')

        if url_opener is None:
            # Check that the cert file distributed with boto has not been tampered
            # with.
            h = sha1()
            h.update(file(self.ca_certs_file).read())
            actual_sha1 = h.hexdigest()
            if actual_sha1 != CACERTS_FILE_SHA1SUM:
                raise Error(
                    'CA certificates file does not have expected SHA1 sum; '
                    'expected: %s, actual: %s' %
                    (CACERTS_FILE_SHA1SUM, actual_sha1))
            # TODO(Google): set user agent?
            url_opener = urllib2.build_opener(
                fancy_urllib.FancyProxyHandler(),
                fancy_urllib.FancyRedirectHandler(),
                fancy_urllib.FancyHTTPSHandler())
        self.url_opener = url_opener
예제 #2
0
def UrlOpen(url):
    request = fancy_urllib.FancyRequest(url)
    ca_certs = os.path.join(SCRIPT_DIR, 'cacerts.txt')
    request.set_ssl_info(ca_certs=ca_certs)
    url_opener = urllib2.build_opener(fancy_urllib.FancyProxyHandler(),
                                      fancy_urllib.FancyRedirectHandler(),
                                      fancy_urllib.FancyHTTPSHandler())
    return url_opener.open(request)