perf: speed up oauth bearer token query (#29184)

* perf: index user and make not_nullable

* refactor: simpler expiry setting

* perf: don't fetch all tokens
This commit is contained in:
Ankush Menat 2025-01-15 18:54:46 +05:30 committed by GitHub
parent 5874d6bf12
commit 4116a33560
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 10 additions and 7 deletions

View file

@ -30,8 +30,10 @@
"fieldname": "user",
"fieldtype": "Link",
"label": "User",
"not_nullable": 1,
"options": "User",
"read_only": 1
"read_only": 1,
"search_index": 1
},
{
"fieldname": "scopes",
@ -75,7 +77,7 @@
}
],
"links": [],
"modified": "2024-03-23 16:03:32.559867",
"modified": "2025-01-15 17:54:35.661941",
"modified_by": "Administrator",
"module": "Integrations",
"name": "OAuth Bearer Token",

View file

@ -3,6 +3,7 @@
import frappe
from frappe.model.document import Document
from frappe.utils.data import add_to_date
class OAuthBearerToken(Document):
@ -21,11 +22,9 @@ class OAuthBearerToken(Document):
refresh_token: DF.Data | None
scopes: DF.Text | None
status: DF.Literal["Active", "Revoked"]
user: DF.Link | None
user: DF.Link
# end: auto-generated types
def validate(self):
if not self.expiration_time:
self.expiration_time = frappe.utils.datetime.datetime.strptime(
self.creation, "%Y-%m-%d %H:%M:%S.%f"
) + frappe.utils.datetime.timedelta(seconds=self.expires_in)
self.expiration_time = add_to_date(self.creation, seconds=self.expires_in, as_datetime=True)

View file

@ -93,7 +93,9 @@ def authorize(**kwargs):
frappe.flags.oauth_credentials["client_id"],
"skip_authorization",
)
unrevoked_tokens = frappe.get_all("OAuth Bearer Token", filters={"status": "Active"})
unrevoked_tokens = frappe.db.exists(
"OAuth Bearer Token", {"status": "Active", "user": frappe.session.user}
)
if skip_auth or (get_oauth_settings().skip_authorization == "Auto" and unrevoked_tokens):
frappe.local.response["type"] = "redirect"