22
22
23
23
import google .auth .crypt
24
24
import google .auth .jwt
25
- import requests
26
- from six .moves import urllib
27
25
26
+ import requests
28
27
29
- def generate_jwt (service_account_file ):
30
- """Generates a signed JSON Web Token using a Google API Service Account."""
31
28
32
- # Note: this sample shows how to manually create the JWT for the purposes
33
- # of showing how the authentication works, but you can use
34
- # google.auth.jwt.Credentials to automatically create the JWT.
35
- # http://google-auth.readthedocs.io/en/latest/reference
36
- # /google.auth.jwt.html#google.auth.jwt.Credentials
29
+ # [START endpoints_generate_jwt_sa]
30
+ def generate_jwt ( sa_keyfile ,
31
+ sa_email = 'account@project-id.iam.gserviceaccount.com' ,
32
+ audience = 'your-service-name' ,
33
+ expiry_length = 3600 ):
37
34
38
- signer = google .auth .crypt .RSASigner .from_service_account_file (
39
- service_account_file )
35
+ """Generates a signed JSON Web Token using a Google API Service Account."""
40
36
41
37
now = int (time .time ())
42
- expires = now + 3600 # One hour in seconds
43
38
39
+ # build payload
44
40
payload = {
45
41
'iat' : now ,
46
- 'exp' : expires ,
47
- # aud must match 'audience' in the security configuration in your
48
- # swagger spec. It can be any string.
49
- 'aud' : 'echo.endpoints.sample.google.com' ,
42
+ # expires after 'expirary_length' seconds.
43
+ "exp" : now + expiry_length ,
50
44
# iss must match 'issuer' in the security configuration in your
51
45
# swagger spec (e.g. service account email). It can be any string.
52
- 'iss' : 'jwt-client.endpoints.sample.google.com' ,
53
- # sub and email are mapped to the user id and email respectively.
54
- # sub should match 'iss'
55
- 'sub' : 'jwt-client.endpoints.sample.google.com' ,
56
- 'email' : 'user@example.com'
46
+ 'iss' : sa_email ,
47
+ # aud must be either your Endpoints service name, or match the value
48
+ # specified as the 'x-google-audience' in the OpenAPI document.
49
+ 'aud' : audience ,
50
+ # sub and email should match the service account's email address
51
+ 'sub' : sa_email ,
52
+ 'email' : sa_email
57
53
}
58
54
59
- jwt = google .auth .jwt .encode (signer , payload ).decode ('UTF-8' )
55
+ # sign with keyfile
56
+ signer = google .auth .crypt .RSASigner .from_service_account_file (sa_keyfile )
57
+ jwt = google .auth .jwt .encode (signer , payload )
60
58
61
59
return jwt
60
+ # [END endpoints_generate_jwt_sa]
62
61
63
62
64
- def make_request (host , api_key , signed_jwt ):
65
- """Makes a request to the auth info endpoint for Google JWTs."""
66
- url = urllib .parse .urljoin (host , '/auth/info/googlejwt' )
67
- params = {
68
- 'key' : api_key
69
- }
63
+ # [START endpoints_jwt_request]
64
+ def make_jwt_request (signed_jwt , url = 'https://your-endpoint.com' ):
65
+ """Makes an authorized request to the endpoint"""
70
66
headers = {
71
- 'Authorization' : 'Bearer {}' .format (signed_jwt )
67
+ 'Authorization' : 'Bearer {}' .format (signed_jwt ),
68
+ 'content-type' : 'application/json'
72
69
}
73
-
74
- response = requests .get (url , params = params , headers = headers )
70
+ response = requests .get (url , headers = headers )
75
71
76
72
response .raise_for_status ()
77
73
return response .text
78
-
79
-
80
- def main (host , api_key , service_account_file ):
81
- signed_jwt = generate_jwt (service_account_file )
82
- response = make_request (host , api_key , signed_jwt )
83
- print (response )
74
+ # [END endpoints_jwt_request]
84
75
85
76
86
77
if __name__ == '__main__' :
@@ -90,11 +81,19 @@ def main(host, api_key, service_account_file):
90
81
parser .add_argument (
91
82
'host' , help = 'Your API host, e.g. https://your-project.appspot.com.' )
92
83
parser .add_argument (
93
- 'api_key ' , help = 'Your API key. ' )
84
+ 'audience ' , help = 'The aud entry for the JWT ' )
94
85
parser .add_argument (
95
- 'service_account_file ' ,
86
+ 'sa_path ' ,
96
87
help = 'The path to your service account json file.' )
88
+ parser .add_argument (
89
+ 'sa_email' ,
90
+ help = 'The email address for the service account.' )
97
91
98
92
args = parser .parse_args ()
99
93
100
- main (args .host , args .api_key , args .service_account_file )
94
+ expiry_length = 3600
95
+ keyfile_jwt = generate_jwt (args .sa_path ,
96
+ args .sa_email ,
97
+ args .audience ,
98
+ expiry_length )
99
+ print (make_jwt_request (keyfile_jwt , args .host ))
0 commit comments