casdoor/web/src/auth/Provider.js

95 lines
4.4 KiB
JavaScript
Raw Normal View History

2021-02-14 16:59:08 +08:00
// Copyright 2021 The casbin Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2021-03-21 13:45:55 +08:00
import * as Util from "./Util";
2021-06-12 11:52:59 +08:00
import {StaticBaseUrl} from "../Setting";
2021-02-14 16:59:08 +08:00
const GoogleAuthScope = "profile+email"
const GoogleAuthUri = "https://accounts.google.com/signin/oauth";
2021-06-12 11:52:59 +08:00
const GoogleAuthLogo = `${StaticBaseUrl}/img/social_google.png`;
2021-02-14 16:59:08 +08:00
const GithubAuthScope = "user:email+read:user"
const GithubAuthUri = "https://github.com/login/oauth/authorize";
2021-06-12 11:52:59 +08:00
const GithubAuthLogo = `${StaticBaseUrl}/img/social_github.png`;
2021-02-14 16:59:08 +08:00
const QqAuthScope = "get_user_info"
const QqAuthUri = "https://graph.qq.com/oauth2.0/authorize";
2021-06-12 11:52:59 +08:00
const QqAuthLogo = `${StaticBaseUrl}/img/social_qq.png`;
2021-02-14 16:59:08 +08:00
const WeChatAuthScope = "snsapi_login"
const WeChatAuthUri = "https://open.weixin.qq.com/connect/qrconnect";
2021-06-12 11:52:59 +08:00
const WeChatAuthLogo = `${StaticBaseUrl}/img/social_wechat.png`;
2021-02-14 16:59:08 +08:00
2021-06-12 11:52:59 +08:00
const FacebookAuthScope = "email,public_profile";
const FacebookAuthUri = "https://www.facebook.com/dialog/oauth";
const FacebookAuthLogo = `${StaticBaseUrl}/img/social_facebook.png`;
2021-06-12 11:52:59 +08:00
// const DingTalkAuthScope = "email,public_profile";
const DingTalkAuthUri = "https://oapi.dingtalk.com/connect/oauth2/sns_authorize";
const DingTalkAuthLogo = `${StaticBaseUrl}/img/social_dingtalk.png`;
2021-06-12 11:52:59 +08:00
const WeiboAuthScope = "email";
const WeiboAuthUri = "https://api.weibo.com/oauth2/authorize";
const WeiboAuthLogo = `${StaticBaseUrl}/img/social_weibo.png`;
2021-06-15 22:12:50 +08:00
const GiteeAuthScope = "user_info,emails";
const GiteeAuthUri = "https://gitee.com/oauth/authorize";
const GiteeAuthLogo = `${StaticBaseUrl}/img/social_gitee.png`;
2021-02-14 16:59:08 +08:00
export function getAuthLogo(provider) {
2021-03-28 20:20:40 +08:00
if (provider.type === "Google") {
2021-02-14 16:59:08 +08:00
return GoogleAuthLogo;
2021-03-28 20:20:40 +08:00
} else if (provider.type === "GitHub") {
2021-02-14 16:59:08 +08:00
return GithubAuthLogo;
2021-03-28 20:20:40 +08:00
} else if (provider.type === "QQ") {
2021-02-14 16:59:08 +08:00
return QqAuthLogo;
2021-03-28 20:20:40 +08:00
} else if (provider.type === "WeChat") {
2021-02-14 16:59:08 +08:00
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;
2021-02-14 16:59:08 +08:00
}
}
2021-02-14 21:21:42 +08:00
export function getAuthUrl(application, provider, method) {
2021-04-19 01:14:41 +08:00
if (application === null || provider === null) {
return "";
}
2021-03-21 16:05:00 +08:00
const redirectUri = `${window.location.origin}/callback`;
const state = Util.getQueryParamsToState(application.name, provider.name, method);
2021-03-28 20:20:40 +08:00
if (provider.type === "Google") {
2021-03-21 13:45:55 +08:00
return `${GoogleAuthUri}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&scope=${GoogleAuthScope}&response_type=code&state=${state}`;
2021-03-28 20:20:40 +08:00
} else if (provider.type === "GitHub") {
2021-03-21 13:45:55 +08:00
return `${GithubAuthUri}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&scope=${GithubAuthScope}&response_type=code&state=${state}`;
2021-03-28 20:20:40 +08:00
} else if (provider.type === "QQ") {
2021-03-21 13:45:55 +08:00
return `${QqAuthUri}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&scope=${QqAuthScope}&response_type=code&state=${state}`;
2021-03-28 20:20:40 +08:00
} else if (provider.type === "WeChat") {
2021-03-21 13:45:55 +08:00
return `${WeChatAuthUri}?appid=${provider.clientId}&redirect_uri=${redirectUri}&scope=${WeChatAuthScope}&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}`;
} else if (provider.type === "DingTalk") {
return `${DingTalkAuthUri}?appid=${provider.clientId}&redirect_uri=${redirectUri}&scope=snsapi_login&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}`;
} else if (provider.type === "Gitee") {
return `${GiteeAuthUri}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&scope=${GiteeAuthScope}&response_type=code&state=${state}`;
2021-02-14 16:59:08 +08:00
}
}