1515from cryptojwt .jws .hmac import HMACSigner
1616from cryptojwt .jwt import utc_time_sans_frac
1717from cryptojwt .key_jar import init_key_jar
18+ from cryptojwt .utils import as_bytes
1819
20+ from idpyoidc .encrypter import init_encrypter
1921from idpyoidc .server .util import lv_pack
2022from idpyoidc .server .util import lv_unpack
2123from idpyoidc .time_util import epoch_in_a_while
@@ -37,17 +39,25 @@ def __init__(
3739 keys : Optional [dict ] = None ,
3840 sign_alg : [str ] = "SHA256" ,
3941 name : Optional [dict ] = None ,
42+ crypt_config : Optional [dict ] = None ,
4043 ** kwargs ,
4144 ):
45+ self .sign_key = None
46+ self .enc_key = None
47+ self .crypt = None
4248
4349 if keys :
4450 key_jar = init_key_jar (** keys )
45- _keys = key_jar .get_signing_key (key_type = "oct" , kid = "sig" )
51+ _keys = key_jar .get_signing_key (key_type = "oct" )
4652 if _keys :
4753 self .sign_key = _keys [0 ]
48- _keys = key_jar .get_encrypt_key (key_type = "oct" , kid = "enc" )
54+ _keys = key_jar .get_encrypt_key (key_type = "oct" )
4955 if _keys :
5056 self .enc_key = _keys [0 ]
57+ elif crypt_config :
58+ _crypt = init_encrypter (crypt_config )
59+ self .crypt = _crypt ["encrypter" ]
60+ self .crypt_config = _crypt ["conf" ]
5161 else :
5262 if sign_key :
5363 if isinstance (sign_key , SYMKey ):
@@ -132,6 +142,11 @@ def _sign_enc_payload(self, payload: str, timestamp: Optional[Union[int, str]] =
132142 base64 .b64encode (ctx ),
133143 base64 .b64encode (tag ),
134144 ]
145+ elif self .crypt :
146+ msg = lv_pack (timestamp , payload )
147+ cookie_payload = [
148+ bytes_timestamp ,
149+ base64 .b64encode (self .crypt .encrypt (msg .encode ()))]
135150 else :
136151 cookie_payload = [bytes_timestamp , bytes_load , base64 .b64encode (mac )]
137152
@@ -147,6 +162,15 @@ def _ver_dec_content(self, parts):
147162
148163 if parts is None :
149164 return None
165+ elif len (parts ) == 2 :
166+ t0 , enc_payload = parts
167+ if not self .crypt :
168+ raise VerificationError ("Can not decrypt" )
169+ msg = self .crypt .decrypt (base64 .b64decode (as_bytes (enc_payload )))
170+ t1 , payload = lv_unpack (msg .decode ("utf-8" ))
171+ if t0 != t1 :
172+ raise VerificationError ('Suspicious timestamp' )
173+ return payload , t1
150174 elif len (parts ) == 3 :
151175 # verify the cookie signature
152176 timestamp , payload , b64_mac = parts
@@ -255,15 +279,15 @@ def parse_cookie(self, name: str, cookies: List[dict]) -> Optional[List[dict]]:
255279 LOGGER .debug ("Looking for '{}' cookies" .format (name ))
256280 res = []
257281 for _cookie in cookies :
258- LOGGER .debug ("Cookie: {}" . format ( _cookie ) )
282+ LOGGER .debug (f "Cookie: { _cookie } " )
259283 if "name" in _cookie and _cookie ["name" ] == name :
260284 _content = self ._ver_dec_content (_cookie ["value" ].split ("|" ))
261285 if _content :
262- payload , timestamp = self . _ver_dec_content ( _cookie [ "value" ]. split ( "|" ))
286+ payload , timestamp = _content
263287 value , typ = payload .split ("::" )
264288 res .append ({"value" : value , "type" : typ , "timestamp" : timestamp })
265289 else :
266- LOGGER .debug (f"Could not verify { name } cookie" )
290+ LOGGER .debug (f"Could not verify ' { name } ' cookie" )
267291 return res
268292
269293
0 commit comments