Example #1
0
    def __init__(self, host: str, port: str, user: str, secret: str,
                 verify_ssl: bool) -> None:
        self._host = host
        self._token_file = "%s/%s" % (str(
            self._token_dir), self._token_file_suffix % self._host)
        self._port = port
        self._user = user
        self._secret = secret
        self._verify_ssl = verify_ssl
        if not verify_ssl:
            urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

        # We need to use LegacyClient due to grant_type==password
        self._client = LegacyApplicationClient(None)
        self._client.prepare_request_body(username=self._user,
                                          password=self._secret)

        # Check if token file exists, read it & create OAuthSession with it
        try:
            self._json_token = self.load_token_file_and_update_expire_in()
            LOGGER.debug("Loaded token content: %s", self._json_token)

            self._oauth_session = OAuth2Session(
                self._user,
                client=self._client,
                auto_refresh_url="https://%s:%s%s" %
                (self._host, self._port, self._refresh_endpoint),
                token_updater=lambda x: self.
                store_token_file_and_update_expires_in_abs(to_token_dict(x)),
                token={
                    "access_token": self._json_token["access_token"],
                    "refresh_token": self._json_token["refresh_token"],
                    "expires_in": self._json_token["expires_in"],
                },
            )
        except (FileNotFoundError, KeyError):
            LOGGER.debug(
                "Token file not found or error in token file. Creating new connection."
            )
            self._oauth_session = OAuth2Session(
                self._user,
                client=self._client,
                auto_refresh_url="https://%s:%s%s" %
                (self._host, self._port, self._refresh_endpoint),
                token_updater=lambda x: self.
                store_token_file_and_update_expires_in_abs(to_token_dict(x)),
            )
            # Fetch token
            token_dict = to_token_dict(
                self._oauth_session.fetch_token(
                    token_url="https://%s:%s%s" %
                    (self._host, self._port, self._token_endpoint),
                    username=self._user,
                    password=self._secret,
                    verify=self._verify_ssl,
                ))
            # Initially create the token file
            self.store_token_file_and_update_expires_in_abs(token_dict)
            self._json_token = token_dict
Example #2
0
    def load_token_file_and_update_expire_in(self) -> TokenDict:
        with open(self._token_file, "r") as token_file:
            token_json = json.load(token_file)

            # Update expires_in from expires_in_abs
            expires_in_abs = token_json["expires_in_abs"]
            expires_in_updated = (dt.datetime.strptime(
                expires_in_abs,
                self._dt_fmt,
            ) - dt.datetime.now())
            token_json["expires_in"] = math.floor(
                expires_in_updated.total_seconds())
            return to_token_dict(token_json)