|
19 | 19 | import logging
|
20 | 20 | import os
|
21 | 21 |
|
22 |
| -from google.auth import jwt |
23 | 22 | from google.auth.transport import requests
|
24 | 23 | from google.cloud import pubsub_v1
|
25 | 24 | from google.oauth2 import id_token
|
|
38 | 37 | # Global list to store messages, tokens, etc. received by this instance.
|
39 | 38 | MESSAGES = []
|
40 | 39 | TOKENS = []
|
41 |
| -HEADERS = [] |
42 | 40 | CLAIMS = []
|
43 | 41 |
|
44 | 42 | # [START index]
|
45 | 43 | @app.route('/', methods=['GET', 'POST'])
|
46 | 44 | def index():
|
47 | 45 | if request.method == 'GET':
|
48 | 46 | return render_template('index.html', messages=MESSAGES, tokens=TOKENS,
|
49 |
| - headers=HEADERS, claims=CLAIMS) |
| 47 | + claims=CLAIMS) |
50 | 48 |
|
51 | 49 | data = request.form.get('payload', 'Example payload').encode('utf-8')
|
52 | 50 |
|
@@ -74,18 +72,17 @@ def receive_messages_handler():
|
74 | 72 | token = bearer_token.split(' ')[1]
|
75 | 73 | TOKENS.append(token)
|
76 | 74 |
|
77 |
| - header = jwt.decode_header(token) |
78 |
| - HEADERS.append(header) |
79 |
| - |
80 |
| - # Verify and decode the JWT. Underneath it checks the signature against |
81 |
| - # Google's public certs at https://www.googleapis.com/oauth2/v1/certs. |
82 |
| - # It also checks the token expiration time. |
83 |
| - claim = id_token.verify_oauth2_token(token, requests.Request()) |
| 75 | + # Verify and decode the JWT. `verify_oauth2_token` verifies |
| 76 | + # the JWT signature, the `aud` claim, and the `exp` claim. |
| 77 | + claim = id_token.verify_oauth2_token(token, requests.Request(), |
| 78 | + audience='example.com') |
| 79 | + # Must also verify the `iss` claim. |
| 80 | + if claim['iss'] not in [ |
| 81 | + 'accounts.google.com', |
| 82 | + 'https://accounts.google.com' |
| 83 | + ]: |
| 84 | + raise ValueError('Wrong issuer.') |
84 | 85 | CLAIMS.append(claim)
|
85 |
| - |
86 |
| - # Check the audience field in the claim. It was specified in |
87 |
| - # `--push-auth-token-audience` when you created the subscription. |
88 |
| - assert claim['aud'] == 'example.com' |
89 | 86 | except Exception as e:
|
90 | 87 | return 'Invalid token: {}\n'.format(e), 400
|
91 | 88 |
|
|
0 commit comments