|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2016 Google Inc. All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""Example of calling a Google Cloud Endpoint API with a JWT signed by |
| 18 | +a Google API Service Account.""" |
| 19 | + |
| 20 | +import argparse |
| 21 | +import time |
| 22 | + |
| 23 | +import oauth2client.crypt |
| 24 | +from oauth2client.service_account import ServiceAccountCredentials |
| 25 | +import requests |
| 26 | +from six.moves import urllib |
| 27 | + |
| 28 | + |
| 29 | +def generate_jwt(service_account_file): |
| 30 | + """Generates a signed JSON Web Token using a Google API Service Account.""" |
| 31 | + credentials = ServiceAccountCredentials.from_json_keyfile_name( |
| 32 | + service_account_file) |
| 33 | + |
| 34 | + now = int(time.time()) |
| 35 | + |
| 36 | + payload = { |
| 37 | + 'iat': now, |
| 38 | + 'exp': now + credentials.MAX_TOKEN_LIFETIME_SECS, |
| 39 | + # aud must match 'audience' in the security configuration in your |
| 40 | + # swagger spec. It can be any string. |
| 41 | + 'aud': 'echo.endpoints.sample.google.com', |
| 42 | + # iss must match 'issuer' in the security configuration in your |
| 43 | + # swagger spec. It can be any string. |
| 44 | + 'iss': 'jwt-client.endpoints.sample.google.com', |
| 45 | + # sub and email are mapped to the user id and email respectively. |
| 46 | + 'sub': '12345678', |
| 47 | + 'email': 'user@example.com' |
| 48 | + } |
| 49 | + |
| 50 | + signed_jwt = oauth2client.crypt.make_signed_jwt( |
| 51 | + credentials._signer, payload, key_id=credentials._private_key_id) |
| 52 | + |
| 53 | + return signed_jwt |
| 54 | + |
| 55 | + |
| 56 | +def make_request(host, api_key, signed_jwt): |
| 57 | + """Makes a request to the auth info endpoint for Google JWTs.""" |
| 58 | + url = urllib.parse.urljoin(host, '/auth/info/googlejwt') |
| 59 | + params = { |
| 60 | + 'key': api_key |
| 61 | + } |
| 62 | + headers = { |
| 63 | + 'Authorization': 'Bearer {}'.format(signed_jwt) |
| 64 | + } |
| 65 | + |
| 66 | + response = requests.get(url, params=params, headers=headers) |
| 67 | + |
| 68 | + response.raise_for_status() |
| 69 | + return response.text |
| 70 | + |
| 71 | + |
| 72 | +def main(host, api_key, service_account_file): |
| 73 | + signed_jwt = generate_jwt(service_account_file) |
| 74 | + response = make_request(host, api_key, signed_jwt) |
| 75 | + print(response) |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == '__main__': |
| 79 | + parser = argparse.ArgumentParser( |
| 80 | + description=__doc__, |
| 81 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 82 | + parser.add_argument( |
| 83 | + 'host', help='Your API host, e.g. https://your-project.appspot.com.') |
| 84 | + parser.add_argument( |
| 85 | + 'api_key', help='Your API key.') |
| 86 | + parser.add_argument( |
| 87 | + 'service_account_file', |
| 88 | + help='The path to your service account json file.') |
| 89 | + |
| 90 | + args = parser.parse_args() |
| 91 | + |
| 92 | + main(args.host, args.api_key, args.service_account_file) |
0 commit comments