|
| 1 | +import requests |
| 2 | +from google.auth.transport import requests as google_requests |
| 3 | +from google.oauth2 import id_token |
| 4 | +from models.User import User |
| 5 | +from utils.oidc import update_oidc_info |
| 6 | +from constants.environment_variables import ( |
| 7 | + OIDC_GOOGLE_CLIENT_ID, |
| 8 | + OIDC_GOOGLE_CLIENT_SECRET, |
| 9 | + OIDC_GOOGLE_REDIRECT_URI, |
| 10 | + GOOGLE_TOKEN_URL, |
| 11 | + GOOGLE_USER_INFO_URL, |
| 12 | + HTTP_REQUEST_TIMEOUT |
| 13 | +) |
| 14 | + |
| 15 | +def get_user_infos_from_google_token_url(code): |
| 16 | + token_data = { |
| 17 | + "code": code, |
| 18 | + "client_id": OIDC_GOOGLE_CLIENT_ID, |
| 19 | + "client_secret": OIDC_GOOGLE_CLIENT_SECRET, |
| 20 | + "redirect_uri": OIDC_GOOGLE_REDIRECT_URI, |
| 21 | + "grant_type": "authorization_code", |
| 22 | + } |
| 23 | + |
| 24 | + token_response = requests.post(GOOGLE_TOKEN_URL, data=token_data, timeout=HTTP_REQUEST_TIMEOUT) |
| 25 | + access_token = token_response.json().get("access_token") |
| 26 | + |
| 27 | + user_infos_response = requests.get(GOOGLE_USER_INFO_URL, headers={"Authorization": f"Bearer {access_token}"}, timeout=HTTP_REQUEST_TIMEOUT) |
| 28 | + user_infos = user_infos_response.json() |
| 29 | + |
| 30 | + if not user_infos: |
| 31 | + return { |
| 32 | + "status": False, |
| 33 | + "user_infos": user_infos |
| 34 | + } |
| 35 | + |
| 36 | + return { |
| 37 | + "status": True, |
| 38 | + "user_infos": user_infos |
| 39 | + } |
| 40 | + |
| 41 | +def get_user_infos_from_google_token(id_token_str): |
| 42 | + id_info = id_token.verify_oauth2_token(id_token_str, google_requests.Request(), OIDC_GOOGLE_CLIENT_ID) |
| 43 | + user_id = id_info['sub'] |
| 44 | + email = id_info.get('email') |
| 45 | + |
| 46 | + user_infos = { |
| 47 | + 'id': user_id, |
| 48 | + 'email': email, |
| 49 | + } |
| 50 | + |
| 51 | + if not user_infos: |
| 52 | + return { |
| 53 | + "status": False, |
| 54 | + "user_infos": user_infos |
| 55 | + } |
| 56 | + |
| 57 | + return { |
| 58 | + "status": True, |
| 59 | + "user_infos": user_infos |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | +def create_user(user_infos, db): |
| 64 | + user_exists = db.query(User).filter(User.email == user_infos['email']).first() |
| 65 | + |
| 66 | + if user_exists: |
| 67 | + got_updated = update_oidc_info( |
| 68 | + user_exists.id, |
| 69 | + 'google', |
| 70 | + user_infos['id'], |
| 71 | + user_infos['email'], |
| 72 | + db |
| 73 | + ) |
| 74 | + if got_updated is False: |
| 75 | + return { |
| 76 | + "status": False, |
| 77 | + "message": "Failed to update user" |
| 78 | + } |
| 79 | + |
| 80 | + return { |
| 81 | + "status": True, |
| 82 | + "user": user_exists |
| 83 | + } |
| 84 | + |
| 85 | + new_user = User( |
| 86 | + email=user_infos['email'], |
| 87 | + oidc_configs=[{ |
| 88 | + "provider": "google", |
| 89 | + "id": user_infos['id'], |
| 90 | + "email": user_infos['email'] |
| 91 | + }] |
| 92 | + ) |
| 93 | + new_user.save(db) |
| 94 | + return { |
| 95 | + "status": True, |
| 96 | + "user": new_user |
| 97 | + } |
0 commit comments