Source code for pytket.extensions.quantinuum.backends.credential_storage
# Copyright 2020-2024 Quantinuum## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.fromabcimportABC,abstractmethodfromtypingimportOptionalfromdatetimeimporttimedelta,datetime,timezoneimportjwtfrom.configimportQuantinuumConfigclassCredentialStorage(ABC):"""Storage for Quantinuum username and tokens"""def__init__(self,id_token_timedelt:timedelta=timedelta(minutes=55),refresh_token_timedelt:timedelta=timedelta(days=29),)->None:self._id_timedelt=id_token_timedeltself._refresh_timedelt=refresh_token_timedelt@abstractmethoddefsave_refresh_token(self,refresh_token:str)->None:"""save refresh token"""@abstractmethoddefsave_id_token(self,id_token:str)->None:"""save ID token"""@abstractmethoddefsave_user_name(self,user_name:str)->None:"""save user_name"""defsave_tokens(self,id_token:str,refresh_token:str)->None:self.save_id_token(id_token)self.save_refresh_token(refresh_token)@abstractmethoddefdelete_credential(self)->None:"""delete credential"""@propertydefid_token(self)->Optional[str]:"""returns a ID token if valid"""@propertydefrefresh_token(self)->Optional[str]:"""returns a refresh token if valid"""@propertydefuser_name(self)->Optional[str]:"""returns the user name"""
[docs]classMemoryCredentialStorage(CredentialStorage):"""In memory credential storage. Intended use is only to store id tokens, refresh tokens and user_name. Password storage is only included for debug purposes."""def__init__(self,id_token_timedelt:timedelta=timedelta(minutes=55),refresh_token_timedelt:timedelta=timedelta(days=29),)->None:super().__init__(id_token_timedelt,refresh_token_timedelt)self._user_name:Optional[str]=Noneself._password:Optional[str]=Noneself._id_token:Optional[str]=Noneself._refresh_token:Optional[str]=Noneself._id_token_timeout:Optional[datetime]=Noneself._refresh_token_timeout:Optional[datetime]=None
[docs]classQuantinuumConfigCredentialStorage(CredentialStorage):"""Store tokens in the default pytket configuration file."""def__init__(self,id_token_timedelt:timedelta=timedelta(minutes=55),refresh_token_timedelt:timedelta=timedelta(days=29),)->None:super().__init__(id_token_timedelt,refresh_token_timedelt)