C.J. Collier | 37141e4 | 2020-02-13 13:49:49 -0800 | [diff] [blame] | 1 | # Copyright 2016 Google LLC |
Jon Wayne Parrott | 9281ca0 | 2017-08-11 14:36:42 -0700 | [diff] [blame] | 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 | """Base classes for cryptographic signers and verifiers.""" |
| 16 | |
| 17 | import abc |
Danny Hermes | 1cd8390 | 2018-02-08 15:41:51 -0800 | [diff] [blame] | 18 | import io |
| 19 | import json |
Jon Wayne Parrott | 9281ca0 | 2017-08-11 14:36:42 -0700 | [diff] [blame] | 20 | |
arithmetic1728 | 5bd5ccf | 2021-10-21 15:25:46 -0700 | [diff] [blame^] | 21 | import six |
| 22 | |
Jon Wayne Parrott | 9281ca0 | 2017-08-11 14:36:42 -0700 | [diff] [blame] | 23 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 24 | _JSON_FILE_PRIVATE_KEY = "private_key" |
| 25 | _JSON_FILE_PRIVATE_KEY_ID = "private_key_id" |
Danny Hermes | 1cd8390 | 2018-02-08 15:41:51 -0800 | [diff] [blame] | 26 | |
| 27 | |
arithmetic1728 | 5bd5ccf | 2021-10-21 15:25:46 -0700 | [diff] [blame^] | 28 | @six.add_metaclass(abc.ABCMeta) |
| 29 | class Verifier(object): |
Jon Wayne Parrott | 9281ca0 | 2017-08-11 14:36:42 -0700 | [diff] [blame] | 30 | """Abstract base class for crytographic signature verifiers.""" |
| 31 | |
| 32 | @abc.abstractmethod |
| 33 | def verify(self, message, signature): |
| 34 | """Verifies a message against a cryptographic signature. |
| 35 | |
| 36 | Args: |
| 37 | message (Union[str, bytes]): The message to verify. |
| 38 | signature (Union[str, bytes]): The cryptography signature to check. |
| 39 | |
| 40 | Returns: |
| 41 | bool: True if message was signed by the private key associated |
| 42 | with the public key that this object was constructed with. |
| 43 | """ |
| 44 | # pylint: disable=missing-raises-doc,redundant-returns-doc |
| 45 | # (pylint doesn't recognize that this is abstract) |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 46 | raise NotImplementedError("Verify must be implemented") |
Jon Wayne Parrott | 9281ca0 | 2017-08-11 14:36:42 -0700 | [diff] [blame] | 47 | |
| 48 | |
arithmetic1728 | 5bd5ccf | 2021-10-21 15:25:46 -0700 | [diff] [blame^] | 49 | @six.add_metaclass(abc.ABCMeta) |
| 50 | class Signer(object): |
Jon Wayne Parrott | 9281ca0 | 2017-08-11 14:36:42 -0700 | [diff] [blame] | 51 | """Abstract base class for cryptographic signers.""" |
| 52 | |
| 53 | @abc.abstractproperty |
| 54 | def key_id(self): |
| 55 | """Optional[str]: The key ID used to identify this private key.""" |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 56 | raise NotImplementedError("Key id must be implemented") |
Jon Wayne Parrott | 9281ca0 | 2017-08-11 14:36:42 -0700 | [diff] [blame] | 57 | |
| 58 | @abc.abstractmethod |
| 59 | def sign(self, message): |
| 60 | """Signs a message. |
| 61 | |
| 62 | Args: |
| 63 | message (Union[str, bytes]): The message to be signed. |
| 64 | |
| 65 | Returns: |
| 66 | bytes: The signature of the message. |
| 67 | """ |
| 68 | # pylint: disable=missing-raises-doc,redundant-returns-doc |
| 69 | # (pylint doesn't recognize that this is abstract) |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 70 | raise NotImplementedError("Sign must be implemented") |
Danny Hermes | 1cd8390 | 2018-02-08 15:41:51 -0800 | [diff] [blame] | 71 | |
| 72 | |
arithmetic1728 | 5bd5ccf | 2021-10-21 15:25:46 -0700 | [diff] [blame^] | 73 | @six.add_metaclass(abc.ABCMeta) |
| 74 | class FromServiceAccountMixin(object): |
Danny Hermes | 1cd8390 | 2018-02-08 15:41:51 -0800 | [diff] [blame] | 75 | """Mix-in to enable factory constructors for a Signer.""" |
| 76 | |
| 77 | @abc.abstractmethod |
| 78 | def from_string(cls, key, key_id=None): |
| 79 | """Construct an Signer instance from a private key string. |
| 80 | |
| 81 | Args: |
| 82 | key (str): Private key as a string. |
| 83 | key_id (str): An optional key id used to identify the private key. |
| 84 | |
| 85 | Returns: |
| 86 | google.auth.crypt.Signer: The constructed signer. |
| 87 | |
| 88 | Raises: |
| 89 | ValueError: If the key cannot be parsed. |
| 90 | """ |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 91 | raise NotImplementedError("from_string must be implemented") |
Danny Hermes | 1cd8390 | 2018-02-08 15:41:51 -0800 | [diff] [blame] | 92 | |
| 93 | @classmethod |
| 94 | def from_service_account_info(cls, info): |
| 95 | """Creates a Signer instance instance from a dictionary containing |
| 96 | service account info in Google format. |
| 97 | |
| 98 | Args: |
| 99 | info (Mapping[str, str]): The service account info in Google |
| 100 | format. |
| 101 | |
| 102 | Returns: |
| 103 | google.auth.crypt.Signer: The constructed signer. |
| 104 | |
| 105 | Raises: |
| 106 | ValueError: If the info is not in the expected format. |
| 107 | """ |
| 108 | if _JSON_FILE_PRIVATE_KEY not in info: |
| 109 | raise ValueError( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 110 | "The private_key field was not found in the service account " "info." |
| 111 | ) |
Danny Hermes | 1cd8390 | 2018-02-08 15:41:51 -0800 | [diff] [blame] | 112 | |
| 113 | return cls.from_string( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 114 | info[_JSON_FILE_PRIVATE_KEY], info.get(_JSON_FILE_PRIVATE_KEY_ID) |
| 115 | ) |
Danny Hermes | 1cd8390 | 2018-02-08 15:41:51 -0800 | [diff] [blame] | 116 | |
| 117 | @classmethod |
| 118 | def from_service_account_file(cls, filename): |
| 119 | """Creates a Signer instance from a service account .json file |
| 120 | in Google format. |
| 121 | |
| 122 | Args: |
| 123 | filename (str): The path to the service account .json file. |
| 124 | |
| 125 | Returns: |
| 126 | google.auth.crypt.Signer: The constructed signer. |
| 127 | """ |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 128 | with io.open(filename, "r", encoding="utf-8") as json_file: |
Danny Hermes | 1cd8390 | 2018-02-08 15:41:51 -0800 | [diff] [blame] | 129 | data = json.load(json_file) |
| 130 | |
| 131 | return cls.from_service_account_info(data) |