_service_account_info.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. import six
  18. from google.auth import crypt
  19. def from_dict(data, require=None):
  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. Returns:
  28. google.auth.crypt.Signer: A signer created from the private key in the
  29. service account file.
  30. Raises:
  31. ValueError: if the data was in the wrong format, or if one of the
  32. required keys is missing.
  33. """
  34. keys_needed = set(require if require is not None else [])
  35. missing = keys_needed.difference(six.iterkeys(data))
  36. if missing:
  37. raise ValueError(
  38. "Service account info was not in the expected format, missing "
  39. "fields {}.".format(", ".join(missing))
  40. )
  41. # Create a signer.
  42. signer = crypt.RSASigner.from_service_account_info(data)
  43. return signer
  44. def from_filename(filename, require=None):
  45. """Reads a Google service account JSON file and returns its parsed info.
  46. Args:
  47. filename (str): The path to the service account .json file.
  48. require (Sequence[str]): List of keys required to be present in the
  49. info.
  50. Returns:
  51. Tuple[ Mapping[str, str], google.auth.crypt.Signer ]: The verified
  52. info and a signer instance.
  53. """
  54. with io.open(filename, "r", encoding="utf-8") as json_file:
  55. data = json.load(json_file)
  56. return data, from_dict(data, require=require)