Skip to content

Commit e40c301

Browse files
ajaskaJon Wayne Parrott
authored andcommitted
Improve documentation around ID Tokens (#224)
1 parent bf8ee7f commit e40c301

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

packages/google-auth/docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ also provides integration with several HTTP libraries.
1414

1515
- Support for Google :func:`Application Default Credentials <google.auth.default>`.
1616
- Support for signing and verifying :mod:`JWTs <google.auth.jwt>`.
17+
- Support for verifying and decoding :mod:`ID Tokens <google.oauth2.id_token>`.
1718
- Support for Google :mod:`Service Account credentials <google.oauth2.service_account>`.
1819
- Support for :mod:`Google Compute Engine credentials <google.auth.compute_engine>`.
1920
- Support for :mod:`Google App Engine standard credentials <google.auth.app_engine>`.

packages/google-auth/google/oauth2/id_token.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,51 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
"""Google ID Token helpers."""
15+
"""Google ID Token helpers.
16+
17+
Provides support for verifying `OpenID Connect ID Tokens`_, especially ones
18+
generated by Google infrastructure.
19+
20+
To parse and verify an ID Token issued by Google's OAuth 2.0 authorization
21+
server use :func:`verify_oauth2_token`. To verify an ID Token issued by
22+
Firebase, use :func:`verify_firebase_token`.
23+
24+
A general purpose ID Token verifier is available as :func:`verify_token`.
25+
26+
Example::
27+
28+
from google.oauth2 import id_token
29+
from google.auth.transport import requests
30+
31+
request = requests.Request()
32+
33+
id_info = id_token.verify_oauth2_token(
34+
token, request, 'my-client-id.example.com')
35+
36+
if id_info['iss'] != 'https://accounts.google.com':
37+
raise ValueError('Wrong issuer.')
38+
39+
userid = id_info['sub']
40+
41+
By default, this will re-fetch certificates for each verification. Because
42+
Google's public keys are only changed infrequently (on the order of once per
43+
day), you may wish to take advantage of caching to reduce latency and the
44+
potential for network errors. This can be accomplished using an external
45+
library like `CacheControl`_ to create a cache-aware
46+
:class:`google.auth.transport.Request`::
47+
48+
import cachecontrol
49+
import google.auth.transport.requests
50+
import requests
51+
52+
session = requests.session()
53+
cached_session = cachecontrol.CacheControl(session)
54+
request = google.auth.transport.requests.Request(session=cached_session)
55+
56+
.. _OpenID Connect ID Token:
57+
http://openid.net/specs/openid-connect-core-1_0.html#IDToken
58+
.. _CacheControl: https://cachecontrol.readthedocs.io
59+
"""
1660

1761
import json
1862

0 commit comments

Comments
 (0)