|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
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 | +""" |
16 | 60 |
|
17 | 61 | import json |
18 | 62 |
|
|
0 commit comments