• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2021 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import google.auth
16import google.auth.credentials
17import google.auth.transport.requests
18from google.oauth2 import service_account
19
20
21def test_authorized_session_with_service_account_and_self_signed_jwt():
22    credentials, project_id = google.auth.default()
23
24    credentials = credentials.with_scopes(
25        scopes=[],
26        default_scopes=["https://www.googleapis.com/auth/pubsub"],
27    )
28
29    session = google.auth.transport.requests.AuthorizedSession(
30        credentials=credentials, default_host="pubsub.googleapis.com"
31    )
32
33    # List Pub/Sub Topics through the REST API
34    # https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.topics/list
35    url = "https://pubsub.googleapis.com/v1/projects/{}/topics".format(project_id)
36    with session:
37        response = session.get(url)
38        response.raise_for_status()
39
40    # Check that self-signed JWT was created and is being used
41    assert credentials._jwt_credentials is not None
42    assert credentials._jwt_credentials.token == credentials.token
43