Exemple #1
0
    def token(self, plaintext: str):
        """
        Encrypt, then store to the database the token string.

        Parameters
        ----------
        plaintext: str
            Token to encrypt.
        """
        self._token = get_fernet().encrypt(plaintext.encode()).decode()
Exemple #2
0
    def secret_key(self, plaintext: str):
        """
        Encrypt, then store to the database the per user otp secret.

        Parameters
        ----------
        plaintext: str
            Key to encrypt.
        """
        self._secret_key = get_fernet().encrypt(plaintext.encode()).decode()
Exemple #3
0
    def decrypted_token(self) -> str:
        """
        Decrypted token.

        Returns
        -------
        str
            Returns the decrypted token.
        """
        token = self._token
        try:
            token = token.encode()
        except AttributeError:
            pass  # Already bytes
        return get_fernet().decrypt(token).decode()
Exemple #4
0
    def decrypted_secret_key(self) -> str:
        """
        Decrypted per user otp secret.

        Returns
        -------
        str
            Returns the decrypted secret key
        """
        key = self._secret_key
        try:
            key = key.encode()
        except AttributeError:
            pass  # Already bytes
        try:
            return get_fernet().decrypt(key).decode()
        except Exception as exc:
            current_app.logger.debug(
                "Failed to decrypt key.", key=key, orig=self._secret_key, exception=exc
            )
            raise exc