Esempio n. 1
0
    def __init__(self, access_token=None, expires_at=None, on_refresh=None):
        logger.info(("Setting up a RenewingAuthorizer. It will use an "
                     "auth type of Bearer and can handle 401s."))
        if access_token is not None and expires_at is None:
            logger.warning(
                ("Initializing a RenewingAuthorizer with an "
                 "access_token and no expires_at time means that this "
                 "access_token will be discarded. You should either pass "
                 "expires_at or not pass an access_token at all"))
            # coerce to None for simplicity / consistency
            access_token = None

        self.access_token = access_token
        self.expires_at = None
        self.on_refresh = on_refresh

        # check access_token too -- it's not clear what it would mean to set
        # expiration without an access token
        if expires_at is not None and self.access_token is not None:
            self.access_token_hash = sha256_string(self.access_token)
            logger.info(
                ("Got both expires_at and access_token. "
                 "Will start by using "
                 'RenewingAuthorizer.access_token with hash "{}"').format(
                     self.access_token_hash))
            self._set_expiration_time(expires_at)

        # if these were unspecified, fetch a new access token
        if self.access_token is None and self.expires_at is None:
            logger.info("Creating RenewingAuthorizer without Access "
                        "Token. Fetching initial token now.")
            self._get_new_access_token()
Esempio n. 2
0
    def __init__(self, access_token):
        logger.info(("Setting up an AccessTokenAuthorizer. It will use an "
                     "auth type of Bearer and cannot handle 401s."))
        self.access_token = access_token
        self.header_val = "Bearer %s" % access_token

        self.access_token_hash = sha256_string(self.access_token)
        logger.debug('Bearer token has hash "{}"'.format(
            self.access_token_hash))
Esempio n. 3
0
    def __init__(self, access_token):
        logger.info(
            (
                "Setting up an AccessTokenAuthorizer. It will use an "
                "auth type of Bearer and cannot handle 401s."
            )
        )
        self.access_token = access_token
        self.header_val = "Bearer %s" % access_token

        self.access_token_hash = sha256_string(self.access_token)
        logger.debug('Bearer token has hash "{}"'.format(self.access_token_hash))
Esempio n. 4
0
    def __init__(self, access_token=None, expires_at=None, on_refresh=None):
        logger.info(
            (
                "Setting up a RenewingAuthorizer. It will use an "
                "auth type of Bearer and can handle 401s."
            )
        )
        if access_token is not None and expires_at is None:
            logger.warning(
                (
                    "Initializing a RenewingAuthorizer with an "
                    "access_token and no expires_at time means that this "
                    "access_token will be discarded. You should either pass "
                    "expires_at or not pass an access_token at all"
                )
            )
            # coerce to None for simplicity / consistency
            access_token = None

        self.access_token = access_token
        self.expires_at = None
        self.on_refresh = on_refresh

        # check access_token too -- it's not clear what it would mean to set
        # expiration without an access token
        if expires_at is not None and self.access_token is not None:
            self.access_token_hash = sha256_string(self.access_token)
            logger.info(
                (
                    "Got both expires_at and access_token. "
                    "Will start by using "
                    'RenewingAuthorizer.access_token with hash "{}"'
                ).format(self.access_token_hash)
            )
            self._set_expiration_time(expires_at)

        # if these were unspecified, fetch a new access token
        if self.access_token is None and self.expires_at is None:
            logger.info(
                "Creating RenewingAuthorizer without Access "
                "Token. Fetching initial token now."
            )
            self._get_new_access_token()
Esempio n. 5
0
    def _get_new_access_token(self):
        """
        Given token data from _get_token_response and _extract_token_data,
        set the access token and expiration time, calculate the new token
        hash, and call on_refresh
        """
        # get the first (and only) token
        res = self._get_token_response()
        token_data = self._extract_token_data(res)

        self._set_expiration_time(token_data["expires_at_seconds"])
        self.access_token = token_data["access_token"]
        self.access_token_hash = sha256_string(self.access_token)

        logger.info(("RenewingAuthorizer.access_token updated to token "
                     "with hash"
                     '"{}"').format(self.access_token_hash))

        if callable(self.on_refresh):
            self.on_refresh(res)
            logger.debug("Invoked on_refresh callback")
Esempio n. 6
0
    def _get_new_access_token(self):
        """
        Given token data from _get_token_response and _extract_token_data,
        set the access token and expiration time, calculate the new token
        hash, and call on_refresh
        """
        # get the first (and only) token
        res = self._get_token_response()
        token_data = self._extract_token_data(res)

        self._set_expiration_time(token_data["expires_at_seconds"])
        self.access_token = token_data["access_token"]
        self.access_token_hash = sha256_string(self.access_token)

        logger.info(
            (
                "RenewingAuthorizer.access_token updated to token " "with hash" '"{}"'
            ).format(self.access_token_hash)
        )

        if callable(self.on_refresh):
            self.on_refresh(res)
            logger.debug("Invoked on_refresh callback")