예제 #1
0
    def get_bucket_name_and_prefix(self, product, url=None):
        """Extract bucket name and prefix from product URL

        :param product: The EO product to download
        :type product: :class:`~eodag.api.product.EOProduct`
        :param url: URL to use as product.location
        :type url: str
        :return: bucket_name and prefix as str
        :rtype: tuple
        """
        if not url:
            url = product.location
        bucket, prefix = None, None
        # Assume the bucket is encoded into the product location as a URL or given as the 'default_bucket' config
        # param
        scheme, netloc, path, params, query, fragment = urlparse(url)
        if not scheme or scheme == "s3":
            bucket = (netloc
                      if netloc else getattr(self.config, "products", {}).get(
                          product.product_type, {}).get("default_bucket", ""))
            prefix = path.strip("/")
        elif scheme in ("http", "https", "ftp"):
            parts = path.split("/")
            bucket, prefix = parts[1], "/{}".format("/".join(parts[2:]))
        return bucket, prefix
예제 #2
0
 def __call__(self, request):
     """Perform the actual authentication"""
     if self.where == "qs":
         parts = urlparse(request.url)
         qs = parse_qs(parts.query)
         qs[self.key] = self.token
         request.url = urlunparse((
             parts.scheme,
             parts.netloc,
             parts.path,
             parts.params,
             urlencode(qs),
             parts.fragment,
         ))
     elif self.where == "header":
         request.headers["Authorization"] = "Bearer {}".format(self.token)
     return request
예제 #3
0
 def exchange_code_for_token(self, authorized_url, state):
     """Get exchange code for token"""
     qs = parse_qs(urlparse(authorized_url).query)
     if qs["state"][0] != state:
         raise AuthenticationError(
             "The state received in the authorized url does not match initially computed state"
         )
     code = qs["code"][0]
     token_exchange_data = {
         "redirect_uri": self.config.redirect_uri,
         "client_id": self.config.client_id,
         "code": code,
         "state": state,
     }
     # If necessary, change the keys of the form data that will be passed to the token exchange POST request
     custom_token_exchange_params = getattr(self.config,
                                            "token_exchange_params", {})
     if custom_token_exchange_params:
         token_exchange_data[custom_token_exchange_params[
             "redirect_uri"]] = token_exchange_data.pop("redirect_uri")
         token_exchange_data[custom_token_exchange_params[
             "client_id"]] = token_exchange_data.pop("client_id")
     # If the client_secret is known, the token exchange request must be authenticated with a BASIC Auth, using the
     # client_id and client_secret as username and password respectively
     if getattr(self.config, "client_secret", None):
         token_exchange_data.update({
             "auth": (self.config.client_id, self.config.client_secret),
             "grant_type":
             "authorization_code",
             "client_secret":
             self.config.client_secret,
         })
     post_request_kwargs = {
         self.config.token_exchange_post_data_method: token_exchange_data
     }
     r = self.session.post(self.config.token_uri, **post_request_kwargs)
     return r.json()[self.config.token_key]