fix(push_notification): improve check before returning token
Co-authored-by: Sagar Vora <16315650+sagarvora@users.noreply.github.com>
This commit is contained in:
parent
c22a1937b5
commit
5a1211ae9e
1 changed files with 9 additions and 14 deletions
|
|
@ -203,16 +203,16 @@ class PushNotification:
|
||||||
|
|
||||||
# Generate new credentials
|
# Generate new credentials
|
||||||
token = frappe.generate_hash(length=48)
|
token = frappe.generate_hash(length=48)
|
||||||
|
secret = frappe.generate_hash(length=32)
|
||||||
|
|
||||||
# store the token in the redis cache
|
# store the token in the redis cache
|
||||||
frappe.cache().set_value(
|
frappe.cache.set_value(f"push_relay_registration_token:{secret}", token, expires_in_sec=600)
|
||||||
f"{self._site_name}:push_relay_registration_token", token, expires_in_sec=600
|
|
||||||
)
|
|
||||||
body = {
|
body = {
|
||||||
"endpoint": self._site_name,
|
"endpoint": self._site_name,
|
||||||
"protocol": self._site_protocol,
|
"protocol": self._site_protocol,
|
||||||
"port": self._site_port,
|
"port": self._site_port,
|
||||||
"token": token,
|
"token": token,
|
||||||
"webhook_route": "/api/method/frappe.push_notification.auth_webhook",
|
"webhook_route": f"/api/method/frappe.push_notification.auth_webhook?secret={secret}",
|
||||||
}
|
}
|
||||||
response = self._send_post_request("notification_relay.api.auth.get_credential", body, False)
|
response = self._send_post_request("notification_relay.api.auth.get_credential", body, False)
|
||||||
success = response["success"]
|
success = response["success"]
|
||||||
|
|
@ -268,19 +268,14 @@ class PushNotification:
|
||||||
|
|
||||||
# Webhook which will be called by the central relay server for authentication
|
# Webhook which will be called by the central relay server for authentication
|
||||||
@frappe.whitelist(allow_guest=True, methods=["GET"])
|
@frappe.whitelist(allow_guest=True, methods=["GET"])
|
||||||
def auth_webhook():
|
def auth_webhook(secret: str):
|
||||||
url = urlparse(frappe.utils.get_url()).hostname
|
|
||||||
token = frappe.cache().get_value(f"{url}:push_relay_registration_token")
|
|
||||||
response = Response()
|
response = Response()
|
||||||
response.mimetype = "text/plain; charset=UTF-8"
|
response.mimetype = "text/plain; charset=UTF-8"
|
||||||
|
response.status_code = 401
|
||||||
|
|
||||||
if token is None or token == "":
|
if token := frappe.cache.get_value(f"push_relay_registration_token:{secret}"):
|
||||||
response.data = ""
|
response.data = token
|
||||||
response.status_code = 401
|
response.status_code = 200
|
||||||
return response
|
|
||||||
|
|
||||||
response.data = token
|
|
||||||
response.status_code = 200
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue