def _get_cached_auth_token(self, client, username, password):
        if not os.path.isdir(ST2_CONFIG_DIRECTORY):
            os.makedirs(ST2_CONFIG_DIRECTORY)

        cached_token_path = self._get_cached_token_path_for_user(username=username)
        if not os.path.isfile(cached_token_path):
            return None

        if not os.access(ST2_CONFIG_DIRECTORY, os.R_OK):
            # We don't have read access to the file with a cached token
            message = ('Unable to retrieve cached token from "%s" (user %s doesn\'t have read '
                       'access to the parent directory). Subsequent requests won\'t use a '
                       'cached token meaning they may be slower.' % (cached_token_path,
                                                                     os.getlogin()))
            LOG.warn(message)
            return None

        if not os.access(cached_token_path, os.R_OK):
            # We don't have read access to the file with a cached token
            message = ('Unable to retrieve cached token from "%s" (user %s doesn\'t have read '
                       'access to this file). Subsequent requests won\'t use a cached token '
                       'meaning they may be slower.' % (cached_token_path, os.getlogin()))
            LOG.warn(message)
            return None

        with open(cached_token_path) as fp:
            data = fp.read()

        try:
            data = json.loads(data)

            token = data['token']
            expire_timestamp = data['expire_timestamp']
        except Exception as e:
            msg = ('File "%s" with cached token is corrupted or invalid (%s). Please delete '
                   ' this file' % (cached_token_path, str(e)))
            raise ValueError(msg)

        now = int(time.time())
        if (expire_timestamp - TOKEN_EXPIRATION_GRACE_PERIOD_SECONDS) < now:
            LOG.debug('Cached token from file "%s" has expired' % (cached_token_path))
            # Token has expired
            return None

        LOG.debug('Using cached token from file "%s"' % (cached_token_path))
        return token
Save changes made to any of the translations in this resource
dataset/ETHPy150Open StackStorm/st2/st2client/st2client/shell.py/Shell._get_cached_auth_token--crossover with--dataset/ETHPy150Open mozilla/pontoon/pontoon/sync/vcs/models.py/VCSResource.save