Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/auth/_service_account_info.py: 42%

19 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-03-26 07:30 +0000

1# Copyright 2016 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"""Helper functions for loading data from a Google service account file.""" 

16 

17import io 

18import json 

19 

20import six 

21 

22from google.auth import crypt 

23from google.auth import exceptions 

24 

25 

26def from_dict(data, require=None, use_rsa_signer=True): 

27 """Validates a dictionary containing Google service account data. 

28 

29 Creates and returns a :class:`google.auth.crypt.Signer` instance from the 

30 private key specified in the data. 

31 

32 Args: 

33 data (Mapping[str, str]): The service account data 

34 require (Sequence[str]): List of keys required to be present in the 

35 info. 

36 use_rsa_signer (Optional[bool]): Whether to use RSA signer or EC signer. 

37 We use RSA signer by default. 

38 

39 Returns: 

40 google.auth.crypt.Signer: A signer created from the private key in the 

41 service account file. 

42 

43 Raises: 

44 MalformedError: if the data was in the wrong format, or if one of the 

45 required keys is missing. 

46 """ 

47 keys_needed = set(require if require is not None else []) 

48 

49 missing = keys_needed.difference(six.iterkeys(data)) 

50 

51 if missing: 

52 raise exceptions.MalformedError( 

53 "Service account info was not in the expected format, missing " 

54 "fields {}.".format(", ".join(missing)) 

55 ) 

56 

57 # Create a signer. 

58 if use_rsa_signer: 

59 signer = crypt.RSASigner.from_service_account_info(data) 

60 else: 

61 signer = crypt.ES256Signer.from_service_account_info(data) 

62 

63 return signer 

64 

65 

66def from_filename(filename, require=None, use_rsa_signer=True): 

67 """Reads a Google service account JSON file and returns its parsed info. 

68 

69 Args: 

70 filename (str): The path to the service account .json file. 

71 require (Sequence[str]): List of keys required to be present in the 

72 info. 

73 use_rsa_signer (Optional[bool]): Whether to use RSA signer or EC signer. 

74 We use RSA signer by default. 

75 

76 Returns: 

77 Tuple[ Mapping[str, str], google.auth.crypt.Signer ]: The verified 

78 info and a signer instance. 

79 """ 

80 with io.open(filename, "r", encoding="utf-8") as json_file: 

81 data = json.load(json_file) 

82 return data, from_dict(data, require=require, use_rsa_signer=use_rsa_signer)