|
| 1 | +# Copyright 2017 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 | +import platform |
| 16 | + |
| 17 | +import flask |
| 18 | + |
| 19 | +import validate_jwt <
8000
/code> |
| 20 | + |
| 21 | +CLOUD_PROJECT_ID = 'YOUR_PROJECT_ID' |
| 22 | +BACKEND_SERVICE_ID = 'YOUR_BACKEND_SERVICE_ID' |
| 23 | + |
| 24 | +app = flask.Flask(__name__) |
| 25 | + |
| 26 | + |
| 27 | +@app.route('/') |
| 28 | +def root(): |
| 29 | + jwt = flask.request.headers.get('x-goog-iap-jwt-assertion') |
| 30 | + if jwt is None: |
| 31 | + return 'Unauthorized request.' |
| 32 | + user_id, user_email, error_str = ( |
| 33 | + validate_jwt.validate_iap_jwt_from_compute_engine( |
| 34 | + jwt, CLOUD_PROJECT_ID, BACKEND_SERVICE_ID)) |
| 35 | + if error_str: |
| 36 | + return 'Error: {}'.format(error_str) |
| 37 | + else: |
| 38 | + return 'Hi, {}. I am {}.'.format(user_email, platform.node()) |
| 39 | + |
| 40 | + |
| 41 | +if __name__ == '__main__': |
| 42 | + app.run() |
0 commit comments