2022-11-16 17:12:38 +00:00
|
|
|
"""
|
|
|
|
temporary legacy
|
|
|
|
"""
|
|
|
|
from typing import Optional
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
from selfprivacy_api.utils import UserDataFiles, WriteUserData, ReadUserData
|
|
|
|
from selfprivacy_api.models.tokens.token import Token
|
|
|
|
from selfprivacy_api.models.tokens.recovery_key import RecoveryKey
|
|
|
|
from selfprivacy_api.models.tokens.new_device_key import NewDeviceKey
|
|
|
|
from selfprivacy_api.repositories.tokens.exceptions import (
|
|
|
|
TokenNotFound,
|
|
|
|
)
|
|
|
|
from selfprivacy_api.repositories.tokens.abstract_tokens_repository import (
|
|
|
|
AbstractTokensRepository,
|
|
|
|
)
|
|
|
|
|
|
|
|
DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S.%f"
|
|
|
|
|
|
|
|
|
|
|
|
class JsonTokensRepository(AbstractTokensRepository):
|
|
|
|
def get_tokens(self) -> list[Token]:
|
|
|
|
"""Get the tokens"""
|
|
|
|
tokens_list = []
|
|
|
|
|
|
|
|
with ReadUserData(UserDataFiles.TOKENS) as tokens_file:
|
|
|
|
for userdata_token in tokens_file["tokens"]:
|
|
|
|
tokens_list.append(
|
|
|
|
Token(
|
|
|
|
token=userdata_token["token"],
|
|
|
|
device_name=userdata_token["name"],
|
|
|
|
created_at=userdata_token["date"],
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return tokens_list
|
|
|
|
|
2022-12-12 11:50:04 +00:00
|
|
|
def _store_token(self, new_token: Token):
|
|
|
|
"""Store a token directly"""
|
2022-11-16 17:12:38 +00:00
|
|
|
with WriteUserData(UserDataFiles.TOKENS) as tokens_file:
|
|
|
|
tokens_file["tokens"].append(
|
|
|
|
{
|
|
|
|
"token": new_token.token,
|
|
|
|
"name": new_token.device_name,
|
|
|
|
"date": new_token.created_at.strftime(DATETIME_FORMAT),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
def delete_token(self, input_token: Token) -> None:
|
|
|
|
"""Delete the token"""
|
|
|
|
with WriteUserData(UserDataFiles.TOKENS) as tokens_file:
|
|
|
|
for userdata_token in tokens_file["tokens"]:
|
|
|
|
if userdata_token["token"] == input_token.token:
|
|
|
|
tokens_file["tokens"].remove(userdata_token)
|
|
|
|
return
|
|
|
|
|
|
|
|
raise TokenNotFound("Token not found!")
|
|
|
|
|
|
|
|
def get_recovery_key(self) -> Optional[RecoveryKey]:
|
|
|
|
"""Get the recovery key"""
|
|
|
|
with ReadUserData(UserDataFiles.TOKENS) as tokens_file:
|
|
|
|
|
|
|
|
if (
|
|
|
|
"recovery_token" not in tokens_file
|
|
|
|
or tokens_file["recovery_token"] is None
|
|
|
|
):
|
|
|
|
return
|
|
|
|
|
|
|
|
recovery_key = RecoveryKey(
|
|
|
|
key=tokens_file["recovery_token"].get("token"),
|
|
|
|
created_at=tokens_file["recovery_token"].get("date"),
|
2022-12-19 17:37:44 +00:00
|
|
|
expires_at=tokens_file["recovery_token"].get("expiration"),
|
2022-11-16 17:12:38 +00:00
|
|
|
uses_left=tokens_file["recovery_token"].get("uses_left"),
|
|
|
|
)
|
|
|
|
|
|
|
|
return recovery_key
|
|
|
|
|
|
|
|
def create_recovery_key(
|
|
|
|
self,
|
|
|
|
expiration: Optional[datetime],
|
|
|
|
uses_left: Optional[int],
|
|
|
|
) -> RecoveryKey:
|
|
|
|
"""Create the recovery key"""
|
|
|
|
|
|
|
|
recovery_key = RecoveryKey.generate(expiration, uses_left)
|
|
|
|
|
|
|
|
with WriteUserData(UserDataFiles.TOKENS) as tokens_file:
|
2022-12-30 18:06:16 +00:00
|
|
|
key_expiration: Optional[str] = None
|
|
|
|
if recovery_key.expires_at is not None:
|
|
|
|
key_expiration = recovery_key.expires_at.strftime(DATETIME_FORMAT)
|
2022-11-16 17:12:38 +00:00
|
|
|
tokens_file["recovery_token"] = {
|
|
|
|
"token": recovery_key.key,
|
|
|
|
"date": recovery_key.created_at.strftime(DATETIME_FORMAT),
|
2022-12-30 18:06:16 +00:00
|
|
|
"expiration": key_expiration,
|
2022-11-16 17:12:38 +00:00
|
|
|
"uses_left": recovery_key.uses_left,
|
|
|
|
}
|
|
|
|
|
|
|
|
return recovery_key
|
|
|
|
|
2022-12-12 14:06:24 +00:00
|
|
|
def _decrement_recovery_token(self):
|
2022-12-12 14:22:36 +00:00
|
|
|
"""Decrement recovery key use count by one"""
|
2022-12-12 14:06:24 +00:00
|
|
|
if self.is_recovery_key_valid():
|
|
|
|
with WriteUserData(UserDataFiles.TOKENS) as tokens:
|
2022-12-19 12:57:32 +00:00
|
|
|
if tokens["recovery_token"]["uses_left"] is not None:
|
|
|
|
tokens["recovery_token"]["uses_left"] -= 1
|
2022-11-16 17:12:38 +00:00
|
|
|
|
2022-12-26 12:31:09 +00:00
|
|
|
def _store_new_device_key(self, new_device_key: NewDeviceKey) -> None:
|
2022-11-16 17:12:38 +00:00
|
|
|
with WriteUserData(UserDataFiles.TOKENS) as tokens_file:
|
|
|
|
tokens_file["new_device"] = {
|
|
|
|
"token": new_device_key.key,
|
|
|
|
"date": new_device_key.created_at.strftime(DATETIME_FORMAT),
|
|
|
|
"expiration": new_device_key.expires_at.strftime(DATETIME_FORMAT),
|
|
|
|
}
|
|
|
|
|
|
|
|
def delete_new_device_key(self) -> None:
|
|
|
|
"""Delete the new device key"""
|
|
|
|
with WriteUserData(UserDataFiles.TOKENS) as tokens_file:
|
|
|
|
if "new_device" in tokens_file:
|
|
|
|
del tokens_file["new_device"]
|
|
|
|
return
|
|
|
|
|
2022-12-12 15:19:00 +00:00
|
|
|
def _get_stored_new_device_key(self) -> Optional[NewDeviceKey]:
|
|
|
|
"""Retrieves new device key that is already stored."""
|
2022-11-16 17:12:38 +00:00
|
|
|
with ReadUserData(UserDataFiles.TOKENS) as tokens_file:
|
|
|
|
if "new_device" not in tokens_file or tokens_file["new_device"] is None:
|
2022-12-12 15:19:00 +00:00
|
|
|
return
|
2022-11-16 17:12:38 +00:00
|
|
|
|
|
|
|
new_device_key = NewDeviceKey(
|
|
|
|
key=tokens_file["new_device"]["token"],
|
|
|
|
created_at=tokens_file["new_device"]["date"],
|
|
|
|
expires_at=tokens_file["new_device"]["expiration"],
|
|
|
|
)
|
2022-12-12 15:19:00 +00:00
|
|
|
return new_device_key
|