def forgot_user(self, email, password_verifier, display_name): try: # delete user, then re create new user with activate status is True old_user_id = self.get_user_by_email(email)["id"] self.delete_user(old_user_id) new_user_id = KeyCloakUtils.create_user(email, email, password_verifier, "", display_name) if new_user_id: KeyCloakUtils.active_user(new_user_id) return new_user_id except Exception as e: logger.info(e) raise Exception(Message.REGISTER_USER_FAILED)
def google_login(self, google_id_token): # google login by using google id token, return user_name, user_id and boolean variable indicate if this user is new user try: verify_id_token_url = "https://oauth2.googleapis.com/tokeninfo?id_token=" + google_id_token req = requests.get(url=verify_id_token_url) if req.status_code != 200: raise Exception(Message.GOOGLE_AUTH_ID_TOKEN_INVALID) google_token_info = req.json() logger.info("Google login token spec:") logger.info(google_token_info) # check google_token_info["aud"] matching with google app id google_app_id = get_system_config()["google_app_id"] if google_token_info["aud"] != google_app_id[ "ios"] and google_token_info["aud"] != google_app_id[ "android"]: raise Exception(Message.GOOGLE_AUTH_FAILED) google_email = google_token_info["email"] # check account exits user = self.get_user_by_email(email=google_email) #active_user if user: if not user["emailVerified"]: KeyCloakUtils.active_user(user["id"]) user_info = UserService().get_user_by_id(user["id"]) return google_email, user[ "id"], user_info.password_verifier is None or user_info.password_verifier == "" else: # create new user new_user_id = KeyCloakUtils.create_user_without_password( google_email, google_email, "", google_token_info["name"]) new_user = UserService().create_user_social( id=new_user_id, email=google_email, display_name=google_token_info["name"], auth_source='google') if new_user is None: self.delete_user(new_user_id) raise Exception(Message.REGISTER_USER_FAILED) return google_email, new_user_id, True except Exception as e: logger.info(e) raise Exception(Message.GOOGLE_AUTH_FAILED)