Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a1b5e56

Browse filesBrowse files
qiwzhangJon Wayne Parrott
authored andcommitted
Add some files for grpc auth tutorial (GoogleCloudPlatform#712)
* Add api config_auth, and rename http_api_config to api_config_http. * Use google.auth for jwt_token_gen.py. * updated README.
1 parent b96ec93 commit a1b5e56
Copy full SHA for a1b5e56

File tree

Expand file treeCollapse file tree

5 files changed

+128
-0
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+128
-0
lines changed

‎endpoints/bookstore-grpc/README.md

Copy file name to clipboardExpand all lines: endpoints/bookstore-grpc/README.md
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ To run the client:
2828
As with the server, the `-h` command line flag shows the various settings
2929
available.
3030

31+
## Generating a JWT token from a service account file
32+
33+
To run the script:
34+
35+
python jwt_token_gen.py --file=account_file --audiences=audiences --issuer=issuer
36+
37+
The output can be used as "--auth_token" for bookstore_client.py
38+
3139
## Regenerating the API stubs
3240

3341
The bookstore gRPC API is defined by `bookstore.proto`
+48Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
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+
#
16+
# A Bookstore example API configuration.
17+
#
18+
# Below, replace MY_PROJECT_ID with your Google Cloud Project ID.
19+
#
20+
21+
# The configuration schema is defined by service.proto file
22+
# https://github.com/googleapis/googleapis/blob/master/google/api/service.proto
23+
type: google.api.Service
24+
config_version: 3
25+
26+
#
27+
# Name of the service configuration.
28+
#
29+
name: bookstore.MY_PROJECT_ID.appspot.com
30+
31+
#
32+
# API title to appear in the user interface (Google Cloud Console).
33+
#
34+
title: Bookstore gRPC API
35+
apis:
36+
- name: endpoints.examples.bookstore.Bookstore
37+
38+
authentication:
39+
providers:
40+
- id: google_service_account
41+
# Replace SERVICE-ACCOUNT-EMAIL with your service account's email address.
42+
issuer: SERVICE-ACCOUNT-EMAI
43+
jwks_uri: https://www.googleapis.com/robot/v1/metadata/x509/SERVICE-ACCOUNT-EMAI
44+
rules:
45+
# This auth rule will apply to all methods.
46+
- selector: "*"
47+
requirements:
48+
- provider_id: google_service_account
+71Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 generateing a JWT signed from a service account file."""
18+
19+
import argparse
20+
import json
21+
import time
22+
23+
import google.auth.crypt
24+
import google.auth.jwt
25+
26+
"""Max lifetime of the token (one hour, in seconds)."""
27+
MAX_TOKEN_LIFETIME_SECS = 3600
28+
29+
30+
def generate_jwt(service_account_file, issuer, audiences):
31+
"""Generates a signed JSON Web Token using a Google API Service Account."""
32+
with open(service_account_file, 'r') as fh:
33+
service_account_info = json.load(fh)
34+
35+
signer = google.auth.crypt.Signer.from_string(
36+
service_account_info['private_key'],
37+
service_account_info['private_key_id'])
38+
39+
now = int(time.time())
40+
41+
payload = {
42+
'iat': now,
43+
'exp': now + MAX_TOKEN_LIFETIME_SECS,
44+
# aud must match 'audience' in the security configuration in your
45+
# swagger spec. It can be any string.
46+
'aud': audiences,
47+
# iss must match 'issuer' in the security configuration in your
48+
# swagger spec. It can be any string.
49+
'iss': issuer,
50+
# sub and email are mapped to the user id and email respectively.
51+
'sub': '12345678',
52+
'email': 'user@example.com'
53+
}
54+
55+
signed_jwt = google.auth.jwt.encode(signer, payload)
56+
return signed_jwt
57+
58+
59+
if __name__ == '__main__':
60+
parser = argparse.ArgumentParser(
61+
description=__doc__,
62+
formatter_class=argparse.RawDescriptionHelpFormatter)
63+
parser.add_argument('--file',
64+
help='The path to your service account json file.')
65+
parser.add_argument('--issuer', default='', help='issuer')
66+
parser.add_argument('--audiences', default='', help='audiences')
67+
68+
args = parser.parse_args()
69+
70+
signed_jwt = generate_jwt(args.file, args.issuer, args.audiences)
71+
print(signed_jwt)
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
grpcio==1.0.1
2+
google-auth==0.3.1

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.