Example #1
0
    def send_files(self, name, file_paths):
        """ Sends multiple files.

        :param str|unicode name:                Name for the gist/paste.
        :param list[str|unicode] file_paths:    List of file paths.

        :return: The result of the upload.
        :rtype: any

        """

        if self.__mode != "gist":
            raise ValueError("Invalid mode for multiple files")

        params = {
            "description": name,
            "public": False,
            "files": {
                # name: {
                #     "content": code
                # }
            }
        }

        for file_path in file_paths:
            if not os.path.isfile(file_path):
                continue
            code = self.__read_file_bytes(file_path)
            file_name = os.path.split(file_path)
            params["files"][file_name[-1]] = {"content": code}

        headers = {
            "Content-Type": "application/json"
        }
        post_data = JsonHelper.dump(params, pretty_print=False)
        data = UriHandler.open("https://api.github.com/gists", params=post_data,
                               proxy=self.__proxy, additional_headers=headers)
        if not data:
            raise IOError("Error posting Gist to GitHub")

        json_data = JsonHelper(data)
        url = json_data.get_value("html_url")
        if self.__logger:
            self.__logger.info("Gist: %s", url)

        # minify with google
        # POST https://www.googleapis.com/urlshortener/v1/url
        # Content-Type: application/json
        shortener = {"longUrl": url}
        google = "https://www.googleapis.com/urlshortener/v1/url?key=%s" % (self.__apiKey,)
        google_data = UriHandler.open(google, params=JsonHelper.dump(shortener, False),
                                      proxy=self.__proxy,
                                      additional_headers={"Content-Type": "application/json"})

        google_url = JsonHelper(google_data).get_value("id")
        if self.__logger:
            self.__logger.info("Goo.gl: %s", google_url)
        return google_url
    def authenticate(self, username, password):
        # Step 1: First initiate an authentication request
        auth_request = self.__get_authentication_request(username)
        auth_data = JsonHelper.dump(auth_request)
        auth_headers = {
            "X-Amz-Target": "AWSCognitoIdentityProviderService.InitiateAuth",
            "Accept-Encoding": "identity",
            "Content-Type": "application/x-amz-json-1.1"
        }
        auth_response = UriHandler.open(self.url,
                                        proxy=self.__proxy,
                                        params=auth_data,
                                        additional_headers=auth_headers)
        auth_response_json = JsonHelper(auth_response)
        challenge_parameters = auth_response_json.get_value(
            "ChallengeParameters")
        if self.__logger:
            self.__logger.trace(challenge_parameters)

        challenge_name = auth_response_json.get_value("ChallengeName")
        if not challenge_name == "PASSWORD_VERIFIER":
            if self.__logger:
                self.__logger.error("Cannot start authentication challenge")
            return None

        # Step 2: Respond to the Challenge with a valid ChallengeResponse
        challenge_request = self.__get_challenge_response_request(
            challenge_parameters, password)
        challenge_data = JsonHelper.dump(challenge_request)
        challenge_headers = {
            "X-Amz-Target":
            "AWSCognitoIdentityProviderService.RespondToAuthChallenge",
            "Content-Type": "application/x-amz-json-1.1"
        }
        auth_response = UriHandler.open(self.url,
                                        proxy=self.__proxy,
                                        params=challenge_data,
                                        additional_headers=challenge_headers)

        auth_response_json = JsonHelper(auth_response)
        if "message" in auth_response_json.json:
            self.__logger.error("Error logging in: %s",
                                auth_response_json.get_value("message"))
            return None, None

        id_token = auth_response_json.get_value("AuthenticationResult",
                                                "IdToken")
        refresh_token = auth_response_json.get_value("AuthenticationResult",
                                                     "RefreshToken")
        return id_token, refresh_token
Example #3
0
    def __init__(self, channel, settings_store, logger=None):
        """ Creates a Cloaker object that helps with cloaking objects

        :param ChannelInfo channel:             The ChannelInfo of the channel for which we need
                                                cloak information.
        :param SettingsStore settings_store:    The settings store to use for retrieving the
                                                settings data.
        :param any logger:                      A Logger object for logging purposes.

        """

        self.__logger = logger
        self.__channel = channel
        self.__channelId = channel.guid
        self.__settingsStore = settings_store

        if self.__logger:
            self.__logger.debug("Setting up a Cloaker based on '%s'",
                                self.__settingsStore)

        # Create a new file if none existed
        self.__cloaked = self.__settingsStore.get_setting("cloaked",
                                                          channel=channel,
                                                          default=None)
        if self.__cloaked is None:
            self.__cloaked = {}
            self.__store(False)

        if self.__logger:
            self.__logger.trace(
                "Found cloaked data:\n%s",
                JsonHelper.dump(self.__cloaked, pretty_print=True))
    def renew_token(self, refresh_token):
        """
        Sets a new access token on the User using the refresh token. The basic expire time of the
        refresh token is 30 days:

        http://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-with-identity-providers.html

        :param str refresh_token:   Token to use for refreshing the authorization token.

        """

        refresh_request = {
            "AuthParameters": {
                "REFRESH_TOKEN": refresh_token
            },
            "ClientId": self.client_id,
            "AuthFlow": "REFRESH_TOKEN"
        }
        refresh_headers = {
            "X-Amz-Target": "AWSCognitoIdentityProviderService.InitiateAuth",
            "Content-Type": "application/x-amz-json-1.1"
        }
        refresh_request_data = JsonHelper.dump(refresh_request)
        refresh_response = UriHandler.open(self.url,
                                           proxy=self.__proxy,
                                           params=refresh_request_data,
                                           additional_headers=refresh_headers)
        refresh_json = JsonHelper(refresh_response)
        id_token = refresh_json.get_value("AuthenticationResult", "IdToken")
        return id_token
Example #5
0
    def __send_git_hub_gist(self, name, code):
        """ Send a file to a Github gist.

        :param str|unicode name:            Name of the logfile paste/gist.
        :param str code:                    The content to post.

        :return: the ID of the gist
        :rtype: int

        """

        params = {
            "description": name,
            "public": False,
            "files": {
                name: {
                    "content": code
                }
            }
        }
        headers = {"Content-Type": "application/json"}
        post_data = JsonHelper.dump(params, pretty_print=False)
        data = UriHandler.open("https://api.github.com/gists",
                               params=post_data.encode(),
                               proxy=self.__proxy,
                               additional_headers=headers)
        if not data:
            raise IOError("Error posting Gist to GitHub")

        json_data = JsonHelper(data)
        url = json_data.get_value("html_url")
        if self.__logger:
            self.__logger.info("Gist: %s", url)

        # minify with google
        # POST https://www.googleapis.com/urlshortener/v1/url
        # Content-Type: application/json
        shortener = {"longUrl": url}
        google = "https://www.googleapis.com/urlshortener/v1/url?key=%s" % (
            self.__apiKey, )
        google_data = UriHandler.open(
            google,
            params=JsonHelper.dump(shortener, False),
            proxy=self.__proxy,
            additional_headers={"Content-Type": "application/json"})

        return JsonHelper(google_data).get_value("id")
Example #6
0
    def __rebuild_index(self):
        """ Rebuilds the channel index that contains all channels and performs all necessary steps:

        1. Find all channel add-on paths and determine the version of the channel add-on
        2. For all channel sets in the add-on:
            a. See if it is a new channel set (pyo and pyc check)
            b. If so, initialise the channel set and then perform the first time actions on
               the included channels.
            c. Add all channels within the channel set to the channelIndex

        Remark: this method only generates the index of the channels, it does not import at all!

        :return: The current channel index.
        :rtype: dict

        """

        if self.__reindexed:
            Logger.error("Channel index was already re-indexed this run. Not doing it again.")
            return self.__channelIndex

        Logger.info("Rebuilding the channel index.")
        index = {
            self.__CHANNEL_INDEX_ADD_ONS_KEY: [],
            self.__CHANNEL_INDEX_CHANNEL_KEY: {}
        }

        # iterate all Retrospect Channel Packages
        channel_pack_base, channel_pack_folders = self.__get_channel_pack_folders()
        for channel_pack_name in channel_pack_folders:
            index[self.__CHANNEL_INDEX_ADD_ONS_KEY].append(channel_pack_name)

            channel_package_path = os.path.join(channel_pack_base, channel_pack_name)
            channel_package_id, channel_add_on_version = self.__validate_and_get_add_on_version(channel_package_path)
            if channel_package_id is None:
                continue

            channel_sets = os.listdir(channel_package_path)
            for channel_set in channel_sets:
                if not os.path.isdir(os.path.join(channel_package_path, channel_set)):
                    continue

                channel_set_id = "chn_%s" % (channel_set,)
                Logger.debug("Found channel set '%s'", channel_set_id)
                index[self.__CHANNEL_INDEX_CHANNEL_KEY][channel_set_id] = {
                    self.__CHANNEL_INDEX_CHANNEL_VERSION_KEY: str(channel_add_on_version),
                    self.__CHANNEL_INDEX_CHANNEL_INFO_KEY: os.path.join(channel_package_path, channel_set, "%s.json" % (channel_set_id,))
                }

        with io.open(self.__CHANNEL_INDEX, 'wt+', encoding='utf-8') as f:
            f.write(JsonHelper.dump(index))

        # now we marked that we already re-indexed.
        self.__reindexed = True
        self.__channelIndex = index
        Logger.info("Rebuilding channel index completed with %d channelSets and %d add-ons: %s.",
                    len(index[self.__CHANNEL_INDEX_CHANNEL_KEY]),
                    len(index[self.__CHANNEL_INDEX_ADD_ONS_KEY]),
                    index)

        return index