123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- # Copyright 2014 Google Inc.
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- import json
- import os
- import mock
- import pytest
- from google.auth import environment_vars
- from google.auth import exceptions
- from google.auth import transport
- import google.auth.compute_engine._metadata
- from google.oauth2 import id_token
- from google.oauth2 import service_account
- import yatest.common
- SERVICE_ACCOUNT_FILE = os.path.join(
- yatest.common.test_source_path(), "data/service_account.json"
- )
- def make_request(status, data=None):
- response = mock.create_autospec(transport.Response, instance=True)
- response.status = status
- if data is not None:
- response.data = json.dumps(data).encode("utf-8")
- request = mock.create_autospec(transport.Request)
- request.return_value = response
- return request
- def test__fetch_certs_success():
- certs = {"1": "cert"}
- request = make_request(200, certs)
- returned_certs = id_token._fetch_certs(request, mock.sentinel.cert_url)
- request.assert_called_once_with(mock.sentinel.cert_url, method="GET")
- assert returned_certs == certs
- def test__fetch_certs_failure():
- request = make_request(404)
- with pytest.raises(exceptions.TransportError):
- id_token._fetch_certs(request, mock.sentinel.cert_url)
- request.assert_called_once_with(mock.sentinel.cert_url, method="GET")
- @mock.patch("google.auth.jwt.decode", autospec=True)
- @mock.patch("google.oauth2.id_token._fetch_certs", autospec=True)
- def test_verify_token(_fetch_certs, decode):
- result = id_token.verify_token(mock.sentinel.token, mock.sentinel.request)
- assert result == decode.return_value
- _fetch_certs.assert_called_once_with(
- mock.sentinel.request, id_token._GOOGLE_OAUTH2_CERTS_URL
- )
- decode.assert_called_once_with(
- mock.sentinel.token, certs=_fetch_certs.return_value, audience=None
- )
- @mock.patch("google.auth.jwt.decode", autospec=True)
- @mock.patch("google.oauth2.id_token._fetch_certs", autospec=True)
- def test_verify_token_args(_fetch_certs, decode):
- result = id_token.verify_token(
- mock.sentinel.token,
- mock.sentinel.request,
- audience=mock.sentinel.audience,
- certs_url=mock.sentinel.certs_url,
- )
- assert result == decode.return_value
- _fetch_certs.assert_called_once_with(mock.sentinel.request, mock.sentinel.certs_url)
- decode.assert_called_once_with(
- mock.sentinel.token,
- certs=_fetch_certs.return_value,
- audience=mock.sentinel.audience,
- )
- @mock.patch("google.oauth2.id_token.verify_token", autospec=True)
- def test_verify_oauth2_token(verify_token):
- verify_token.return_value = {"iss": "accounts.google.com"}
- result = id_token.verify_oauth2_token(
- mock.sentinel.token, mock.sentinel.request, audience=mock.sentinel.audience
- )
- assert result == verify_token.return_value
- verify_token.assert_called_once_with(
- mock.sentinel.token,
- mock.sentinel.request,
- audience=mock.sentinel.audience,
- certs_url=id_token._GOOGLE_OAUTH2_CERTS_URL,
- )
- @mock.patch("google.oauth2.id_token.verify_token", autospec=True)
- def test_verify_oauth2_token_invalid_iss(verify_token):
- verify_token.return_value = {"iss": "invalid_issuer"}
- with pytest.raises(exceptions.GoogleAuthError):
- id_token.verify_oauth2_token(
- mock.sentinel.token, mock.sentinel.request, audience=mock.sentinel.audience
- )
- @mock.patch("google.oauth2.id_token.verify_token", autospec=True)
- def test_verify_firebase_token(verify_token):
- result = id_token.verify_firebase_token(
- mock.sentinel.token, mock.sentinel.request, audience=mock.sentinel.audience
- )
- assert result == verify_token.return_value
- verify_token.assert_called_once_with(
- mock.sentinel.token,
- mock.sentinel.request,
- audience=mock.sentinel.audience,
- certs_url=id_token._GOOGLE_APIS_CERTS_URL,
- )
- def test_fetch_id_token_from_metadata_server(monkeypatch):
- monkeypatch.delenv(environment_vars.CREDENTIALS, raising=False)
- def mock_init(self, request, audience, use_metadata_identity_endpoint):
- assert use_metadata_identity_endpoint
- self.token = "id_token"
- with mock.patch("google.auth.compute_engine._metadata.ping", return_value=True):
- with mock.patch.multiple(
- google.auth.compute_engine.IDTokenCredentials,
- __init__=mock_init,
- refresh=mock.Mock(),
- ):
- request = mock.Mock()
- token = id_token.fetch_id_token(request, "https://pubsub.googleapis.com")
- assert token == "id_token"
- def test_fetch_id_token_from_explicit_cred_json_file(monkeypatch):
- monkeypatch.setenv(environment_vars.CREDENTIALS, SERVICE_ACCOUNT_FILE)
- def mock_refresh(self, request):
- self.token = "id_token"
- with mock.patch.object(service_account.IDTokenCredentials, "refresh", mock_refresh):
- request = mock.Mock()
- token = id_token.fetch_id_token(request, "https://pubsub.googleapis.com")
- assert token == "id_token"
- def test_fetch_id_token_no_cred_exists(monkeypatch):
- monkeypatch.delenv(environment_vars.CREDENTIALS, raising=False)
- with mock.patch(
- "google.auth.compute_engine._metadata.ping",
- side_effect=exceptions.TransportError(),
- ):
- with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
- request = mock.Mock()
- id_token.fetch_id_token(request, "https://pubsub.googleapis.com")
- assert excinfo.match(
- r"Neither metadata server or valid service account credentials are found."
- )
- with mock.patch("google.auth.compute_engine._metadata.ping", return_value=False):
- with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
- request = mock.Mock()
- id_token.fetch_id_token(request, "https://pubsub.googleapis.com")
- assert excinfo.match(
- r"Neither metadata server or valid service account credentials are found."
- )
- def test_fetch_id_token_invalid_cred_file_type(monkeypatch):
- user_credentials_file = os.path.join(
- yatest.common.test_source_path(), "data/authorized_user.json"
- )
- monkeypatch.setenv(environment_vars.CREDENTIALS, user_credentials_file)
- with mock.patch("google.auth.compute_engine._metadata.ping", return_value=False):
- with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
- request = mock.Mock()
- id_token.fetch_id_token(request, "https://pubsub.googleapis.com")
- assert excinfo.match(
- r"Neither metadata server or valid service account credentials are found."
- )
- def test_fetch_id_token_invalid_json(monkeypatch):
- not_json_file = os.path.join(yatest.common.test_source_path(), "data/public_cert.pem")
- monkeypatch.setenv(environment_vars.CREDENTIALS, not_json_file)
- with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
- request = mock.Mock()
- id_token.fetch_id_token(request, "https://pubsub.googleapis.com")
- assert excinfo.match(
- r"GOOGLE_APPLICATION_CREDENTIALS is not valid service account credentials."
- )
- def test_fetch_id_token_invalid_cred_path(monkeypatch):
- not_json_file = os.path.join(yatest.common.test_source_path(), "data/not_exists.json")
- monkeypatch.setenv(environment_vars.CREDENTIALS, not_json_file)
- with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
- request = mock.Mock()
- id_token.fetch_id_token(request, "https://pubsub.googleapis.com")
- assert excinfo.match(
- r"GOOGLE_APPLICATION_CREDENTIALS path is either not found or invalid."
- )
|