You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. 'use client'
  2. import { useRouter, useSearchParams } from 'next/navigation'
  3. import type { FC } from 'react'
  4. import React, { useCallback, useEffect } from 'react'
  5. import { useTranslation } from 'react-i18next'
  6. import { RiDoorLockLine } from '@remixicon/react'
  7. import cn from '@/utils/classnames'
  8. import Toast from '@/app/components/base/toast'
  9. import { fetchWebOAuth2SSOUrl, fetchWebOIDCSSOUrl, fetchWebSAMLSSOUrl } from '@/service/share'
  10. import { setAccessToken } from '@/app/components/share/utils'
  11. import { useGlobalPublicStore } from '@/context/global-public-context'
  12. import { SSOProtocol } from '@/types/feature'
  13. import Loading from '@/app/components/base/loading'
  14. import AppUnavailable from '@/app/components/base/app-unavailable'
  15. const WebSSOForm: FC = () => {
  16. const { t } = useTranslation()
  17. const systemFeatures = useGlobalPublicStore(s => s.systemFeatures)
  18. const searchParams = useSearchParams()
  19. const router = useRouter()
  20. const redirectUrl = searchParams.get('redirect_url')
  21. const tokenFromUrl = searchParams.get('web_sso_token')
  22. const message = searchParams.get('message')
  23. const showErrorToast = (message: string) => {
  24. Toast.notify({
  25. type: 'error',
  26. message,
  27. })
  28. }
  29. const getAppCodeFromRedirectUrl = useCallback(() => {
  30. const appCode = redirectUrl?.split('/').pop()
  31. if (!appCode)
  32. return null
  33. return appCode
  34. }, [redirectUrl])
  35. const processTokenAndRedirect = useCallback(async () => {
  36. const appCode = getAppCodeFromRedirectUrl()
  37. if (!appCode || !tokenFromUrl || !redirectUrl) {
  38. showErrorToast('redirect url or app code or token is invalid.')
  39. return
  40. }
  41. await setAccessToken(appCode, tokenFromUrl)
  42. router.push(redirectUrl)
  43. }, [getAppCodeFromRedirectUrl, redirectUrl, router, tokenFromUrl])
  44. const handleSSOLogin = useCallback(async () => {
  45. const appCode = getAppCodeFromRedirectUrl()
  46. if (!appCode || !redirectUrl) {
  47. showErrorToast('redirect url or app code is invalid.')
  48. return
  49. }
  50. switch (systemFeatures.webapp_auth.sso_config.protocol) {
  51. case SSOProtocol.SAML: {
  52. const samlRes = await fetchWebSAMLSSOUrl(appCode, redirectUrl)
  53. router.push(samlRes.url)
  54. break
  55. }
  56. case SSOProtocol.OIDC: {
  57. const oidcRes = await fetchWebOIDCSSOUrl(appCode, redirectUrl)
  58. router.push(oidcRes.url)
  59. break
  60. }
  61. case SSOProtocol.OAuth2: {
  62. const oauth2Res = await fetchWebOAuth2SSOUrl(appCode, redirectUrl)
  63. router.push(oauth2Res.url)
  64. break
  65. }
  66. case '':
  67. break
  68. default:
  69. showErrorToast('SSO protocol is not supported.')
  70. }
  71. }, [getAppCodeFromRedirectUrl, redirectUrl, router, systemFeatures.webapp_auth.sso_config.protocol])
  72. useEffect(() => {
  73. const init = async () => {
  74. if (message) {
  75. showErrorToast(message)
  76. return
  77. }
  78. if (!tokenFromUrl) {
  79. await handleSSOLogin()
  80. return
  81. }
  82. await processTokenAndRedirect()
  83. }
  84. init()
  85. }, [message, processTokenAndRedirect, tokenFromUrl, handleSSOLogin])
  86. if (tokenFromUrl)
  87. return <div className='flex h-full items-center justify-center'><Loading /></div>
  88. if (message) {
  89. return <div className='flex h-full items-center justify-center'>
  90. <AppUnavailable code={'App Unavailable'} unknownReason={message} />
  91. </div>
  92. }
  93. if (systemFeatures.webapp_auth.enabled) {
  94. if (systemFeatures.webapp_auth.allow_sso) {
  95. return (
  96. <div className="flex h-full items-center justify-center">
  97. <div className={cn('flex w-full grow flex-col items-center justify-center', 'px-6', 'md:px-[108px]')}>
  98. <Loading />
  99. </div>
  100. </div>
  101. )
  102. }
  103. return <div className="flex h-full items-center justify-center">
  104. <div className="rounded-lg bg-gradient-to-r from-workflow-workflow-progress-bg-1 to-workflow-workflow-progress-bg-2 p-4">
  105. <div className='shadows-shadow-lg mb-2 flex h-10 w-10 items-center justify-center rounded-xl bg-components-card-bg shadow'>
  106. <RiDoorLockLine className='h-5 w-5' />
  107. </div>
  108. <p className='system-sm-medium text-text-primary'>{t('login.webapp.noLoginMethod')}</p>
  109. <p className='system-xs-regular mt-1 text-text-tertiary'>{t('login.webapp.noLoginMethodTip')}</p>
  110. </div>
  111. <div className="relative my-2 py-2">
  112. <div className="absolute inset-0 flex items-center" aria-hidden="true">
  113. <div className='h-px w-full bg-gradient-to-r from-background-gradient-mask-transparent via-divider-regular to-background-gradient-mask-transparent'></div>
  114. </div>
  115. </div>
  116. </div>
  117. }
  118. else {
  119. return <div className="flex h-full items-center justify-center">
  120. <p className='system-xs-regular text-text-tertiary'>{t('login.webapp.disabled')}</p>
  121. </div>
  122. }
  123. }
  124. export default React.memo(WebSSOForm)