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

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

18 statements  

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 

20from google.auth import crypt, exceptions 

21 

22 

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

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

25 

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

27 private key specified in the data. 

28 

29 Args: 

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

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

32 info. 

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

34 We use RSA signer by default. 

35 

36 Returns: 

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

38 service account file. 

39 

40 Raises: 

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

42 required keys is missing. 

43 """ 

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

45 

46 missing = keys_needed.difference(data.keys()) 

47 

48 if missing: 

49 raise exceptions.MalformedError( 

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

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

52 ) 

53 

54 # Create a signer. 

55 if use_rsa_signer: 

56 signer = crypt.RSASigner.from_service_account_info(data) 

57 else: 

58 signer = crypt.EsSigner.from_service_account_info(data) 

59 

60 return signer 

61 

62 

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

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

65 

66 Args: 

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

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

69 info. 

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

71 We use RSA signer by default. 

72 

73 Returns: 

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

75 info and a signer instance. 

76 """ 

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

78 data = json.load(json_file) 

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