C.J. Collier | 37141e4 | 2020-02-13 13:49:49 -0800 | [diff] [blame^] | 1 | # Copyright 2016 Google LLC |
Jon Wayne Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame] | 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 | |
| 15 | import datetime |
| 16 | |
Tres Seaver | b096a3d | 2017-10-30 16:12:37 -0400 | [diff] [blame] | 17 | import pytest |
| 18 | |
Jon Wayne Parrott | e60c124 | 2017-03-23 16:00:24 -0700 | [diff] [blame] | 19 | from google.auth import _helpers |
Jon Wayne Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame] | 20 | from google.auth import credentials |
| 21 | |
| 22 | |
| 23 | class CredentialsImpl(credentials.Credentials): |
| 24 | def refresh(self, request): |
| 25 | self.token = request |
| 26 | |
| 27 | |
| 28 | def test_credentials_constructor(): |
| 29 | credentials = CredentialsImpl() |
| 30 | assert not credentials.token |
| 31 | assert not credentials.expiry |
| 32 | assert not credentials.expired |
| 33 | assert not credentials.valid |
| 34 | |
| 35 | |
| 36 | def test_expired_and_valid(): |
| 37 | credentials = CredentialsImpl() |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 38 | credentials.token = "token" |
Jon Wayne Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame] | 39 | |
| 40 | assert credentials.valid |
| 41 | assert not credentials.expired |
| 42 | |
Jon Wayne Parrott | 7af9f66 | 2017-05-08 09:40:56 -0700 | [diff] [blame] | 43 | # Set the expiration to one second more than now plus the clock skew |
| 44 | # accomodation. These credentials should be valid. |
Jon Wayne Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame] | 45 | credentials.expiry = ( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 46 | datetime.datetime.utcnow() + _helpers.CLOCK_SKEW + datetime.timedelta(seconds=1) |
| 47 | ) |
Jon Wayne Parrott | e60c124 | 2017-03-23 16:00:24 -0700 | [diff] [blame] | 48 | |
| 49 | assert credentials.valid |
| 50 | assert not credentials.expired |
| 51 | |
Jon Wayne Parrott | 7af9f66 | 2017-05-08 09:40:56 -0700 | [diff] [blame] | 52 | # Set the credentials expiration to now. Because of the clock skew |
| 53 | # accomodation, these credentials should report as expired. |
| 54 | credentials.expiry = datetime.datetime.utcnow() |
Jon Wayne Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame] | 55 | |
| 56 | assert not credentials.valid |
| 57 | assert credentials.expired |
| 58 | |
| 59 | |
| 60 | def test_before_request(): |
| 61 | credentials = CredentialsImpl() |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 62 | request = "token" |
Jon Wayne Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame] | 63 | headers = {} |
| 64 | |
| 65 | # First call should call refresh, setting the token. |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 66 | credentials.before_request(request, "http://example.com", "GET", headers) |
Jon Wayne Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame] | 67 | assert credentials.valid |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 68 | assert credentials.token == "token" |
| 69 | assert headers["authorization"] == "Bearer token" |
Jon Wayne Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame] | 70 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 71 | request = "token2" |
Jon Wayne Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame] | 72 | headers = {} |
| 73 | |
| 74 | # Second call shouldn't call refresh. |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 75 | credentials.before_request(request, "http://example.com", "GET", headers) |
Jon Wayne Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame] | 76 | assert credentials.valid |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 77 | assert credentials.token == "token" |
| 78 | assert headers["authorization"] == "Bearer token" |
Jon Wayne Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame] | 79 | |
| 80 | |
Tres Seaver | b096a3d | 2017-10-30 16:12:37 -0400 | [diff] [blame] | 81 | def test_anonymous_credentials_ctor(): |
| 82 | anon = credentials.AnonymousCredentials() |
| 83 | assert anon.token is None |
| 84 | assert anon.expiry is None |
| 85 | assert not anon.expired |
| 86 | assert anon.valid |
| 87 | |
| 88 | |
| 89 | def test_anonymous_credentials_refresh(): |
| 90 | anon = credentials.AnonymousCredentials() |
| 91 | request = object() |
| 92 | with pytest.raises(ValueError): |
| 93 | anon.refresh(request) |
| 94 | |
| 95 | |
| 96 | def test_anonymous_credentials_apply_default(): |
| 97 | anon = credentials.AnonymousCredentials() |
| 98 | headers = {} |
| 99 | anon.apply(headers) |
| 100 | assert headers == {} |
| 101 | with pytest.raises(ValueError): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 102 | anon.apply(headers, token="TOKEN") |
Tres Seaver | b096a3d | 2017-10-30 16:12:37 -0400 | [diff] [blame] | 103 | |
| 104 | |
| 105 | def test_anonymous_credentials_before_request(): |
| 106 | anon = credentials.AnonymousCredentials() |
| 107 | request = object() |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 108 | method = "GET" |
| 109 | url = "https://example.com/api/endpoint" |
Tres Seaver | b096a3d | 2017-10-30 16:12:37 -0400 | [diff] [blame] | 110 | headers = {} |
| 111 | anon.before_request(request, method, url, headers) |
| 112 | assert headers == {} |
| 113 | |
| 114 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 115 | class ReadOnlyScopedCredentialsImpl(credentials.ReadOnlyScoped, CredentialsImpl): |
Jon Wayne Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame] | 116 | @property |
| 117 | def requires_scopes(self): |
Jon Wayne Parrott | 4460a96 | 2017-09-12 10:01:23 -0700 | [diff] [blame] | 118 | return super(ReadOnlyScopedCredentialsImpl, self).requires_scopes |
Jon Wayne Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame] | 119 | |
| 120 | |
Tres Seaver | 4246832 | 2017-09-11 15:36:53 -0400 | [diff] [blame] | 121 | def test_readonly_scoped_credentials_constructor(): |
Jon Wayne Parrott | 4460a96 | 2017-09-12 10:01:23 -0700 | [diff] [blame] | 122 | credentials = ReadOnlyScopedCredentialsImpl() |
Jon Wayne Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame] | 123 | assert credentials._scopes is None |
| 124 | |
| 125 | |
Tres Seaver | 4246832 | 2017-09-11 15:36:53 -0400 | [diff] [blame] | 126 | def test_readonly_scoped_credentials_scopes(): |
Jon Wayne Parrott | 4460a96 | 2017-09-12 10:01:23 -0700 | [diff] [blame] | 127 | credentials = ReadOnlyScopedCredentialsImpl() |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 128 | credentials._scopes = ["one", "two"] |
| 129 | assert credentials.scopes == ["one", "two"] |
| 130 | assert credentials.has_scopes(["one"]) |
| 131 | assert credentials.has_scopes(["two"]) |
| 132 | assert credentials.has_scopes(["one", "two"]) |
| 133 | assert not credentials.has_scopes(["three"]) |
Jon Wayne Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame] | 134 | |
| 135 | |
Tres Seaver | 4246832 | 2017-09-11 15:36:53 -0400 | [diff] [blame] | 136 | def test_readonly_scoped_credentials_requires_scopes(): |
Jon Wayne Parrott | 4460a96 | 2017-09-12 10:01:23 -0700 | [diff] [blame] | 137 | credentials = ReadOnlyScopedCredentialsImpl() |
Jon Wayne Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame] | 138 | assert not credentials.requires_scopes |
Jon Wayne Parrott | f89a3cf | 2016-10-31 10:52:57 -0700 | [diff] [blame] | 139 | |
| 140 | |
| 141 | class RequiresScopedCredentialsImpl(credentials.Scoped, CredentialsImpl): |
| 142 | def __init__(self, scopes=None): |
| 143 | super(RequiresScopedCredentialsImpl, self).__init__() |
| 144 | self._scopes = scopes |
| 145 | |
| 146 | @property |
| 147 | def requires_scopes(self): |
| 148 | return not self.scopes |
| 149 | |
| 150 | def with_scopes(self, scopes): |
| 151 | return RequiresScopedCredentialsImpl(scopes=scopes) |
| 152 | |
| 153 | |
| 154 | def test_create_scoped_if_required_scoped(): |
| 155 | unscoped_credentials = RequiresScopedCredentialsImpl() |
| 156 | scoped_credentials = credentials.with_scopes_if_required( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 157 | unscoped_credentials, ["one", "two"] |
| 158 | ) |
Jon Wayne Parrott | f89a3cf | 2016-10-31 10:52:57 -0700 | [diff] [blame] | 159 | |
| 160 | assert scoped_credentials is not unscoped_credentials |
| 161 | assert not scoped_credentials.requires_scopes |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 162 | assert scoped_credentials.has_scopes(["one", "two"]) |
Jon Wayne Parrott | f89a3cf | 2016-10-31 10:52:57 -0700 | [diff] [blame] | 163 | |
| 164 | |
| 165 | def test_create_scoped_if_required_not_scopes(): |
| 166 | unscoped_credentials = CredentialsImpl() |
| 167 | scoped_credentials = credentials.with_scopes_if_required( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 168 | unscoped_credentials, ["one", "two"] |
| 169 | ) |
Jon Wayne Parrott | f89a3cf | 2016-10-31 10:52:57 -0700 | [diff] [blame] | 170 | |
| 171 | assert scoped_credentials is unscoped_credentials |