From c87303508435adb5d8941bb76235204373c70f17 Mon Sep 17 00:00:00 2001 From: Garfield Dai Date: Tue, 21 May 2024 17:52:41 +0800 Subject: [PATCH] oauth2 supports. (#4551) --- web/app/(shareLayout)/webapp-signin/page.tsx | 9 ++++++++- web/app/signin/userSSOForm.tsx | 19 +++++++++++++++++-- web/service/share.ts | 9 +++++++++ web/service/sso.ts | 4 ++++ 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/web/app/(shareLayout)/webapp-signin/page.tsx b/web/app/(shareLayout)/webapp-signin/page.tsx index d0d05cdd0d..bb25c70f3e 100644 --- a/web/app/(shareLayout)/webapp-signin/page.tsx +++ b/web/app/(shareLayout)/webapp-signin/page.tsx @@ -6,7 +6,7 @@ import React, { useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import Toast from '@/app/components/base/toast' import Button from '@/app/components/base/button' -import { fetchSystemFeatures, fetchWebOIDCSSOUrl, fetchWebSAMLSSOUrl } from '@/service/share' +import { fetchSystemFeatures, fetchWebOAuth2SSOUrl, fetchWebOIDCSSOUrl, fetchWebSAMLSSOUrl } from '@/service/share' import LogoSite from '@/app/components/base/logo/logo-site' import { setAccessToken } from '@/app/components/share/utils' @@ -90,6 +90,13 @@ const WebSSOForm: FC = () => { setIsLoading(false) }) } + else if (protocal === 'oauth2') { + fetchWebOAuth2SSOUrl(appCode, redirectUrl).then((res) => { + router.push(res.url) + }).finally(() => { + setIsLoading(false) + }) + } else { Toast.notify({ type: 'error', diff --git a/web/app/signin/userSSOForm.tsx b/web/app/signin/userSSOForm.tsx index fe95be8c66..d5f92e7ab6 100644 --- a/web/app/signin/userSSOForm.tsx +++ b/web/app/signin/userSSOForm.tsx @@ -5,7 +5,7 @@ import type { FC } from 'react' import { useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import Toast from '@/app/components/base/toast' -import { getUserOIDCSSOUrl, getUserSAMLSSOUrl } from '@/service/sso' +import { getUserOAuth2SSOUrl, getUserOIDCSSOUrl, getUserSAMLSSOUrl } from '@/service/sso' import Button from '@/app/components/base/button' type UserSSOFormProps = { @@ -47,7 +47,7 @@ const UserSSOForm: FC = ({ setIsLoading(false) }) } - else { + else if (protocol === 'oidc') { getUserOIDCSSOUrl().then((res) => { document.cookie = `user-oidc-state=${res.state}` router.push(res.url) @@ -55,6 +55,21 @@ const UserSSOForm: FC = ({ setIsLoading(false) }) } + else if (protocol === 'oauth2') { + getUserOAuth2SSOUrl().then((res) => { + document.cookie = `user-oauth2-state=${res.state}` + router.push(res.url) + }).finally(() => { + setIsLoading(false) + }) + } + else { + Toast.notify({ + type: 'error', + message: 'invalid SSO protocol', + }) + setIsLoading(false) + } } return ( diff --git a/web/service/share.ts b/web/service/share.ts index 4b8ce6d3b3..9f3936d84d 100644 --- a/web/service/share.ts +++ b/web/service/share.ts @@ -159,6 +159,15 @@ export const fetchWebOIDCSSOUrl = async (appCode: string, redirectUrl: string) = }) as Promise<{ url: string }> } +export const fetchWebOAuth2SSOUrl = async (appCode: string, redirectUrl: string) => { + return (getAction('get', false))(getUrl('/enterprise/sso/oauth2/login', false, ''), { + params: { + app_code: appCode, + redirect_url: redirectUrl, + }, + }) as Promise<{ url: string }> +} + export const fetchAppMeta = async (isInstalledApp: boolean, installedAppId = '') => { return (getAction('get', isInstalledApp))(getUrl('meta', isInstalledApp, installedAppId)) as Promise } diff --git a/web/service/sso.ts b/web/service/sso.ts index 77b81fe4a6..9a8581a894 100644 --- a/web/service/sso.ts +++ b/web/service/sso.ts @@ -7,3 +7,7 @@ export const getUserSAMLSSOUrl = () => { export const getUserOIDCSSOUrl = () => { return get<{ url: string; state: string }>('/enterprise/sso/oidc/login') } + +export const getUserOAuth2SSOUrl = () => { + return get<{ url: string; state: string }>('/enterprise/sso/oauth2/login') +}