Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/google/auth/_credentials_base.py: 67%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

12 statements  

1# Copyright 2024 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 

15 

16"""Interface for base credentials.""" 

17 

18import abc 

19 

20from google.auth import _helpers 

21 

22 

23class _BaseCredentials(metaclass=abc.ABCMeta): 

24 """Base class for all credentials. 

25 

26 All credentials have a :attr:`token` that is used for authentication and 

27 may also optionally set an :attr:`expiry` to indicate when the token will 

28 no longer be valid. 

29 

30 Most credentials will be :attr:`invalid` until :meth:`refresh` is called. 

31 Credentials can do this automatically before the first HTTP request in 

32 :meth:`before_request`. 

33 

34 Although the token and expiration will change as the credentials are 

35 :meth:`refreshed <refresh>` and used, credentials should be considered 

36 immutable. Various credentials will accept configuration such as private 

37 keys, scopes, and other options. These options are not changeable after 

38 construction. Some classes will provide mechanisms to copy the credentials 

39 with modifications such as :meth:`ScopedCredentials.with_scopes`. 

40 

41 Attributes: 

42 token (Optional[str]): The bearer token that can be used in HTTP headers to make 

43 authenticated requests. 

44 """ 

45 

46 def __init__(self): 

47 self.token = None 

48 

49 @abc.abstractmethod 

50 def refresh(self, request): 

51 """Refreshes the access token. 

52 

53 Args: 

54 request (google.auth.transport.Request): The object used to make 

55 HTTP requests. 

56 

57 Raises: 

58 google.auth.exceptions.RefreshError: If the credentials could 

59 not be refreshed. 

60 """ 

61 # pylint: disable=missing-raises-doc 

62 # (pylint doesn't recognize that this is abstract) 

63 raise NotImplementedError("Refresh must be implemented") 

64 

65 def _apply(self, headers, token=None): 

66 """Apply the token to the authentication header. 

67 

68 Args: 

69 headers (Mapping): The HTTP request headers. 

70 token (Optional[str]): If specified, overrides the current access 

71 token. 

72 """ 

73 headers["authorization"] = "Bearer {}".format( 

74 _helpers.from_bytes(token or self.token) 

75 )