From 64d7e83d15f1b52bc91916d00f84ca8b6735731a Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Wed, 11 May 2022 12:54:06 -0400 Subject: [PATCH] Add __eq__ function for JWS, JWE, JWT Signed-off-by: Simo Sorce --- jwcrypto/jwe.py | 12 ++++++++++++ jwcrypto/jws.py | 8 ++++++++ jwcrypto/jwt.py | 7 +++++++ 3 files changed, 27 insertions(+) diff --git a/jwcrypto/jwe.py b/jwcrypto/jwe.py index cbe64a1..d3a271a 100644 --- a/jwcrypto/jwe.py +++ b/jwcrypto/jwe.py @@ -525,6 +525,18 @@ def from_jose_token(cls, token): obj.deserialize(token) return obj + def __eq__(self, other): + if not isinstance(other, JWE): + return False + try: + return self.serialize() == other.serialize() + except Exception: # pylint: disable=broad-except + a = {'plaintext': self.plaintext} + a.update(self.objects) + b = {'plaintext': other.plaintext} + b.update(other.objects) + return a == b + def __str__(self): try: return self.serialize() diff --git a/jwcrypto/jws.py b/jwcrypto/jws.py index c3aba23..1e1c686 100644 --- a/jwcrypto/jws.py +++ b/jwcrypto/jws.py @@ -646,6 +646,14 @@ def from_jose_token(cls, token): obj.deserialize(token) return obj + def __eq__(self, other): + if not isinstance(other, JWS): + return False + try: + return self.serialize() == other.serialize() + except Exception: # pylint: disable=broad-except + return self.objects == other.objects + def __str__(self): try: return self.serialize() diff --git a/jwcrypto/jwt.py b/jwcrypto/jwt.py index 187e2c7..ff8c899 100644 --- a/jwcrypto/jwt.py +++ b/jwcrypto/jwt.py @@ -580,6 +580,13 @@ def from_jose_token(cls, token): obj.deserialize(token) return obj + def __eq__(self, other): + if not isinstance(other, JWT): + return False + return self._claims == other._claims and \ + self._header == other._header and \ + self.token == other.token + def __str__(self): try: return self.serialize()