Esempio n. 1
0
    def loadToken(self):
        """This function handles loading API Gateway token from cache/storage. Default token load is from local JSON file.
        Override this function with storeToken function to implement secure access token caching mechanism.

        :raises UserWarning: Warning to capture empty JSON
        :return: token dict loaded from default implementation of locally stored JSON file consisting of
            access_token and refresh_token.
        :rtype: dict
        """
        fullName = tokenLocalStoreUtil(self.token_store,
                                       self.central_info["customer_id"],
                                       self.central_info["client_id"])
        token = None
        try:
            with open(fullName, 'r') as fp:
                token = json.load(fp)
            if token:
                self.logger.info("Loaded token from storage from file: " + \
                                 "%s" % str(fullName))
            else:
                raise UserWarning("Empty file!")
        except Exception as err:
            self.logger.error("Unable to load token from storage with error "
                              ".. %s" % str(err))
        return token
Esempio n. 2
0
    def storeToken(self, token):
        """This function handles storage of token for later use. Default storage is unencrypted JSON file and is not secure.
        Override this function and loadToken function to implement secure access token caching mechanism.

        :param token: API Gateway token dict consisting of access_token and refresh_token.
        :type token: dict
        :return: True if the access_token caching is successful.
        :rtype: bool
        """
        fullName = tokenLocalStoreUtil(self.token_store,
                                       self.central_info["customer_id"],
                                       self.central_info["client_id"])
        if not os.path.exists(os.path.dirname(fullName)):
            try:
                os.makedirs(os.path.dirname(fullName))
            except OSError as exc:  # Guard against race condition
                if exc.errno != errno.EEXIST:
                    self.logger.error("Storing token failed with error "
                                      "%s" % str(exc))

        # Dumping data to json file
        try:
            with open(fullName, 'w') as fp:
                json.dump(token, fp, indent=2)
            self.logger.info("Stored Aruba Central token in file " + \
                             "%s" % str(fullName))
            return True
        except Exception as err:
            self.logger.error("Storing token failed with error %s" % str(err))
        return False