-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
Copy pathgoogle-jwt-client.py
98 lines (77 loc) · 3.02 KB
/
google-jwt-client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python
# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Example of calling a Google Cloud Endpoint API with a JWT signed by
a Google API Service Account."""
import argparse
import time
import google.auth.crypt
import google.auth.jwt
import requests
# [START endpoints_generate_jwt_sa]
def generate_jwt(
sa_keyfile,
sa_email="[email protected]",
audience="your-service-name",
expiry_length=3600,
):
"""Generates a signed JSON Web Token using a Google API Service Account."""
now = int(time.time())
# build payload
payload = {
"iat": now,
# expires after 'expiry_length' seconds.
"exp": now + expiry_length,
# iss must match 'issuer' in the security configuration in your
# swagger spec (e.g. service account email). It can be any string.
"iss": sa_email,
# aud must be either your Endpoints service name, or match the value
# specified as the 'x-google-audience' in the OpenAPI document.
"aud": audience,
# sub and email should match the service account's email address
"sub": sa_email,
"email": sa_email,
}
# sign with keyfile
signer = google.auth.crypt.RSASigner.from_service_account_file(sa_keyfile)
jwt = google.auth.jwt.encode(signer, payload)
return jwt
# [END endpoints_generate_jwt_sa]
# [START endpoints_jwt_request]
def make_jwt_request(signed_jwt, url="https://your-endpoint.com"):
"""Makes an authorized request to the endpoint"""
headers = {
"Authorization": "Bearer {}".format(signed_jwt.decode("utf-8")),
"content-type": "application/json",
}
response = requests.get(url, headers=headers)
print(response.status_code, response.content)
response.raise_for_status()
# [END endpoints_jwt_request]
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument(
"host", help="Your API host, e.g. https://your-project.appspot.com."
)
parser.add_argument("audience", help="The aud entry for the JWT")
parser.add_argument("sa_path", help="The path to your service account json file.")
parser.add_argument("sa_email", help="The email address for the service account.")
args = parser.parse_args()
expiry_length = 3600
keyfile_jwt = generate_jwt(
args.sa_path, args.sa_email, args.audience, expiry_length
)
make_jwt_request(keyfile_jwt, args.host)