Skip to content

Commit

Permalink
oauth2 supports. (langgenius#4551)
Browse files Browse the repository at this point in the history
  • Loading branch information
GarfieldDai authored May 21, 2024
1 parent f32bba6 commit c873035
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
9 changes: 8 additions & 1 deletion web/app/(shareLayout)/webapp-signin/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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',
Expand Down
19 changes: 17 additions & 2 deletions web/app/signin/userSSOForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -47,14 +47,29 @@ const UserSSOForm: FC<UserSSOFormProps> = ({
setIsLoading(false)
})
}
else {
else if (protocol === 'oidc') {
getUserOIDCSSOUrl().then((res) => {
document.cookie = `user-oidc-state=${res.state}`
router.push(res.url)
}).finally(() => {
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 (
Expand Down
9 changes: 9 additions & 0 deletions web/service/share.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AppMeta>
}
Expand Down
4 changes: 4 additions & 0 deletions web/service/sso.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}

0 comments on commit c873035

Please sign in to comment.