1# Copyright 2020 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 json 16import pytest 17 18import google.oauth2.credentials 19from google.oauth2 import service_account 20import google.auth.impersonated_credentials 21from google.auth import _helpers 22 23 24GOOGLE_OAUTH2_TOKEN_ENDPOINT = "https://oauth2.googleapis.com/token" 25 26 27@pytest.fixture 28def service_account_credentials(service_account_file): 29 yield service_account.Credentials.from_service_account_file(service_account_file) 30 31 32@pytest.fixture 33def impersonated_service_account_credentials(impersonated_service_account_file): 34 yield service_account.Credentials.from_service_account_file( 35 impersonated_service_account_file 36 ) 37 38 39def test_refresh_with_user_credentials_as_source( 40 authorized_user_file, 41 impersonated_service_account_credentials, 42 http_request, 43 token_info, 44): 45 with open(authorized_user_file, "r") as fh: 46 info = json.load(fh) 47 48 source_credentials = google.oauth2.credentials.Credentials( 49 None, 50 refresh_token=info["refresh_token"], 51 token_uri=GOOGLE_OAUTH2_TOKEN_ENDPOINT, 52 client_id=info["client_id"], 53 client_secret=info["client_secret"], 54 # The source credential needs this scope for the generateAccessToken request 55 # The user must also have `Service Account Token Creator` on the project 56 # that owns the impersonated service account. 57 # See https://cloud.google.com/iam/docs/creating-short-lived-service-account-credentials 58 scopes=["https://www.googleapis.com/auth/cloud-platform"], 59 ) 60 61 source_credentials.refresh(http_request) 62 63 target_scopes = [ 64 "https://www.googleapis.com/auth/devstorage.read_only", 65 "https://www.googleapis.com/auth/analytics", 66 ] 67 target_credentials = google.auth.impersonated_credentials.Credentials( 68 source_credentials=source_credentials, 69 target_principal=impersonated_service_account_credentials.service_account_email, 70 target_scopes=target_scopes, 71 lifetime=100, 72 ) 73 74 target_credentials.refresh(http_request) 75 assert target_credentials.token 76 77 78def test_refresh_with_service_account_credentials_as_source( 79 http_request, 80 service_account_credentials, 81 impersonated_service_account_credentials, 82 token_info, 83): 84 source_credentials = service_account_credentials.with_scopes(["email"]) 85 source_credentials.refresh(http_request) 86 assert source_credentials.token 87 88 target_scopes = [ 89 "https://www.googleapis.com/auth/devstorage.read_only", 90 "https://www.googleapis.com/auth/analytics", 91 ] 92 target_credentials = google.auth.impersonated_credentials.Credentials( 93 source_credentials=source_credentials, 94 target_principal=impersonated_service_account_credentials.service_account_email, 95 target_scopes=target_scopes, 96 ) 97 98 target_credentials.refresh(http_request) 99 assert target_credentials.token 100