mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2024-11-13 22:23:16 +00:00
def
16e0bd56ce
Co-authored-by: def <dettlaff@riseup.net> Co-authored-by: Inex Code <inex.code@selfprivacy.org> Reviewed-on: https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api/pulls/18 Co-authored-by: def <dettlaff@riseup.net> Co-committed-by: def <dettlaff@riseup.net>
47 lines
1.1 KiB
Python
47 lines
1.1 KiB
Python
"""
|
|
New device key used to obtain access token.
|
|
"""
|
|
from datetime import datetime, timedelta
|
|
import secrets
|
|
from pydantic import BaseModel
|
|
from mnemonic import Mnemonic
|
|
|
|
|
|
class NewDeviceKey(BaseModel):
|
|
"""
|
|
Recovery key used to obtain access token.
|
|
|
|
Recovery key has a key string, date of creation, date of expiration.
|
|
"""
|
|
|
|
key: str
|
|
created_at: datetime
|
|
expires_at: datetime
|
|
|
|
def is_valid(self) -> bool:
|
|
"""
|
|
Check if the recovery key is valid.
|
|
"""
|
|
if self.expires_at < datetime.now():
|
|
return False
|
|
return True
|
|
|
|
def as_mnemonic(self) -> str:
|
|
"""
|
|
Get the recovery key as a mnemonic.
|
|
"""
|
|
return Mnemonic(language="english").to_mnemonic(bytes.fromhex(self.key))
|
|
|
|
@staticmethod
|
|
def generate() -> "NewDeviceKey":
|
|
"""
|
|
Factory to generate a random token.
|
|
"""
|
|
creation_date = datetime.now()
|
|
key = secrets.token_bytes(16).hex()
|
|
return NewDeviceKey(
|
|
key=key,
|
|
created_at=creation_date,
|
|
expires_at=datetime.now() + timedelta(minutes=10),
|
|
)
|