Skip to content

Commit

Permalink
feat: support wecom sso (#3518)
Browse files Browse the repository at this point in the history
* feat: support wecom sso

* chore: remove unused wecom js-sdk dependency
  • Loading branch information
FinleyGe authored and c121914yu committed Jan 5, 2025
1 parent a065456 commit c4cd2a9
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 35 deletions.
5 changes: 5 additions & 0 deletions packages/global/common/system/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ export type FastGPTFeConfigsType = {
google?: string;
wechat?: string;
dingtalk?: string;
wecom?: {
corpid?: string;
agentid?: string;
secret?: string;
};
microsoft?: {
clientId?: string;
tenantId?: string;
Expand Down
1 change: 1 addition & 0 deletions packages/global/support/user/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ export enum OAuthEnum {
wechat = 'wechat',
microsoft = 'microsoft',
dingtalk = 'dingtalk',
wecom = 'wecom',
sso = 'sso'
}
3 changes: 3 additions & 0 deletions packages/service/support/user/wecom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isWecomTerminal() {
return /wxwork/i.test(navigator.userAgent);
}
3 changes: 2 additions & 1 deletion packages/web/i18n/zh-CN/login.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
"register": "注册账号",
"root_password_placeholder": "root 用户密码为环境变量 DEFAULT_ROOT_PSW 的值",
"terms": "服务协议",
"use_root_login": "使用 root 用户登录"
"use_root_login": "使用 root 用户登录",
"wecom": "企业微信"
}
56 changes: 34 additions & 22 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import { useTranslation } from 'next-i18next';
import I18nLngSelector from '@/components/Select/I18nLngSelector';
import { useSystem } from '@fastgpt/web/hooks/useSystem';
import MyImage from '@fastgpt/web/components/common/Image/MyImage';
import { useToast } from '@fastgpt/web/hooks/useToast';
import { isWecomTerminal } from '@fastgpt/service/support/user/wecom';
import { PUT } from '@fastgpt/service/common/api/plusRequest';
const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz1234567890', 8);

interface Props {
Expand All @@ -29,6 +30,7 @@ const FormLayout = ({ children, setPageType, pageType }: Props) => {
const state = useRef(nanoid());
const redirectUri = `${location.origin}/login/provider`;
const { isPc } = useSystem();
const isWecom = isWecomTerminal();

const oAuthList = [
...(feConfigs?.oauth?.wechat && pageType !== LoginPageTypeEnum.wechat
Expand Down Expand Up @@ -82,6 +84,18 @@ const FormLayout = ({ children, setPageType, pageType }: Props) => {
}
]
: []),
...(feConfigs?.oauth?.wecom
? [
{
label: t('login:wecom'),
provider: OAuthEnum.wecom,
icon: 'common/wecom',
redirectUrl: isWecom
? `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${feConfigs?.oauth?.wecom?.corpid}&redirect_uri=${redirectUri}&response_type=code&scope=snsapi_base&agentid=${feConfigs?.oauth?.wecom?.agentid}&state=${state.current}#wechat_redirect`
: `https://login.work.weixin.qq.com/wwlogin/sso/login?login_type=CorpApp&appid=${feConfigs?.oauth?.wecom?.corpid}&agentid=${feConfigs?.oauth?.wecom?.agentid}&redirect_uri=${redirectUri}&state=${state.current}`
}
]
: []),
...(pageType !== LoginPageTypeEnum.passwordLogin
? [
{
Expand All @@ -99,6 +113,20 @@ const FormLayout = ({ children, setPageType, pageType }: Props) => {
[feConfigs?.sso?.url, oAuthList.length]
);

const onClickOauth = useCallback(
async (item: any) => {
item.redirectUrl &&
setLoginStore({
provider: item.provider,
lastRoute,
state: state.current
});
item.redirectUrl && router.replace(item.redirectUrl, '_self');
item.pageType && setPageType(item.pageType);
},
[lastRoute, setLoginStore, setPageType]
);

const onClickSso = useCallback(() => {
if (!feConfigs?.sso?.url) return;
setLoginStore({
Expand All @@ -115,7 +143,10 @@ const FormLayout = ({ children, setPageType, pageType }: Props) => {
if (feConfigs?.sso?.autoLogin) {
onClickSso();
}
}, [feConfigs?.sso?.autoLogin]);
if (isWecom) {
onClickOauth(oAuthList.find((item) => item.provider === OAuthEnum.wecom));
}
}, [feConfigs?.sso?.autoLogin, isWecom]);

return (
<Flex flexDirection={'column'} h={'100%'}>
Expand Down Expand Up @@ -159,16 +190,7 @@ const FormLayout = ({ children, setPageType, pageType }: Props) => {
borderRadius={'sm'}
fontWeight={'medium'}
leftIcon={<MyIcon name={item.icon as any} w={'20px'} />}
onClick={() => {
item.redirectUrl &&
setLoginStore({
provider: item.provider,
lastRoute,
state: state.current
});
item.redirectUrl && router.replace(item.redirectUrl, '_self');
item.pageType && setPageType(item.pageType);
}}
onClick={() => onClickOauth(item)}
>
{item.label}
</Button>
Expand Down

0 comments on commit c4cd2a9

Please sign in to comment.