blob: 968f2714273022f5fed3c215872cd097027df347 [file] [log] [blame]
Jon Wayne Parrotte2ab1002016-11-10 15:11:48 -08001# Copyright 2016 Google Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Google ID Token helpers."""
16
17import json
18
19from six.moves import http_client
20
21from google.auth import exceptions
22from google.auth import jwt
23
24# The URL that provides public certificates for verifying ID tokens issued
25# by Google's OAuth 2.0 authorization server.
26_GOOGLE_OAUTH2_CERTS_URL = 'https://www.googleapis.com/oauth2/v1/certs'
27
28# The URL that provides public certificates for verifying ID tokens issued
29# by Firebase and the Google APIs infrastructure
30_GOOGLE_APIS_CERTS_URL = (
31 'https://www.googleapis.com/robot/v1/metadata/x509'
32 '/securetoken@system.gserviceaccount.com')
33
34
35def _fetch_certs(request, certs_url):
36 """Fetches certificates.
37
38 Google-style cerificate endpoints return JSON in the format of
39 ``{'key id': 'x509 certificate'}``.
40
41 Args:
42 request (google.auth.transport.Request): The object used to make
43 HTTP requests.
44 certs_url (str): The certificate endpoint URL.
45
46 Returns:
47 Mapping[str, str]: A mapping of public key ID to x.509 certificate
48 data.
49 """
50 response = request('GET', certs_url)
51
52 if response.status != http_client.OK:
53 raise exceptions.TransportError(
54 'Could not fetch certificates at {}'.format(certs_url))
55
56 return json.loads(response.data.decode('utf-8'))
57
58
59def verify_token(id_token, request, audience=None,
60 certs_url=_GOOGLE_OAUTH2_CERTS_URL):
61 """Verifies an ID token and returns the decoded token.
62
63 Args:
64 id_token (Union[str, bytes]): The encoded token.
65 request (google.auth.transport.Request): The object used to make
66 HTTP requests.
67 audience (str): The audience that this token is intended for. If None
68 then the audience is not verified.
69 certs_url (str): The URL that specifies the certificates to use to
70 verify the token. This URL should return JSON in the format of
71 ``{'key id': 'x509 certificate'}``.
72
73 Returns:
74 Mapping[str, Any]: The decoded token.
75 """
76 certs = _fetch_certs(request, certs_url)
77
78 return jwt.decode(id_token, certs=certs, audience=audience)
79
80
81def verify_oauth2_token(id_token, request, audience=None):
82 """Verifies an ID Token issued by Google's OAuth 2.0 authorization server.
83
84 Args:
85 id_token (Union[str, bytes]): The encoded token.
86 request (google.auth.transport.Request): The object used to make
87 HTTP requests.
88 audience (str): The audience that this token is intended for. This is
89 typically your application's OAuth 2.0 client ID. If None then the
90 audience is not verified.
91
92 Returns:
93 Mapping[str, Any]: The decoded token.
94 """
95 return verify_token(
96 id_token, request, audience=audience,
97 certs_url=_GOOGLE_OAUTH2_CERTS_URL)
98
99
100def verify_firebase_token(id_token, request, audience=None):
101 """Verifies an ID Token issued by Firebase Authentication.
102
103 Args:
104 id_token (Union[str, bytes]): The encoded token.
105 request (google.auth.transport.Request): The object used to make
106 HTTP requests.
107 audience (str): The audience that this token is intended for. This is
108 typically your Firebase application ID. If None then the audience
109 is not verified.
110
111 Returns:
112 Mapping[str, Any]: The decoded token.
113 """
114 return verify_token(
115 id_token, request, audience=audience, certs_url=_GOOGLE_APIS_CERTS_URL)