Skip to content

Commit

Permalink
Merge pull request #14 from rwth-iat/http_api/improve_base64url_decoding
Browse files Browse the repository at this point in the history
adapter.http: fix base64 decoding without padding
  • Loading branch information
jkhsjdhjs authored Mar 15, 2024
2 parents 107f158 + a9975f7 commit 9a22020
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion basyx/aas/adapter/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,11 @@ def to_url(self, value: model.Identifier) -> str:
def to_python(self, value: str) -> model.Identifier:
value = super().to_python(value)
try:
decoded = base64.urlsafe_b64decode(super().to_python(value)).decode(self.encoding)
# If the requester omits the base64 padding, an exception will be raised.
# However, Python doesn't complain about too much padding,
# thus we simply always append two padding characters (==).
# See also: https://stackoverflow.com/a/49459036/4780052
decoded = base64.urlsafe_b64decode(super().to_python(value) + "==").decode(self.encoding)
except binascii.Error:
raise BadRequest(f"Encoded identifier {value} is invalid base64url!")
except UnicodeDecodeError:
Expand Down

0 comments on commit 9a22020

Please sign in to comment.