Skip to content

Commit

Permalink
Add __eq__ function for JWS, JWE, JWT
Browse files Browse the repository at this point in the history
Signed-off-by: Simo Sorce <simo@redhat.com>
  • Loading branch information
simo5 committed May 11, 2022
1 parent 78104fc commit 0073c16
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
12 changes: 12 additions & 0 deletions jwcrypto/jwe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 8 additions & 0 deletions jwcrypto/jws.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions jwcrypto/jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 0073c16

Please sign in to comment.