diff --git a/web/src/auth/Provider.js b/web/src/auth/Provider.js index 47fe9ff9..a79dbf9f 100644 --- a/web/src/auth/Provider.js +++ b/web/src/auth/Provider.js @@ -15,81 +15,60 @@ import * as Util from "./Util"; import {StaticBaseUrl} from "../Setting"; -const GoogleAuthScope = "profile+email" -const GoogleAuthUri = "https://accounts.google.com/signin/oauth"; -const GoogleAuthLogo = `${StaticBaseUrl}/img/social_google.png`; - -const GithubAuthScope = "user:email+read:user" -const GithubAuthUri = "https://github.com/login/oauth/authorize"; -const GithubAuthLogo = `${StaticBaseUrl}/img/social_github.png`; - -const QqAuthScope = "get_user_info" -const QqAuthUri = "https://graph.qq.com/oauth2.0/authorize"; -const QqAuthLogo = `${StaticBaseUrl}/img/social_qq.png`; - -const WeChatAuthScope = "snsapi_login" -const WeChatAuthUri = "https://open.weixin.qq.com/connect/qrconnect"; -const WeChatAuthLogo = `${StaticBaseUrl}/img/social_wechat.png`; - -const FacebookAuthScope = "email,public_profile"; -const FacebookAuthUri = "https://www.facebook.com/dialog/oauth"; -const FacebookAuthLogo = `${StaticBaseUrl}/img/social_facebook.png`; - -// const DingTalkAuthScope = "email,public_profile"; -const DingTalkAuthUri = "https://oapi.dingtalk.com/connect/oauth2/sns_authorize"; -const DingTalkAuthLogo = `${StaticBaseUrl}/img/social_dingtalk.png`; - -const WeiboAuthScope = "email"; -const WeiboAuthUri = "https://api.weibo.com/oauth2/authorize"; -const WeiboAuthLogo = `${StaticBaseUrl}/img/social_weibo.png`; - -const GiteeAuthScope = "user_info%20emails"; -const GiteeAuthUri = "https://gitee.com/oauth/authorize"; -const GiteeAuthLogo = `${StaticBaseUrl}/img/social_gitee.png`; - -const LinkedInAuthScope = "r_liteprofile%20r_emailaddress"; -const LinkedInAuthUri = "https://www.linkedin.com/oauth/v2/authorization"; -const LinkedInAuthLogo = `${StaticBaseUrl}/img/social_linkedin.png`; - -const WeComSilentAuthScope = "snsapi_userinfo"; -const WeComAuthUri = "https://open.work.weixin.qq.com/wwopen/sso/3rd_qrConnect"; -const WeComSilentAuthUrl = "https://open.weixin.qq.com/connect/oauth2/authorize"; -const WeComAuthLogo = `${StaticBaseUrl}/img/social_wecom.png`; - -// const LarkAuthScope = ""; -const LarkAuthUri = "https://open.feishu.cn/open-apis/authen/v1/index"; -const LarkAuthLogo = `${StaticBaseUrl}/img/social_lark.png`; - -const GitLabAuthScope = "read_user+profile"; -const GitLabAuthUri = "https://gitlab.com/oauth/authorize"; -const GitLabAuthLogo = `${StaticBaseUrl}/img/social_gitlab.png`; +const authInfo = { + Google: { + scope: "profile+email", + endpoint: "https://accounts.google.com/signin/oauth", + }, + GitHub: { + scope: "user:email+read:user", + endpoint: "https://github.com/login/oauth/authorize", + }, + QQ: { + scope: "get_user_info", + endpoint: "https://graph.qq.com/oauth2.0/authorize", + }, + WeChat: { + scope: "snsapi_login", + endpoint: "https://open.weixin.qq.com/connect/qrconnect", + }, + Facebook: { + scope: "email,public_profile", + endpoint: "https://www.facebook.com/dialog/oauth", + }, + DingTalk: { + scope: "snsapi_login", + endpoint: "https://oapi.dingtalk.com/connect/oauth2/sns_authorize", + }, + Weibo: { + scope: "email", + endpoint: "https://api.weibo.com/oauth2/authorize", + }, + Gitee: { + scope: "user_info%20emails", + endpoint: "https://gitee.com/oauth/authorize", + }, + LinkedIn: { + scope: "r_liteprofile%20r_emailaddress", + endpoint: "https://www.linkedin.com/oauth/v2/authorization", + }, + WeCom: { + scope: "snsapi_userinfo", + endpoint: "https://open.work.weixin.qq.com/wwopen/sso/3rd_qrConnect", + silentEndpoint: "https://open.weixin.qq.com/connect/oauth2/authorize", + }, + Lark: { + // scope: "email", + endpoint: "https://open.feishu.cn/open-apis/authen/v1/index", + }, + GitLab: { + scope: "read_user+profile", + endpoint: "https://gitlab.com/oauth/authorize", + }, +} export function getAuthLogo(provider) { - if (provider.type === "Google") { - return GoogleAuthLogo; - } else if (provider.type === "GitHub") { - return GithubAuthLogo; - } else if (provider.type === "QQ") { - return QqAuthLogo; - } else if (provider.type === "WeChat") { - return WeChatAuthLogo; - } else if (provider.type === "Facebook") { - return FacebookAuthLogo; - } else if (provider.type === "DingTalk") { - return DingTalkAuthLogo; - } else if (provider.type === "Weibo") { - return WeiboAuthLogo; - } else if (provider.type === "Gitee") { - return GiteeAuthLogo; - } else if (provider.type === "LinkedIn") { - return LinkedInAuthLogo; - } else if (provider.type === "WeCom") { - return WeComAuthLogo; - } else if (provider.type === "Lark") { - return LarkAuthLogo; - } else if (provider.type === "GitLab") { - return GitLabAuthLogo; - } + return `${StaticBaseUrl}/img/social_${provider.type.toLowerCase()}.png`; } export function getAuthUrl(application, provider, method) { @@ -97,37 +76,40 @@ export function getAuthUrl(application, provider, method) { return ""; } + const endpoint = authInfo[provider.type].endpoint; const redirectUri = `${window.location.origin}/callback`; + const scope = authInfo[provider.type].scope; const state = Util.getQueryParamsToState(application.name, provider.name, method); + if (provider.type === "Google") { - return `${GoogleAuthUri}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&scope=${GoogleAuthScope}&response_type=code&state=${state}`; + return `${endpoint}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&scope=${scope}&response_type=code&state=${state}`; } else if (provider.type === "GitHub") { - return `${GithubAuthUri}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&scope=${GithubAuthScope}&response_type=code&state=${state}`; + return `${endpoint}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&scope=${scope}&response_type=code&state=${state}`; } else if (provider.type === "QQ") { - return `${QqAuthUri}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&scope=${QqAuthScope}&response_type=code&state=${state}`; + return `${endpoint}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&scope=${scope}&response_type=code&state=${state}`; } else if (provider.type === "WeChat") { - return `${WeChatAuthUri}?appid=${provider.clientId}&redirect_uri=${redirectUri}&scope=${WeChatAuthScope}&response_type=code&state=${state}#wechat_redirect`; + return `${endpoint}?appid=${provider.clientId}&redirect_uri=${redirectUri}&scope=${scope}&response_type=code&state=${state}#wechat_redirect`; } else if (provider.type === "Facebook") { - return `${FacebookAuthUri}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&scope=${FacebookAuthScope}&response_type=code&state=${state}`; + return `${endpoint}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&scope=${scope}&response_type=code&state=${state}`; } else if (provider.type === "DingTalk") { - return `${DingTalkAuthUri}?appid=${provider.clientId}&redirect_uri=${redirectUri}&scope=snsapi_login&response_type=code&state=${state}`; + return `${endpoint}?appid=${provider.clientId}&redirect_uri=${redirectUri}&scope=${scope}&response_type=code&state=${state}`; } else if (provider.type === "Weibo") { - return `${WeiboAuthUri}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&scope=${WeiboAuthScope}&response_type=code&state=${state}`; + return `${endpoint}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&scope=${scope}&response_type=code&state=${state}`; } else if (provider.type === "Gitee") { - return `${GiteeAuthUri}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&scope=${GiteeAuthScope}&response_type=code&state=${state}`; + return `${endpoint}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&scope=${scope}&response_type=code&state=${state}`; } else if (provider.type === "LinkedIn") { - return `${LinkedInAuthUri}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&scope=${LinkedInAuthScope}&response_type=code&state=${state}`; + return `${endpoint}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&scope=${scope}&response_type=code&state=${state}`; } else if (provider.type === "WeCom") { if (provider.method === "Silent") { - return `${WeComSilentAuthUrl}?appid=${provider.clientId}&redirect_uri=${redirectUri}&state=${state}&scope=${WeComSilentAuthScope}&response_type=code#wechat_redirect`; + return `${authInfo[provider.type].silentEndpoint}?appid=${provider.clientId}&redirect_uri=${redirectUri}&state=${state}&scope=${scope}&response_type=code#wechat_redirect`; } else if (provider.method === "Normal") { - return `${WeComAuthUri}?appid=${provider.clientId}&redirect_uri=${redirectUri}&state=${state}&usertype=member`; + return `${endpoint}?appid=${provider.clientId}&redirect_uri=${redirectUri}&state=${state}&usertype=member`; } else { return `https://error:not-supported-provider-method:${provider.method}`; } } else if (provider.type === "Lark") { - return `${LarkAuthUri}?app_id=${provider.clientId}&redirect_uri=${redirectUri}&state=${state}`; + return `${endpoint}?app_id=${provider.clientId}&redirect_uri=${redirectUri}&state=${state}`; } else if (provider.type === "GitLab") { - return `${GitLabAuthUri}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&state=${state}&response_type=code&scope=${GitLabAuthScope}`; + return `${endpoint}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&state=${state}&response_type=code&scope=${scope}`; } }