Skip to content

Commit

Permalink
Improve documentation for audience usage (#484)
Browse files Browse the repository at this point in the history
* Add code example for `aud` being an array

The previous code example only showed the `aud` claim as a single
case-sensitive string, despite the documentation mentioning that the
`aud` claim can be an array of case-sensitive strings

Add a code block demonstrating the `aud` claim being an array of
case-sensitive strings to make it more clear to the user that it is a
permitted use of the `aud` claim

* Add example of the `audience` param as an iterable

Demonstrate to users reading the documentation that the `audience`
parameter is not restricted to the `string` type, but can also accept an
iterable, as implemented in PR#306

jpadilla/pyjwt#306

* Fix short title underlines

Short title underlines throw warnings in reStructuredText linters
  • Loading branch information
rylanhall33 committed Apr 26, 2020
1 parent 6d023d4 commit 44fa87f
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Usage Examples
==============

Encoding & Decoding Tokens with HS256
---------------------------------
--------------------------------------

.. code-block:: python
Expand All @@ -14,7 +14,7 @@ Encoding & Decoding Tokens with HS256
{'some': 'payload'}
Encoding & Decoding Tokens with RS256 (RSA)
---------------------------------
-------------------------------------------

.. code-block:: python
Expand Down Expand Up @@ -183,12 +183,23 @@ Audience Claim (aud)
identify itself with a value in the audience claim. If the principal
processing the claim does not identify itself with a value in the
"aud" claim when this claim is present, then the JWT MUST be
rejected. In the general case, the "aud" value is an array of case-
sensitive strings, each containing a StringOrURI value. In the
special case when the JWT has one audience, the "aud" value MAY be a
single case-sensitive string containing a StringOrURI value. The
interpretation of audience values is generally application specific.
Use of this claim is OPTIONAL.
rejected.

In the general case, the "aud" value is an array of case-
sensitive strings, each containing a StringOrURI value.

.. code-block:: python
payload = {
'some': 'payload',
'aud': ['urn:foo', 'urn:bar']
}
token = jwt.encode(payload, 'secret')
decoded = jwt.decode(token, 'secret', audience='urn:foo', algorithms=['HS256'])
In the special case when the JWT has one audience, the "aud" value MAY be
a single case-sensitive string containing a StringOrURI value.

.. code-block:: python
Expand All @@ -200,6 +211,22 @@ Audience Claim (aud)
token = jwt.encode(payload, 'secret')
decoded = jwt.decode(token, 'secret', audience='urn:foo', algorithms=['HS256'])
If multiple audiences are accepted, the ``audience`` parameter for
``jwt.decode`` can also be an iterable

.. code-block:: python
payload = {
'some': 'payload',
'aud': 'urn:foo'
}
token = jwt.encode(payload, 'secret')
decoded = jwt.decode(token, 'secret', audience=['urn:foo', 'urn:bar'], algorithms=['HS256'])
The interpretation of audience values is generally application specific.
Use of this claim is OPTIONAL.

If the audience claim is incorrect, `jwt.InvalidAudienceError` will be raised.

Issued At Claim (iat)
Expand Down

0 comments on commit 44fa87f

Please sign in to comment.