From e3c1d010dfe61af907b5ac9afb1468522cecb728 Mon Sep 17 00:00:00 2001 From: mahdavi Date: Thu, 4 Jul 2024 15:52:47 +0330 Subject: [PATCH] client id and client owner in token model --- apps/gooyal_oauth2/validators.py | 33 +++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/apps/gooyal_oauth2/validators.py b/apps/gooyal_oauth2/validators.py index cf6c474..92f5dd1 100755 --- a/apps/gooyal_oauth2/validators.py +++ b/apps/gooyal_oauth2/validators.py @@ -125,6 +125,37 @@ class OAuth2Validator(BaseOAuth2Validator): # pylint: disable=w0223 return None if "active" in content and content["active"] is True: + try: + application_introspection_url = introspection_url.rstrip("/") + "_application/" + response = requests.post(application_introspection_url, data={"client_id": content['client_id']}, headers=headers) + except requests.exceptions.RequestException: + log.exception("Introspection: Failed POST to %r in token lookup", introspection_url) + return None + + if response.status_code != http.client.OK: + log.exception( + "Application introspection: Failed to get a valid response " + "from authentication server. Status code: {}, " + "Reason: {}.".format(response.status_code, response.reason) + ) + return None + + try: + application_introspection_content = response.json() + except ValueError: + log.exception("Introspection: Failed to parse response as json") + return None + + client_owner_value = None + if "active" in application_introspection_content and application_introspection_content["active"] is True: + if "client_owner" in application_introspection_content: + client_owner_value = application_introspection_content["client_owner"] + + if client_owner_value: + client_owner, _ = UserModel.objects.get_or_create(pk=client_owner_value) + else: + client_owner = None + if "username" in content: user = self.get_or_create_user_from_content(content) else: @@ -154,7 +185,7 @@ class OAuth2Validator(BaseOAuth2Validator): # pylint: disable=w0223 defaults={ "user": user, "client_id": content.get("client_id", ""), - "client_owner": user, + "client_owner": client_owner, "application": None, "scope": scope, "expires": expires,