_service_account_info.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. """Helper functions for loading data from a Google service account file."""
  15. import io
  16. import json
  17. from google.auth import crypt
  18. from google.auth import exceptions
  19. def from_dict(data, require=None, use_rsa_signer=True):
  20. """Validates a dictionary containing Google service account data.
  21. Creates and returns a :class:`google.auth.crypt.Signer` instance from the
  22. private key specified in the data.
  23. Args:
  24. data (Mapping[str, str]): The service account data
  25. require (Sequence[str]): List of keys required to be present in the
  26. info.
  27. use_rsa_signer (Optional[bool]): Whether to use RSA signer or EC signer.
  28. We use RSA signer by default.
  29. Returns:
  30. google.auth.crypt.Signer: A signer created from the private key in the
  31. service account file.
  32. Raises:
  33. MalformedError: if the data was in the wrong format, or if one of the
  34. required keys is missing.
  35. """
  36. keys_needed = set(require if require is not None else [])
  37. missing = keys_needed.difference(data.keys())
  38. if missing:
  39. raise exceptions.MalformedError(
  40. "Service account info was not in the expected format, missing "
  41. "fields {}.".format(", ".join(missing))
  42. )
  43. # Create a signer.
  44. if use_rsa_signer:
  45. signer = crypt.RSASigner.from_service_account_info(data)
  46. else:
  47. signer = crypt.ES256Signer.from_service_account_info(data)
  48. return signer
  49. def from_filename(filename, require=None, use_rsa_signer=True):
  50. """Reads a Google service account JSON file and returns its parsed info.
  51. Args:
  52. filename (str): The path to the service account .json file.
  53. require (Sequence[str]): List of keys required to be present in the
  54. info.
  55. use_rsa_signer (Optional[bool]): Whether to use RSA signer or EC signer.
  56. We use RSA signer by default.
  57. Returns:
  58. Tuple[ Mapping[str, str], google.auth.crypt.Signer ]: The verified
  59. info and a signer instance.
  60. """
  61. with io.open(filename, "r", encoding="utf-8") as json_file:
  62. data = json.load(json_file)
  63. return data, from_dict(data, require=require, use_rsa_signer=use_rsa_signer)