From a90f2a8e723551755e39955a6fdf8cc84f33b848 Mon Sep 17 00:00:00 2001 From: Corentin Forler Date: Fri, 16 Aug 2024 19:15:37 +0200 Subject: [PATCH 1/3] feat(PWA): Allow login using OAuth --- frontend/src/views/Login.vue | 21 +++++++++++++++++- hrms/www/hrms.py | 41 +++++++++++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/frontend/src/views/Login.vue b/frontend/src/views/Login.vue index bae5cc8a5f..d6573830bc 100644 --- a/frontend/src/views/Login.vue +++ b/frontend/src/views/Login.vue @@ -32,6 +32,20 @@ Login + + @@ -88,7 +102,7 @@ diff --git a/hrms/www/hrms.py b/hrms/www/hrms.py index 50bb5740d1..360973fc4c 100644 --- a/hrms/www/hrms.py +++ b/hrms/www/hrms.py @@ -21,5 +21,44 @@ def get_context_for_dev(): def get_boot(): return frappe._dict( - {"site_name": frappe.local.site, "push_relay_server_url": frappe.conf.get("push_relay_server_url")} + {"site_name": frappe.local.site, "push_relay_server_url": frappe.conf.get("push_relay_server_url") or ""} ) + + +@frappe.whitelist(allow_guest=True) +def oauth_providers(): + from frappe.utils.html_utils import get_icon_html + from frappe.utils.oauth import get_oauth2_authorize_url, get_oauth_keys + from frappe.utils.password import get_decrypted_password + + out = [] + providers = frappe.get_all( + "Social Login Key", + filters={"enable_social_login": 1}, + fields=["name", "client_id", "base_url", "provider_name", "icon"], + order_by="name", + ) + + for provider in providers: + client_secret = get_decrypted_password("Social Login Key", provider.name, "client_secret") + if not client_secret: + continue + + icon = None + if provider.icon: + if provider.provider_name == "Custom": + icon = get_icon_html(provider.icon, small=True) + else: + icon = f"{provider.provider_name}" + + if provider.client_id and provider.base_url and get_oauth_keys(provider.name): + out.append( + { + "name": provider.name, + "provider_name": provider.provider_name, + "auth_url": get_oauth2_authorize_url(provider.name, "/hrms"), + "icon": icon, + } + ) + + return out From 38f7854b830fbc35f958daebf6dd36a41e7d0710 Mon Sep 17 00:00:00 2001 From: Corentin Forler Date: Thu, 17 Oct 2024 16:23:29 +0200 Subject: [PATCH 2/3] refactor: Move social logins API --- frontend/src/views/Login.vue | 2 +- hrms/api/oauth.py | 40 ++++++++++++++++++++++++++++++++ hrms/www/hrms.py | 44 ++++-------------------------------- 3 files changed, 45 insertions(+), 41 deletions(-) create mode 100644 hrms/api/oauth.py diff --git a/frontend/src/views/Login.vue b/frontend/src/views/Login.vue index d6573830bc..fcfa7e10b9 100644 --- a/frontend/src/views/Login.vue +++ b/frontend/src/views/Login.vue @@ -157,7 +157,7 @@ async function submit(e) { } const authProviders = createResource({ - url: "hrms.www.hrms.oauth_providers", + url: "hrms.api.oauth.oauth_providers", auto: true, }); diff --git a/hrms/api/oauth.py b/hrms/api/oauth.py new file mode 100644 index 0000000000..317af986ac --- /dev/null +++ b/hrms/api/oauth.py @@ -0,0 +1,40 @@ +import frappe + + +@frappe.whitelist(allow_guest=True) +def oauth_providers(): + from frappe.utils.html_utils import get_icon_html + from frappe.utils.oauth import get_oauth2_authorize_url, get_oauth_keys + from frappe.utils.password import get_decrypted_password + + out = [] + providers = frappe.get_all( + "Social Login Key", + filters={"enable_social_login": 1}, + fields=["name", "client_id", "base_url", "provider_name", "icon"], + order_by="name", + ) + + for provider in providers: + client_secret = get_decrypted_password("Social Login Key", provider.name, "client_secret") + if not client_secret: + continue + + icon = None + if provider.icon: + if provider.provider_name == "Custom": + icon = get_icon_html(provider.icon, small=True) + else: + icon = f"{provider.provider_name}" + + if provider.client_id and provider.base_url and get_oauth_keys(provider.name): + out.append( + { + "name": provider.name, + "provider_name": provider.provider_name, + "auth_url": get_oauth2_authorize_url(provider.name, "/hrms"), + "icon": icon, + } + ) + + return out diff --git a/hrms/www/hrms.py b/hrms/www/hrms.py index 360973fc4c..70efefb8f1 100644 --- a/hrms/www/hrms.py +++ b/hrms/www/hrms.py @@ -21,44 +21,8 @@ def get_context_for_dev(): def get_boot(): return frappe._dict( - {"site_name": frappe.local.site, "push_relay_server_url": frappe.conf.get("push_relay_server_url") or ""} + { + "site_name": frappe.local.site, + "push_relay_server_url": frappe.conf.get("push_relay_server_url") or "", + } ) - - -@frappe.whitelist(allow_guest=True) -def oauth_providers(): - from frappe.utils.html_utils import get_icon_html - from frappe.utils.oauth import get_oauth2_authorize_url, get_oauth_keys - from frappe.utils.password import get_decrypted_password - - out = [] - providers = frappe.get_all( - "Social Login Key", - filters={"enable_social_login": 1}, - fields=["name", "client_id", "base_url", "provider_name", "icon"], - order_by="name", - ) - - for provider in providers: - client_secret = get_decrypted_password("Social Login Key", provider.name, "client_secret") - if not client_secret: - continue - - icon = None - if provider.icon: - if provider.provider_name == "Custom": - icon = get_icon_html(provider.icon, small=True) - else: - icon = f"{provider.provider_name}" - - if provider.client_id and provider.base_url and get_oauth_keys(provider.name): - out.append( - { - "name": provider.name, - "provider_name": provider.provider_name, - "auth_url": get_oauth2_authorize_url(provider.name, "/hrms"), - "icon": icon, - } - ) - - return out From ca94e9158adb283e269487aa66246f30cb46b298 Mon Sep 17 00:00:00 2001 From: krantheman Date: Fri, 18 Oct 2024 13:07:54 +0530 Subject: [PATCH 3/3] fix: show social login icon --- frontend/src/views/Login.vue | 5 +++-- hrms/api/oauth.py | 9 +-------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/frontend/src/views/Login.vue b/frontend/src/views/Login.vue index fcfa7e10b9..40495aee28 100644 --- a/frontend/src/views/Login.vue +++ b/frontend/src/views/Login.vue @@ -42,7 +42,8 @@ class="flex items-center justify-center gap-2 transition-colors focus:outline-none text-gray-800 bg-gray-100 hover:bg-gray-200 active:bg-gray-300 focus-visible:ring focus-visible:ring-gray-400 h-7 text-base p-2 rounded" :href="provider.auth_url" > - Login with {{ provider.provider_name }} + + Login with {{ provider.provider_name }} @@ -159,5 +160,5 @@ async function submit(e) { const authProviders = createResource({ url: "hrms.api.oauth.oauth_providers", auto: true, -}); +}) diff --git a/hrms/api/oauth.py b/hrms/api/oauth.py index 317af986ac..d5db2268ad 100644 --- a/hrms/api/oauth.py +++ b/hrms/api/oauth.py @@ -20,20 +20,13 @@ def oauth_providers(): if not client_secret: continue - icon = None - if provider.icon: - if provider.provider_name == "Custom": - icon = get_icon_html(provider.icon, small=True) - else: - icon = f"{provider.provider_name}" - if provider.client_id and provider.base_url and get_oauth_keys(provider.name): out.append( { "name": provider.name, "provider_name": provider.provider_name, "auth_url": get_oauth2_authorize_url(provider.name, "/hrms"), - "icon": icon, + "icon": provider.icon, } )