Remove useless code (#4416)
This commit is contained in:
parent
da81233d61
commit
dd94931116
26 changed files with 466 additions and 230 deletions
|
|
@ -1,5 +1,4 @@
|
|||
'use client'
|
||||
|
||||
import type { FC } from 'react'
|
||||
import React from 'react'
|
||||
|
||||
|
|
|
|||
|
|
@ -1,12 +1,87 @@
|
|||
'use client'
|
||||
import type { FC } from 'react'
|
||||
import React from 'react'
|
||||
|
||||
import React, { useEffect } from 'react'
|
||||
import cn from 'classnames'
|
||||
import type { IMainProps } from '@/app/components/share/chat'
|
||||
import Main from '@/app/components/share/chatbot'
|
||||
import Loading from '@/app/components/base/loading'
|
||||
import { fetchSystemFeatures } from '@/service/share'
|
||||
import LogoSite from '@/app/components/base/logo/logo-site'
|
||||
|
||||
const Chatbot: FC<IMainProps> = () => {
|
||||
const [isSSOEnforced, setIsSSOEnforced] = React.useState(true)
|
||||
const [loading, setLoading] = React.useState(true)
|
||||
|
||||
useEffect(() => {
|
||||
fetchSystemFeatures().then((res) => {
|
||||
setIsSSOEnforced(res.sso_enforced_for_web)
|
||||
setLoading(false)
|
||||
})
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<Main />
|
||||
<>
|
||||
{
|
||||
loading
|
||||
? (
|
||||
<div className="flex items-center justify-center h-full" >
|
||||
<div className={
|
||||
cn(
|
||||
'flex flex-col items-center w-full grow items-center justify-center',
|
||||
'px-6',
|
||||
'md:px-[108px]',
|
||||
)
|
||||
}>
|
||||
<Loading type='area' />
|
||||
</div>
|
||||
</div >
|
||||
)
|
||||
: (
|
||||
<>
|
||||
{isSSOEnforced
|
||||
? (
|
||||
<div className={cn(
|
||||
'flex w-full min-h-screen',
|
||||
'sm:p-4 lg:p-8',
|
||||
'gap-x-20',
|
||||
'justify-center lg:justify-start',
|
||||
)}>
|
||||
<div className={
|
||||
cn(
|
||||
'flex w-full flex-col bg-white shadow rounded-2xl shrink-0',
|
||||
'space-between',
|
||||
)
|
||||
}>
|
||||
<div className='flex items-center justify-between p-6 w-full'>
|
||||
<LogoSite />
|
||||
</div>
|
||||
|
||||
<div className={
|
||||
cn(
|
||||
'flex flex-col items-center w-full grow items-center justify-center',
|
||||
'px-6',
|
||||
'md:px-[108px]',
|
||||
)
|
||||
}>
|
||||
<div className='flex flex-col md:w-[400px]'>
|
||||
<div className="w-full mx-auto">
|
||||
<h2 className="text-[16px] font-bold text-gray-900">
|
||||
Warning: Chatbot is not available
|
||||
</h2>
|
||||
<p className="text-[16px] text-gray-600 mt-2">
|
||||
Because SSO is enforced. Please contact your administrator.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
: <Main />
|
||||
}
|
||||
</>
|
||||
)}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
|||
147
web/app/(shareLayout)/webapp-signin/page.tsx
Normal file
147
web/app/(shareLayout)/webapp-signin/page.tsx
Normal file
|
|
@ -0,0 +1,147 @@
|
|||
'use client'
|
||||
import cn from 'classnames'
|
||||
import { useRouter, useSearchParams } from 'next/navigation'
|
||||
import type { FC } from 'react'
|
||||
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 LogoSite from '@/app/components/base/logo/logo-site'
|
||||
import { setAccessToken } from '@/app/components/share/utils'
|
||||
|
||||
const WebSSOForm: FC = () => {
|
||||
const searchParams = useSearchParams()
|
||||
|
||||
const redirectUrl = searchParams.get('redirect_url')
|
||||
const tokenFromUrl = searchParams.get('web_sso_token')
|
||||
const message = searchParams.get('message')
|
||||
|
||||
const router = useRouter()
|
||||
const { t } = useTranslation()
|
||||
|
||||
const [isLoading, setIsLoading] = useState(false)
|
||||
const [protocal, setProtocal] = useState('')
|
||||
|
||||
useEffect(() => {
|
||||
const fetchFeaturesAndSetToken = async () => {
|
||||
await fetchSystemFeatures().then((res) => {
|
||||
setProtocal(res.sso_enforced_for_web_protocol)
|
||||
})
|
||||
|
||||
// Callback from SSO, process token and redirect
|
||||
if (tokenFromUrl && redirectUrl) {
|
||||
const appCode = redirectUrl.split('/').pop()
|
||||
if (!appCode) {
|
||||
Toast.notify({
|
||||
type: 'error',
|
||||
message: 'redirect url is invalid. App code is not found.',
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
await setAccessToken(appCode, tokenFromUrl)
|
||||
router.push(redirectUrl)
|
||||
}
|
||||
}
|
||||
|
||||
fetchFeaturesAndSetToken()
|
||||
|
||||
if (message) {
|
||||
Toast.notify({
|
||||
type: 'error',
|
||||
message,
|
||||
})
|
||||
}
|
||||
}, [])
|
||||
|
||||
const handleSSOLogin = () => {
|
||||
setIsLoading(true)
|
||||
|
||||
if (!redirectUrl) {
|
||||
Toast.notify({
|
||||
type: 'error',
|
||||
message: 'redirect url is not found.',
|
||||
})
|
||||
setIsLoading(false)
|
||||
return
|
||||
}
|
||||
|
||||
const appCode = redirectUrl.split('/').pop()
|
||||
if (!appCode) {
|
||||
Toast.notify({
|
||||
type: 'error',
|
||||
message: 'redirect url is invalid. App code is not found.',
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if (protocal === 'saml') {
|
||||
fetchWebSAMLSSOUrl(appCode, redirectUrl).then((res) => {
|
||||
router.push(res.url)
|
||||
}).finally(() => {
|
||||
setIsLoading(false)
|
||||
})
|
||||
}
|
||||
else if (protocal === 'oidc') {
|
||||
fetchWebOIDCSSOUrl(appCode, redirectUrl).then((res) => {
|
||||
router.push(res.url)
|
||||
}).finally(() => {
|
||||
setIsLoading(false)
|
||||
})
|
||||
}
|
||||
else {
|
||||
Toast.notify({
|
||||
type: 'error',
|
||||
message: 'sso protocal is not supported.',
|
||||
})
|
||||
setIsLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={cn(
|
||||
'flex w-full min-h-screen',
|
||||
'sm:p-4 lg:p-8',
|
||||
'gap-x-20',
|
||||
'justify-center lg:justify-start',
|
||||
)}>
|
||||
<div className={
|
||||
cn(
|
||||
'flex w-full flex-col bg-white shadow rounded-2xl shrink-0',
|
||||
'space-between',
|
||||
)
|
||||
}>
|
||||
<div className='flex items-center justify-between p-6 w-full'>
|
||||
<LogoSite />
|
||||
</div>
|
||||
|
||||
<div className={
|
||||
cn(
|
||||
'flex flex-col items-center w-full grow items-center justify-center',
|
||||
'px-6',
|
||||
'md:px-[108px]',
|
||||
)
|
||||
}>
|
||||
<div className='flex flex-col md:w-[400px]'>
|
||||
<div className="w-full mx-auto">
|
||||
<h2 className="text-[32px] font-bold text-gray-900">{t('login.pageTitle')}</h2>
|
||||
</div>
|
||||
<div className="w-full mx-auto mt-10">
|
||||
<Button
|
||||
tabIndex={0}
|
||||
type='primary'
|
||||
onClick={() => { handleSSOLogin() }}
|
||||
disabled={isLoading}
|
||||
className="w-full !fone-medium !text-sm"
|
||||
>{t('login.sso')}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default React.memo(WebSSOForm)
|
||||
|
|
@ -1,4 +1,6 @@
|
|||
import { CONVERSATION_ID_INFO } from '../base/chat/constants'
|
||||
import { fetchAccessToken } from '@/service/share'
|
||||
|
||||
export const checkOrSetAccessToken = async () => {
|
||||
const sharedToken = globalThis.location.pathname.split('/').slice(-1)[0]
|
||||
const accessToken = localStorage.getItem('token') || JSON.stringify({ [sharedToken]: '' })
|
||||
|
|
@ -15,3 +17,37 @@ export const checkOrSetAccessToken = async () => {
|
|||
localStorage.setItem('token', JSON.stringify(accessTokenJson))
|
||||
}
|
||||
}
|
||||
|
||||
export const setAccessToken = async (sharedToken: string, token: string) => {
|
||||
const accessToken = localStorage.getItem('token') || JSON.stringify({ [sharedToken]: '' })
|
||||
let accessTokenJson = { [sharedToken]: '' }
|
||||
try {
|
||||
accessTokenJson = JSON.parse(accessToken)
|
||||
}
|
||||
catch (e) {
|
||||
|
||||
}
|
||||
|
||||
localStorage.removeItem(CONVERSATION_ID_INFO)
|
||||
|
||||
accessTokenJson[sharedToken] = token
|
||||
localStorage.setItem('token', JSON.stringify(accessTokenJson))
|
||||
}
|
||||
|
||||
export const removeAccessToken = () => {
|
||||
const sharedToken = globalThis.location.pathname.split('/').slice(-1)[0]
|
||||
|
||||
const accessToken = localStorage.getItem('token') || JSON.stringify({ [sharedToken]: '' })
|
||||
let accessTokenJson = { [sharedToken]: '' }
|
||||
try {
|
||||
accessTokenJson = JSON.parse(accessToken)
|
||||
}
|
||||
catch (e) {
|
||||
|
||||
}
|
||||
|
||||
localStorage.removeItem(CONVERSATION_ID_INFO)
|
||||
|
||||
delete accessTokenJson[sharedToken]
|
||||
localStorage.setItem('token', JSON.stringify(accessTokenJson))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,19 +6,20 @@ import Loading from '../components/base/loading'
|
|||
import Forms from './forms'
|
||||
import Header from './_header'
|
||||
import style from './page.module.css'
|
||||
import EnterpriseSSOForm from './enterpriseSSOForm'
|
||||
import UserSSOForm from './userSSOForm'
|
||||
import { IS_CE_EDITION } from '@/config'
|
||||
import { getEnterpriseFeatures } from '@/service/enterprise'
|
||||
import type { EnterpriseFeatures } from '@/types/enterprise'
|
||||
import { defaultEnterpriseFeatures } from '@/types/enterprise'
|
||||
|
||||
import type { SystemFeatures } from '@/types/feature'
|
||||
import { defaultSystemFeatures } from '@/types/feature'
|
||||
import { getSystemFeatures } from '@/service/common'
|
||||
|
||||
const SignIn = () => {
|
||||
const [loading, setLoading] = useState<boolean>(true)
|
||||
const [enterpriseFeatures, setEnterpriseFeatures] = useState<EnterpriseFeatures>(defaultEnterpriseFeatures)
|
||||
const [systemFeatures, setSystemFeatures] = useState<SystemFeatures>(defaultSystemFeatures)
|
||||
|
||||
useEffect(() => {
|
||||
getEnterpriseFeatures().then((res) => {
|
||||
setEnterpriseFeatures(res)
|
||||
getSystemFeatures().then((res) => {
|
||||
setSystemFeatures(res)
|
||||
}).finally(() => {
|
||||
setLoading(false)
|
||||
})
|
||||
|
|
@ -70,7 +71,7 @@ gtag('config', 'AW-11217955271"');
|
|||
</div>
|
||||
)}
|
||||
|
||||
{!loading && !enterpriseFeatures.sso_enforced_for_signin && (
|
||||
{!loading && !systemFeatures.sso_enforced_for_signin && (
|
||||
<>
|
||||
<Forms />
|
||||
<div className='px-8 py-6 text-sm font-normal text-gray-500'>
|
||||
|
|
@ -79,8 +80,8 @@ gtag('config', 'AW-11217955271"');
|
|||
</>
|
||||
)}
|
||||
|
||||
{!loading && enterpriseFeatures.sso_enforced_for_signin && (
|
||||
<EnterpriseSSOForm protocol={enterpriseFeatures.sso_enforced_for_signin_protocol} />
|
||||
{!loading && systemFeatures.sso_enforced_for_signin && (
|
||||
<UserSSOForm protocol={systemFeatures.sso_enforced_for_signin_protocol} />
|
||||
)}
|
||||
</div>
|
||||
|
||||
|
|
|
|||
|
|
@ -5,14 +5,14 @@ import type { FC } from 'react'
|
|||
import { useEffect, useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import Toast from '@/app/components/base/toast'
|
||||
import { getOIDCSSOUrl, getSAMLSSOUrl } from '@/service/enterprise'
|
||||
import { getUserOIDCSSOUrl, getUserSAMLSSOUrl } from '@/service/sso'
|
||||
import Button from '@/app/components/base/button'
|
||||
|
||||
type EnterpriseSSOFormProps = {
|
||||
type UserSSOFormProps = {
|
||||
protocol: string
|
||||
}
|
||||
|
||||
const EnterpriseSSOForm: FC<EnterpriseSSOFormProps> = ({
|
||||
const UserSSOForm: FC<UserSSOFormProps> = ({
|
||||
protocol,
|
||||
}) => {
|
||||
const searchParams = useSearchParams()
|
||||
|
|
@ -41,15 +41,15 @@ const EnterpriseSSOForm: FC<EnterpriseSSOFormProps> = ({
|
|||
const handleSSOLogin = () => {
|
||||
setIsLoading(true)
|
||||
if (protocol === 'saml') {
|
||||
getSAMLSSOUrl().then((res) => {
|
||||
getUserSAMLSSOUrl().then((res) => {
|
||||
router.push(res.url)
|
||||
}).finally(() => {
|
||||
setIsLoading(false)
|
||||
})
|
||||
}
|
||||
else {
|
||||
getOIDCSSOUrl().then((res) => {
|
||||
document.cookie = `oidc-state=${res.state}`
|
||||
getUserOIDCSSOUrl().then((res) => {
|
||||
document.cookie = `user-oidc-state=${res.state}`
|
||||
router.push(res.url)
|
||||
}).finally(() => {
|
||||
setIsLoading(false)
|
||||
|
|
@ -84,4 +84,4 @@ const EnterpriseSSOForm: FC<EnterpriseSSOFormProps> = ({
|
|||
)
|
||||
}
|
||||
|
||||
export default EnterpriseSSOForm
|
||||
export default UserSSOForm
|
||||
|
|
@ -10,6 +10,7 @@ import type {
|
|||
WorkflowFinishedResponse,
|
||||
WorkflowStartedResponse,
|
||||
} from '@/types/workflow'
|
||||
import { removeAccessToken } from '@/app/components/share/utils'
|
||||
const TIME_OUT = 100000
|
||||
|
||||
const ContentType = {
|
||||
|
|
@ -97,6 +98,10 @@ function unicodeToChar(text: string) {
|
|||
})
|
||||
}
|
||||
|
||||
function requiredWebSSOLogin() {
|
||||
globalThis.location.href = `/webapp-signin?redirect_url=${globalThis.location.pathname}`
|
||||
}
|
||||
|
||||
export function format(text: string) {
|
||||
let res = text.trim()
|
||||
if (res.startsWith('\n'))
|
||||
|
|
@ -308,6 +313,15 @@ const baseFetch = <T>(
|
|||
return bodyJson.then((data: ResponseError) => {
|
||||
if (!silent)
|
||||
Toast.notify({ type: 'error', message: data.message })
|
||||
|
||||
if (data.code === 'web_sso_auth_required')
|
||||
requiredWebSSOLogin()
|
||||
|
||||
if (data.code === 'unauthorized') {
|
||||
removeAccessToken()
|
||||
globalThis.location.reload()
|
||||
}
|
||||
|
||||
return Promise.reject(data)
|
||||
})
|
||||
}
|
||||
|
|
@ -467,6 +481,16 @@ export const ssePost = (
|
|||
if (!/^(2|3)\d{2}$/.test(String(res.status))) {
|
||||
res.json().then((data: any) => {
|
||||
Toast.notify({ type: 'error', message: data.message || 'Server Error' })
|
||||
|
||||
if (isPublicAPI) {
|
||||
if (data.code === 'web_sso_auth_required')
|
||||
requiredWebSSOLogin()
|
||||
|
||||
if (data.code === 'unauthorized') {
|
||||
removeAccessToken()
|
||||
globalThis.location.reload()
|
||||
}
|
||||
}
|
||||
})
|
||||
onError?.('Server Error')
|
||||
return
|
||||
|
|
|
|||
|
|
@ -34,6 +34,7 @@ import type {
|
|||
ModelProvider,
|
||||
} from '@/app/components/header/account-setting/model-provider-page/declarations'
|
||||
import type { RETRIEVE_METHOD } from '@/types/app'
|
||||
import type { SystemFeatures } from '@/types/feature'
|
||||
|
||||
export const login: Fetcher<CommonResponse & { data: string }, { url: string; body: Record<string, any> }> = ({ url, body }) => {
|
||||
return post(url, { body }) as Promise<CommonResponse & { data: string }>
|
||||
|
|
@ -271,3 +272,7 @@ type RetrievalMethodsRes = {
|
|||
export const fetchSupportRetrievalMethods: Fetcher<RetrievalMethodsRes, string> = (url) => {
|
||||
return get<RetrievalMethodsRes>(url)
|
||||
}
|
||||
|
||||
export const getSystemFeatures = () => {
|
||||
return get<SystemFeatures>('/system-features')
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +0,0 @@
|
|||
import { get } from './base'
|
||||
import type { EnterpriseFeatures } from '@/types/enterprise'
|
||||
|
||||
export const getEnterpriseFeatures = () => {
|
||||
return get<EnterpriseFeatures>('/enterprise-features')
|
||||
}
|
||||
|
||||
export const getSAMLSSOUrl = () => {
|
||||
return get<{ url: string }>('/enterprise/sso/saml/login')
|
||||
}
|
||||
|
||||
export const getOIDCSSOUrl = () => {
|
||||
return get<{ url: string; state: string }>('/enterprise/sso/oidc/login')
|
||||
}
|
||||
|
|
@ -11,6 +11,7 @@ import type {
|
|||
ConversationItem,
|
||||
} from '@/models/share'
|
||||
import type { ChatConfig } from '@/app/components/base/chat/types'
|
||||
import type { SystemFeatures } from '@/types/feature'
|
||||
|
||||
function getAction(action: 'get' | 'post' | 'del' | 'patch', isInstalledApp: boolean) {
|
||||
switch (action) {
|
||||
|
|
@ -135,6 +136,29 @@ export const fetchAppParams = async (isInstalledApp: boolean, installedAppId = '
|
|||
return (getAction('get', isInstalledApp))(getUrl('parameters', isInstalledApp, installedAppId)) as Promise<ChatConfig>
|
||||
}
|
||||
|
||||
export const fetchSystemFeatures = async () => {
|
||||
return (getAction('get', false))(getUrl('system-features', false, '')) as Promise<SystemFeatures>
|
||||
}
|
||||
|
||||
export const fetchWebSAMLSSOUrl = async (appCode: string, redirectUrl: string) => {
|
||||
return (getAction('get', false))(getUrl('/enterprise/sso/saml/login', false, ''), {
|
||||
params: {
|
||||
app_code: appCode,
|
||||
redirect_url: redirectUrl,
|
||||
},
|
||||
}) as Promise<{ url: string }>
|
||||
}
|
||||
|
||||
export const fetchWebOIDCSSOUrl = async (appCode: string, redirectUrl: string) => {
|
||||
return (getAction('get', false))(getUrl('/enterprise/sso/oidc/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<AppMeta>
|
||||
}
|
||||
|
|
|
|||
9
web/service/sso.ts
Normal file
9
web/service/sso.ts
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import { get } from './base'
|
||||
|
||||
export const getUserSAMLSSOUrl = () => {
|
||||
return get<{ url: string }>('/enterprise/sso/saml/login')
|
||||
}
|
||||
|
||||
export const getUserOIDCSSOUrl = () => {
|
||||
return get<{ url: string; state: string }>('/enterprise/sso/oidc/login')
|
||||
}
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
export type EnterpriseFeatures = {
|
||||
sso_enforced_for_signin: boolean
|
||||
sso_enforced_for_signin_protocol: string
|
||||
}
|
||||
|
||||
export const defaultEnterpriseFeatures: EnterpriseFeatures = {
|
||||
sso_enforced_for_signin: false,
|
||||
sso_enforced_for_signin_protocol: '',
|
||||
}
|
||||
13
web/types/feature.ts
Normal file
13
web/types/feature.ts
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
export type SystemFeatures = {
|
||||
sso_enforced_for_signin: boolean
|
||||
sso_enforced_for_signin_protocol: string
|
||||
sso_enforced_for_web: boolean
|
||||
sso_enforced_for_web_protocol: string
|
||||
}
|
||||
|
||||
export const defaultSystemFeatures: SystemFeatures = {
|
||||
sso_enforced_for_signin: false,
|
||||
sso_enforced_for_signin_protocol: '',
|
||||
sso_enforced_for_web: false,
|
||||
sso_enforced_for_web_protocol: '',
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue