codekingpro/portable-devtools
114k
1# Copyright 2016 Google LLC2#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 at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# 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 and13# limitations under the License.14 15"""Helper functions for loading data from a Google service account file."""16 17import io18import json19 20from google.auth import crypt21from google.auth import exceptions22 23 24def from_dict(data, require=None, use_rsa_signer=True):25 """Validates a dictionary containing Google service account data.26 27 Creates and returns a :class:`google.auth.crypt.Signer` instance from the28 private key specified in the data.29 30 Args:31 data (Mapping[str, str]): The service account data32 require (Sequence[str]): List of keys required to be present in the33 info.34 use_rsa_signer (Optional[bool]): Whether to use RSA signer or EC signer.35 We use RSA signer by default.36 37 Returns:38 google.auth.crypt.Signer: A signer created from the private key in the39 service account file.40 41 Raises:42 MalformedError: if the data was in the wrong format, or if one of the43 required keys is missing.44 """45 keys_needed = set(require if require is not None else [])46 47 missing = keys_needed.difference(data.keys())48 49 if missing:50 raise exceptions.MalformedError(51 "Service account info was not in the expected format, missing "52 "fields {}.".format(", ".join(missing))53 )54 55 # Create a signer.56 if use_rsa_signer:57 signer = crypt.RSASigner.from_service_account_info(data)58 else:59 signer = crypt.ES256Signer.from_service_account_info(data)60 61 return signer62 63 64def from_filename(filename, require=None, use_rsa_signer=True):65 """Reads a Google service account JSON file and returns its parsed info.66 67 Args:68 filename (str): The path to the service account .json file.69 require (Sequence[str]): List of keys required to be present in the70 info.71 use_rsa_signer (Optional[bool]): Whether to use RSA signer or EC signer.72 We use RSA signer by default.73 74 Returns:75 Tuple[ Mapping[str, str], google.auth.crypt.Signer ]: The verified76 info and a signer instance.77 """78 with io.open(filename, "r", encoding="utf-8") as json_file:79 data = json.load(json_file)80 return data, from_dict(data, require=require, use_rsa_signer=use_rsa_signer)81 