|
59 | 59 | """ |
60 | 60 |
|
61 | 61 | import json |
| 62 | +import os |
62 | 63 |
|
| 64 | +import six |
63 | 65 | from six.moves import http_client |
64 | 66 |
|
| 67 | +from google.auth import environment_vars |
65 | 68 | from google.auth import exceptions |
66 | 69 | from google.auth import jwt |
67 | 70 |
|
| 71 | + |
68 | 72 | # The URL that provides public certificates for verifying ID tokens issued |
69 | 73 | # by Google's OAuth 2.0 authorization server. |
70 | 74 | _GOOGLE_OAUTH2_CERTS_URL = "https://www.googleapis.com/oauth2/v1/certs" |
@@ -159,3 +163,90 @@ def verify_firebase_token(id_token, request, audience=None): |
159 | 163 | return verify_token( |
160 | 164 | id_token, request, audience=audience, certs_url=_GOOGLE_APIS_CERTS_URL |
161 | 165 | ) |
| 166 | + |
| 167 | + |
| 168 | +def fetch_id_token(request, audience): |
| 169 | + """Fetch the ID Token from the current environment. |
| 170 | +
|
| 171 | + This function acquires ID token from the environment in the following order: |
| 172 | +
|
| 173 | + 1. If the application is running in Compute Engine, App Engine or Cloud Run, |
| 174 | + then the ID token are obtained from the metadata server. |
| 175 | + 2. If the environment variable ``GOOGLE_APPLICATION_CREDENTIALS`` is set |
| 176 | + to the path of a valid service account JSON file, then ID token is |
| 177 | + acquired using this service account credentials. |
| 178 | + 3. If metadata server doesn't exist and no valid service account credentials |
| 179 | + are found, :class:`~google.auth.exceptions.DefaultCredentialsError` will |
| 180 | + be raised. |
| 181 | +
|
| 182 | + Example:: |
| 183 | +
|
| 184 | + import google.oauth2.id_token |
| 185 | + import google.auth.transport.requests |
| 186 | +
|
| 187 | + request = google.auth.transport.requests.Request() |
| 188 | + target_audience = "https://pubsub.googleapis.com" |
| 189 | +
|
| 190 | + id_token = google.oauth2.id_token.fetch_id_token(request, target_audience) |
| 191 | +
|
| 192 | + Args: |
| 193 | + request (google.auth.transport.Request): A callable used to make |
| 194 | + HTTP requests. |
| 195 | + audience (str): The audience that this ID token is intended for. |
| 196 | +
|
| 197 | + Returns: |
| 198 | + str: The ID token. |
| 199 | +
|
| 200 | + Raises: |
| 201 | + ~google.auth.exceptions.DefaultCredentialsError: |
| 202 | + If metadata server doesn't exist and no valid service account |
| 203 | + credentials are found. |
| 204 | + """ |
| 205 | + # 1. First try to fetch ID token from metada server if it exists. The code |
| 206 | + # works for GAE and Cloud Run metadata server as well. |
| 207 | + try: |
| 208 | + from google.auth import compute_engine |
| 209 | + |
| 210 | + credentials = compute_engine.IDTokenCredentials( |
| 211 | + request, audience, use_metadata_identity_endpoint=True |
| 212 | + ) |
| 213 | + credentials.refresh(request) |
| 214 | + return credentials.token |
| 215 | + except (ImportError, exceptions.TransportError): |
| 216 | + pass |
| 217 | + |
| 218 | + # 2. Try to use service account credentials to get ID token. |
| 219 | + |
| 220 | + # Try to get credentials from the GOOGLE_APPLICATION_CREDENTIALS environment |
| 221 | + # variable. |
| 222 | + credentials_filename = os.environ.get(environment_vars.CREDENTIALS) |
| 223 | + if not ( |
| 224 | + credentials_filename |
| 225 | + and os.path.exists(credentials_filename) |
| 226 | + and os.path.isfile(credentials_filename) |
| 227 | + ): |
| 228 | + raise exceptions.DefaultCredentialsError( |
| 229 | + "Neither metadata server or valid service account credentials are found." |
| 230 | + ) |
| 231 | + |
| 232 | + try: |
| 233 | + with open(credentials_filename, "r") as f: |
| 234 | + info = json.load(f) |
| 235 | + credentials_content = ( |
| 236 | + (info.get("type") == "service_account") and info or None |
| 237 | + ) |
| 238 | + |
| 239 | + from google.oauth2 import service_account |
| 240 | + |
| 241 | + credentials = service_account.IDTokenCredentials.from_service_account_info( |
| 242 | + credentials_content, target_audience=audience |
| 243 | + ) |
| 244 | + except ValueError as caught_exc: |
| 245 | + new_exc = exceptions.DefaultCredentialsError( |
| 246 | + "Neither metadata server or valid service account credentials are found.", |
| 247 | + caught_exc, |
| 248 | + ) |
| 249 | + six.raise_from(new_exc, caught_exc) |
| 250 | + |
| 251 | + credentials.refresh(request) |
| 252 | + return credentials.token |
0 commit comments