service_account.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:metrics 作者: Jeremy-Friedman 项目源码 文件源码
def authorize(self, http):
        """Authorize an httplib2.Http instance with a JWT assertion.

        Unless specified, the 'aud' of the assertion will be the base
        uri of the request.

        Args:
            http: An instance of ``httplib2.Http`` or something that acts
                  like it.
        Returns:
            A modified instance of http that was passed in.
        Example::
            h = httplib2.Http()
            h = credentials.authorize(h)
        """
        request_orig = http.request
        request_auth = super(_JWTAccessCredentials, self).authorize(http).request

        # The closure that will replace 'httplib2.Http.request'.
        def new_request(uri, method='GET', body=None, headers=None,
                        redirections=httplib2.DEFAULT_MAX_REDIRECTS,
                        connection_type=None):
            if 'aud' in self._kwargs:
                # Preemptively refresh token, this is not done for OAuth2
                if self.access_token is None or self.access_token_expired:
                    self.refresh(None)
                return request_auth(uri, method, body,
                                    headers, redirections,
                                    connection_type)
            else:
                # If we don't have an 'aud' (audience) claim,
                # create a 1-time token with the uri root as the audience
                headers = _initialize_headers(headers)
                _apply_user_agent(headers, self.user_agent)
                uri_root = uri.split('?', 1)[0]
                token, unused_expiry = self._create_token({'aud': uri_root})

                headers['Authorization'] = 'Bearer ' + token
                return request_orig(uri, method, body,
                                    clean_headers(headers),
                                    redirections, connection_type)

        # Replace the request method with our own closure.
        http.request = new_request

        return http
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号