PublicKey.py
1 ############################ Copyrights and license ############################ 2 # # 3 # Copyright 2012 Vincent Jacques <vincent@vincent-jacques.net> # 4 # Copyright 2012 Zearin <zearin@gonk.net> # 5 # Copyright 2013 AKFish <akfish@gmail.com> # 6 # Copyright 2013 Vincent Jacques <vincent@vincent-jacques.net> # 7 # Copyright 2014 Vincent Jacques <vincent@vincent-jacques.net> # 8 # Copyright 2016 Jannis Gebauer <ja.geb@me.com> # 9 # Copyright 2016 Peter Buckley <dx-pbuckley@users.noreply.github.com> # 10 # Copyright 2018 Wan Liuyang <tsfdye@gmail.com> # 11 # Copyright 2018 sfdye <tsfdye@gmail.com> # 12 # # 13 # This file is part of PyGithub. # 14 # http://pygithub.readthedocs.io/ # 15 # # 16 # PyGithub is free software: you can redistribute it and/or modify it under # 17 # the terms of the GNU Lesser General Public License as published by the Free # 18 # Software Foundation, either version 3 of the License, or (at your option) # 19 # any later version. # 20 # # 21 # PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY # 22 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # 23 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more # 24 # details. # 25 # # 26 # You should have received a copy of the GNU Lesser General Public License # 27 # along with PyGithub. If not, see <http://www.gnu.org/licenses/>. # 28 # # 29 ################################################################################ 30 31 # https://docs.github.com/en/rest/reference/actions#example-encrypting-a-secret-using-python 32 from __future__ import annotations 33 34 from base64 import b64encode 35 from typing import Any 36 37 from nacl import encoding, public 38 39 from github.GithubObject import Attribute, CompletableGithubObject, NotSet 40 41 42 def encrypt(public_key: str, secret_value: str) -> str: 43 """Encrypt a Unicode string using the public key.""" 44 pk = public.PublicKey(public_key.encode("utf-8"), encoding.Base64Encoder) 45 sealed_box = public.SealedBox(pk) 46 encrypted = sealed_box.encrypt(secret_value.encode("utf-8")) 47 return b64encode(encrypted).decode("utf-8") 48 49 50 class PublicKey(CompletableGithubObject): 51 """ 52 This class represents either an organization public key or a repository public key. 53 The reference can be found here https://docs.github.com/en/rest/reference/actions#get-an-organization-public-key 54 or here https://docs.github.com/en/rest/reference/actions#get-a-repository-public-key 55 """ 56 57 def _initAttributes(self) -> None: 58 self._key_id: Attribute[str | int] = NotSet 59 self._key: Attribute[str] = NotSet 60 61 def __repr__(self) -> str: 62 return self.get__repr__({"key_id": self._key_id.value, "key": self._key.value}) 63 64 @property 65 def key(self) -> str: 66 self._completeIfNotSet(self._key) 67 return self._key.value 68 69 @property 70 def key_id(self) -> str | int: 71 self._completeIfNotSet(self._key_id) 72 return self._key_id.value 73 74 def _useAttributes(self, attributes: dict[str, Any]) -> None: 75 if "key" in attributes: # pragma no branch 76 self._key = self._makeStringAttribute(attributes["key"]) 77 if "key_id" in attributes: # pragma no branch 78 if type(attributes["key_id"]) == str: 79 self._key_id = self._makeStringAttribute(attributes["key_id"]) 80 else: 81 self._key_id = self._makeIntAttribute(attributes["key_id"]) 82 83 def encrypt(self, unencrypted_value: str) -> str: 84 return encrypt(self._key.value, unencrypted_value)