Commit graph

53 commits

Author SHA1 Message Date
Alex Gaynor
6b966a9b21
Reformat most remaining tests (#9771)
this matches both ruff and black style
2023-10-25 11:10:42 -05:00
Paul Kehrer
ffc10f9bb0
remove a test dep (#8446) 2023-03-24 13:41:35 +00:00
Alex Gaynor
7ff729ecf2
Switch from flake8 to ruff (#7920)
It's more than 60x faster.
2022-12-21 09:44:47 +07:00
Robert Coup
a2a9109251
Allow Fernet decryption to accept string tokens (#7116)
* tests: better testid generation for fernet vectors

Use the vector filename and array index for the pytest id rather than a
concatenation of the vector content.

eg: `tests/test_fernet.py::TestFernet::test_invalid[invalid.json:2]`

* (Multi)Fernet: allow str tokens for decryption

Remove some developer friction by allowing string tokens to be passed to
Fernet decryption methods. Because a valid token as generated by
`Fernet.encrypt()` is url-safe base64-encoded, a non-ASCII token is
definitely invalid.

The stdlib base64 function already accepts and checks ASCII str values
so delegate to that.

* Kick CI
2022-04-29 22:27:47 +00:00
Alex Gaynor
02783d2297
Remove explicit subclassing of object now that all classes are new-style (#6830)
As someone who first with Python in 2.4 or so, this habit is going to be hard to break.
2022-01-30 16:29:24 +00:00
Bar Harel
d97bfeda9f
Informative error on incompatible Fernet key (#6768)
* Informative error

* tests + prevent key logging on error

* flake8

* black still downloading...
2021-12-28 00:04:54 +00:00
Alex Gaynor
d09e1eda2b
Final polish on removing backends from places they aren't required (#6644) 2021-11-22 05:01:31 +08:00
Alex Gaynor
245d15b563
Remove requires_backend_interface from tests because it was useless (#5884) 2021-03-01 11:59:24 -06:00
Paul Kehrer
343ac1344a
enable mypy over tests (#5721)
* clean mypy with tests dir

* remove most no_type_check annotations

* le sigh

* remove unneeded comments
2021-01-31 19:27:41 -05:00
Alex Gaynor
db9e20a4ce
Apply type annotations to fernet (#5708) 2021-01-29 15:39:20 -06:00
Alex Gaynor
e66db8079d
Complete removal of py2 (#5533)
* Drop Python 2

* Black everything
2020-12-09 09:13:48 -06:00
Alex Gaynor
e0477596f7
Remove __future__ import from our code (#5610) 2020-12-09 00:35:11 -06:00
Alex Gaynor
7d4e567e16
Switch black to py36 as the minimum version (#5608) 2020-12-08 23:40:21 -06:00
Paul Kehrer
bda138768a
new black, actually slightly different than the old black (#5429) 2020-08-26 22:59:43 -04:00
Alex Gaynor
60aa04481f
Paint it Black by the Rolling Stones (#5324) 2020-07-20 13:06:29 -05:00
Jakub Stasiak
9719375294
Disallow ttl=None in (Multi)Fernet.decrypt_at_time() (#5280)
* Disallow ttl=None in (Multi)Fernet.decrypt_at_time()

Since the introduction of the _at_time() methods in #5256[1] there's
been this little voice in the back of my mind telling me that maybe it's
not the best idea to allow ttl=None in decrypt_at_time(). It's been like
this for convenience and code reuse reasons.

Then I submitted a patch for cryptography stubs in typeshed[2] and I had
to decide whether to define decrypt_at_time()'s ttl as int and be
incompatible with cryptography's behavior or Optional[int] and advertise
an API that can be misused much too easily. I went ahead with int.

Considering the above I decided to propose this patch. Some amount of
redundancy (and a new test to properly cover the
MultiFernet.decrypt_at_time() implementation) is a price to prevent
clients from shooting themselves in the foot with the tll=None gun since
setting ttl to None disabled timestamp checks even if current_time was
provided.

[1] https://github.com/pyca/cryptography/pull/5256
[2] https://github.com/python/typeshed/pull/4238

* Actually test the return value here

* Fix formatting
2020-06-24 21:51:54 -04:00
Jakub Stasiak
0d0d70bd78
Add a way to pass current time to Fernet (#5256)
* Add a way to pass current time to Fernet

The motivation behind this is to be able to unit test code using Fernet
easily without having to monkey patch global state.

* Reformat to satisfy flake8

* Trigger a Fernet.encrypt() branch missing from coverage

* Revert specifying explicit current time in MultiFernet.rotate()

Message's timestamp is not verified anyway since ttl is None.

* Change the Fernet's explicit current time API slightly

This's been suggested in code review.

* Fix a typo

* Fix a typo

* Restore full MultiFernet test coverage and fix a typo

* Restore more coverage

time.time() is not called by MultiFernet.rotate() anymore so the monkey
patching and lambda need to go, because the patched function is not used
and coverage calculation will rightfully notice it.

* Remove an unused import

* Document when the *_at_time Fernet methods were added
2020-06-14 13:30:18 -05:00
Paul Kehrer
36ad98fd5e Add support for extracting timestamp from a Fernet token (#4229)
* Add API for retrieving the seconds-to-expiry for the token, given a TTL.

* Process PR feedback:

* Do compute the TTL, but just the age of the token. The caller
can decided what to do next.

* Factored out the HMAC signature verification to a separate function.

* Fixed a copy&paste mistake in the test cases

* Tests cleanup.

* `struct` no longer needed

* Document `def age()`

* typo in `age()` documentation

* token, not data

* remove test for TTL expiry that is already covered by the parameterized `test_invalid()`.

* let's call this extract_timestamp and just return timestamp

* review comments

* it's UNIX I know this
2018-05-12 11:57:32 -04:00
Chris Wolfe
af6f990064 Add Multifernet.rotate method (#3979)
* add rotate method

* add some more tests for the failure modes

* start adding some documentation for the rotate method

* operate on a single token at a time, leave lists to the caller

* add versionadded
add versionadded, drop rotate from class doctest

* give rotate a doctest

* single level, not aligned

* add changelog for mf.rotate

* show that, once rotated, the old fernet instance can no longer decrypt the token

* add the instead of just the how

* update docs to reflect removal of ttl from rotate

* update tests

* refactor internal methods so that we can extract the timestamp

* implement rotate

* update wordlist (case sensitive?)

* lints

* consistent naming

* get_token_data/get_unverified_token_data -> better name

* doc changes

* use the static method, do not treat as imethod

* move up to MultiFernet docs

* add to authors

* alter wording

* monkeypatch time to make it less possible for the test to pass simply due to calls occuring in less than one second

* set the time after encryption to make sure that the time is preserved as part of re-encryption
2017-10-19 03:23:53 +08:00
Paul Kehrer
4fc597d999 pass bytes to modes/algorithms like we should 2016-03-07 08:41:51 -04:30
Paul Kehrer
a418e96e08 fernet fix: ignore the timestamp entirely when no ttl is set
Previously if the token claimed to have been generated more than 60
seconds in the future we would raise InvalidToken even if ttl was set
to None.
2016-01-21 08:54:59 -06:00
Eeshan Garg
f123415031 Replace the remaining occurrences of six.u with the u prefix 2015-04-29 18:41:00 +05:30
Paul Kehrer
fdae070f8b alter vector file loader to support passing read mode 2014-11-27 08:03:22 -10:00
Alex Gaynor
5951f46be6 Update the license header for every source file, as well as the documentation.
Fixes #1209
2014-11-16 09:08:42 -08:00
Alex Gaynor
e6ac60265e Fixed #1436 -- mark the multifernet tests as requiring the backends they need 2014-10-24 07:50:30 -07:00
Alex Gaynor
7aab8b4ae4 Change how we represented that a test requires a backend.
This way is more extensible and requires less maintaince
2014-10-23 11:01:25 -07:00
Alex Gaynor
41b33b70d3 flake8 + cleanup 2014-10-20 14:34:35 -07:00
Alex Gaynor
4f286cec7a Handle non-iterable arguments reasonable 2014-10-20 11:30:57 -07:00
Alex Gaynor
7b593e1b5e Fixes #1327 -- adds multifernet 2014-10-20 07:55:00 -07:00
Alex Gaynor
a1a21f46c0 Fixed an issue in fernet where the wrong exception would occur on an all-nulls input.
Also switched a few tests to not generate a key
2014-10-19 22:32:42 -07:00
Alex Gaynor
732fbece83 Move the supported marks to class level for fernet 2014-10-19 18:46:55 -07:00
Alex Stapleton
a39a319b09 Move cryptography.vectors to cryptography_vectors
All vectors are now stored in the subpackage in the vectors/ folder.

This package is automatically installed by setup.py test and will also
be uploaded with a matching version number by the PyPI upload task.
2014-03-24 09:46:47 +00:00
Matthew Iversen
68e77c752b Load vectors from cryptography.vectors 2014-03-22 16:25:38 +00:00
Alex Gaynor
c37feed731 Added future imports and licenses that are missing 2014-03-08 08:32:56 -08:00
Paul Kehrer
0abdf870fa add test marks to fernet so backends without cipher (or AES/CBC) will skip 2014-01-09 22:21:14 -06:00
Alex Gaynor
e78960fa8c Handle invalid timestamp length 2013-12-20 11:02:33 -08:00
Alex Gaynor
a8f0b63ddd Replace assertions with real error checks 2013-12-16 15:44:06 -08:00
Alex Gaynor
fae20715b8 Address dreid's comments 2013-12-16 15:29:30 -08:00
Alex Gaynor
36597b4379 An API for generating keys 2013-11-22 10:25:13 -08:00
Alex Gaynor
56bcade581 fix, technically 2013-11-22 10:15:14 -08:00
Alex Gaynor
1d2901cae1 Hide the dangerous bits 2013-11-22 10:12:05 -08:00
Alex Gaynor
898fe0f899 Key in the right place 2013-11-20 16:38:32 -08:00
Alex Gaynor
8912d3afde Include the license 2013-11-02 14:04:19 -07:00
Alex Gaynor
7ecd3148ac py3k fixes 2013-10-31 16:29:18 -07:00
Alex Gaynor
6b9770b159 write more readably 2013-10-31 16:07:35 -07:00
Alex Gaynor
ce8f9a4e2a A test for roundtripping 2013-10-31 15:23:15 -07:00
Alex Gaynor
c1ea0a0d23 Fixed pep8 issues 2013-10-31 15:03:53 -07:00
Alex Gaynor
38f34557e4 Started working on the invalid cases 2013-10-31 14:50:00 -07:00
Alex Gaynor
fb8adfcb2f Use raw vector files 2013-10-31 14:16:24 -07:00
Alex Gaynor
413bd8b45a py3k syntax fix 2013-10-31 11:22:11 -07:00