Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PyJWKSet exposes an iterator but you cannot iterate it. #1024

Open
Kyle-sandeman-mrdfood opened this issue Dec 2, 2024 · 3 comments
Open
Labels

Comments

@Kyle-sandeman-mrdfood
Copy link

Hi, in #724 __getitem__ was introduced which allows jwkset['my key id'] which is great!
However one cannot do e.g. keys_using_hmac = [k for k in jwkset if ...] or any other form of iteration.
I believe __iter__ needs to be implemented as python will treat any class with __getitem__ as iterable.

Expected Result

Example use-case: this should print all the kids

for key in jwkset:
  print(key.key_id)

Actual Result

KeyError: keyset has no key for kid: 0

Reproduction Steps

  1. create a PyJWKSet instance
  2. try iterate the instance

System Information

Can provide if required, I don't think anything more than pyjwt 2.10.0 is relevant as the code in the aforementioned issue is agnostic to the rest.

Proposed resolution

I'm no python expert, but from the docs and the below example, I believe implementing __iter__ is all that is required.

>>> class Test:
...   def __getitem__(self, k):
...     print(type(k), k)
...     raise KeyError('not implemented')
... 
>>> t = Test()
>>> for item in t:
...   print('got an item:', item)
... 
<class 'int'> 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in __getitem__
KeyError: 'not implemented'

I have confirmed this code resolves the error

def __getitem__(...
   # original PyJWKSet impl.

def __iter__(self):
  return iter(self.keys)

I could make a PR later today but I am sure an existing collaborator would be able to get it into a release sooner.

Copy link

github-actions bot commented Feb 1, 2025

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days

@github-actions github-actions bot added the stale Issues without activity for more than 60 days label Feb 1, 2025
@jpadilla jpadilla added keep and removed stale Issues without activity for more than 60 days labels Feb 3, 2025
pachewise added a commit to pachewise/pyjwt that referenced this issue Feb 23, 2025
@pachewise
Copy link
Contributor

opened a draft PR for this, it seems straightforward but not sure if the maintainers want this functinality, or if they have some concerns

auvipy pushed a commit that referenced this issue Feb 26, 2025
* Feat #1024: Add iterator for PyJWKSet

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix mypy type

* changelog

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
@pachewise
Copy link
Contributor

@auvipy this should have been closed by #1041 , but I guess the naming wasn't correct

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants