Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace python-jose with google alternative #566

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 15 additions & 31 deletions authenticating-users/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,9 @@
from flask import Flask
app = Flask(__name__)

CERTS = None
AUDIENCE = None


# [START getting_started_auth_certs]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jipje can you let me know about why you're removing this block here? Thanks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I answered my own question. I need to fix some stuff in documentation because removing this block will break a page. )

def certs():
"""Returns a dictionary of current Google public key certificates for
validating Google-signed JWTs. Since these change rarely, the result
is cached on first request for faster subsequent responses.
"""
import requests

global CERTS
if CERTS is None:
response = requests.get(
'https://www.gstatic.com/iap/verify/public_key'
)
CERTS = response.json()
return CERTS
# [END getting_started_auth_certs]


# [START getting_started_auth_metadata]
def get_metadata(item_name):
"""Returns a string with the project metadata value for the item_name.
Expand Down Expand Up @@ -77,26 +58,29 @@ def audience():
# [END getting_started_auth_audience]


# [START getting_started_auth_validate_assertion]
def validate_assertion(assertion):
# [START iap_validate_jwt]
def validate_iap_jwt(iap_jwt) -> tuple[str, str]:
"""Checks that the JWT assertion is valid (properly signed, for the
correct audience) and if so, returns strings for the requesting user's
email and a persistent user ID. If not valid, returns None for each field.

Source: https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/iap/validate_jwt.py
"""
from jose import jwt
from google.auth.transport import requests as google_auth_requests
from google.oauth2 import id_token

try:
info = jwt.decode(
assertion,
certs(),
algorithms=['ES256'],
audience=audience()
)
return info['email'], info['sub']
decoded_jwt = id_token.verify_token(
iap_jwt,
google_auth_requests.Request(),
audience=audience(),
certs_url="https://www.gstatic.com/iap/verify/public_key",
)
return decoded_jwt["email"], decoded_jwt["sub"]
except Exception as e:
print('Failed to validate assertion: {}'.format(e), file=sys.stderr)
return None, None
# [END getting_started_auth_validate_assertion]
# [END iap_validate_jwt]


# [START getting_started_auth_front_controller]
Expand All @@ -105,7 +89,7 @@ def say_hello():
from flask import request

assertion = request.headers.get('X-Goog-IAP-JWT-Assertion')
email, id = validate_assertion(assertion)
email, id = validate_iap_jwt(assertion)
page = "<h1>Hello {}</h1>".format(email)
return page
# [END getting_started_auth_front_controller]
Expand Down
2 changes: 1 addition & 1 deletion authenticating-users/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def fake_validate(assertion):
return None, None


main.validate_assertion = fake_validate
main.validate_iap_jwt = fake_validate


def test_home_page():
Expand Down
3 changes: 2 additions & 1 deletion authenticating-users/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# [START getting_started_requirements]
Flask==2.2.5
cryptography==41.0.2
python-jose[cryptography]==3.3.0
google-auth~=2.19.1
google-cloud-iam~=2.3.0
requests==2.31.0
# [END getting_started_requirements]