1212import cryptography .hazmat .backends as _backends
1313import cryptography .hazmat .primitives .ciphers as _ciphers
1414
15+ from .errors import SymmetricCryptographyError
16+
1517
1618class Fernet (object ):
1719 """The default symmetric cryptography method."""
@@ -30,26 +32,65 @@ def __init__(self, key=None):
3032
3133 :param key: byte data representing the encyption/decryption key
3234 """
33- self ._symmetric = _fernet .Fernet (key or self .__class__ .generate_key ())
35+ if key :
36+ fernet_key_error = SymmetricCryptographyError (
37+ "Fernet key must be 32 url-safe base64-encoded bytes."
38+ )
39+ try :
40+ raw_key = _base64 .b64decode (key )
41+ except Exception as e :
42+ raise fernet_key_error from e
43+ else :
44+ if len (raw_key ) != 32 :
45+ raise fernet_key_error
46+ else :
47+ key = self .__class__ .generate_key ()
3448
35- def encrypt (self , plaintext ):
49+ self ._symmetric = _fernet .Fernet (key )
50+
51+ def encrypt (self , plaintext , * args , ** kwargs ):
3652 """Encrypt the given plaintext.
3753
3854 :param plaintext: byte data representing the plaintext
3955 :return: byte data representing the ciphertext
4056 """
57+ if args or kwargs :
58+ _deprecation_msg = (
59+ "The '.encrypt' method does not take into account any arguements, "
60+ "other than the 'ciphertext' param. "
61+ "Remove any other arguements. "
62+ "In the next version, this method will not allow them."
63+ )
64+ _warnings .warn (_deprecation_msg , DeprecationWarning )
65+
4166 ciphertext = self ._symmetric .encrypt (plaintext )
4267 return ciphertext
4368
44- def decrypt (self , ciphertext ):
69+ def decrypt (self , ciphertext , * args , ** kwargs ):
4570 """Decrypt the given ciphertext.
4671
4772 :param ciphertext: byte data representing the ciphertext
4873 :return: byte data representing the plaintext
4974 """
75+ if args or kwargs :
76+ _deprecation_msg = (
77+ "The '.decrypt' method does not take into account any arguements, "
78+ "other than the 'ciphertext' param. "
79+ "Remove any other arguements. "
80+ "In the next version, this method will not allow them."
81+ )
82+ _warnings .warn (_deprecation_msg , DeprecationWarning )
83+
5084 plaintext = self ._symmetric .decrypt (ciphertext )
5185 return plaintext
5286
87+ def build_cipher (self , * args , ** kwargs ):
88+ _deprecation_msg = (
89+ "The 'Fernet' class does not need a build_cipher method."
90+ "Remove any calls to this method. "
91+ "In the next version, this method will be removed."
92+ ).format (name = cls .__name__ , type = type (cls ).__name__ )
93+ _warnings .warn (_deprecation_msg , DeprecationWarning )
5394
5495
5596class AESCipher (object ):
@@ -71,7 +112,9 @@ def _deprecation_notice(cls):
71112 _deprecation_msg = (
72113 '{name} {type} is deprecated. '
73114 'It will be removed in the next version. '
74- 'Use saml2.cryptography.symmetric instead.'
115+ 'Use saml2.cryptography.symmetric.Default '
116+ 'or saml2.cryptography.symmetric.Fernet '
117+ 'instead.'
75118 ).format (name = cls .__name__ , type = type (cls ).__name__ )
76119 _warnings .warn (_deprecation_msg , DeprecationWarning )
77120
0 commit comments