feature request: API to check TTL on secure message
For secure messages with a short TTL it can be useful to know if the TTL is about to expire, so a renewed message can be issued. Would it technically be possible to have a API call on the cryptography.fernet.Fernet() instance to determine how many seconds are left?
The TTL is not part of the encrypted payload so parsing it is easy. However, unless you verify the HMAC you don't know if the TTL has been tampered with. That may be safe in some use cases (https://github.com/dolph/keyless-fernet allows this), but is not generically secure. I'm opposed to adding anything that parses TTL without checking HMAC, but would be happy to take a patch to add an API that, given a fernet key, tells you how many seconds are left until expiry (or raises a token error if invalid).
Thanks for the reply.
I did assume the API would indeed require the key in order to have a validated check on the TTL. I have no idea yet whether I can come up with a patch. Any pointers where to start is appreciated. .
You might take a look at how we calculate whether ttl has expired on decrypt (https://github.com/pyca/cryptography/blob/master/src/cryptography/fernet.py#L97-L103) and see if it makes sense to refactor that into a public method that raises or returns.