casdoor/web/src/auth/Auth.js

63 lines
2.8 KiB
JavaScript
Raw Normal View History

2021-02-13 21:04:23 +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-02-14 15:51:56 +08:00
const AuthState = "casdoor";
2021-02-13 21:04:23 +08:00
2021-02-14 15:51:56 +08:00
const GoogleAuthScope = "profile+email"
const GoogleAuthUri = "https://accounts.google.com/signin/oauth";
const GoogleAuthLogo = "https://cdn.jsdelivr.net/gh/casbin/static/img/social_google.png";
const GithubAuthScope = "user:email+read:user"
const GithubAuthUri = "https://github.com/login/oauth/authorize";
const GithubAuthLogo = "https://cdn.jsdelivr.net/gh/casbin/static/img/social_github.png";
const QqAuthScope = "get_user_info"
const QqAuthUri = "https://graph.qq.com/oauth2.0/authorize";
const QqAuthLogo = "https://cdn.jsdelivr.net/gh/casbin/static/img/social_qq.png";
const WeChatAuthScope = "snsapi_login"
const WeChatAuthUri = "https://open.weixin.qq.com/connect/qrconnect";
const WeChatAuthLogo = "https://cdn.jsdelivr.net/gh/casbin/static/img/social_wechat.png";
2021-02-13 21:04:23 +08:00
2021-02-13 23:00:43 +08:00
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;
}
2021-02-13 21:04:23 +08:00
}
2021-02-14 14:34:03 +08:00
export function getClientUrl() {
const hostname = window.location.hostname;
if (hostname === "localhost") {
return `http://${hostname}:7001`;
} else {
return `https://${hostname}`;
}
}
2021-02-13 23:00:43 +08:00
export function getAuthUrl(provider, method) {
2021-02-14 14:34:03 +08:00
const redirectUri = `${getClientUrl()}/callback/${provider.type}/${provider.name}/${method}`;
2021-02-13 23:00:43 +08:00
if (provider.type === "google") {
2021-02-14 00:22:24 +08:00
return `${GoogleAuthUri}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&scope=${GoogleAuthScope}&response_type=code&state=${AuthState}`;
2021-02-13 23:00:43 +08:00
} else if (provider.type === "github") {
2021-02-14 00:22:24 +08:00
return `${GithubAuthUri}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&scope=${GithubAuthScope}&response_type=code&state=${AuthState}`;
2021-02-13 23:00:43 +08:00
} else if (provider.type === "qq") {
2021-02-14 00:22:24 +08:00
return `${QqAuthUri}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&scope=${QqAuthScope}&response_type=code&state=${AuthState}`;
2021-02-13 23:00:43 +08:00
} else if (provider.type === "wechat") {
2021-02-14 00:22:24 +08:00
return `${WeChatAuthUri}?appid=${provider.clientId}&redirect_uri=${redirectUri}&scope=${WeChatAuthScope}&response_type=code&state=${AuthState}#wechat_redirect`;
2021-02-13 23:00:43 +08:00
}
2021-02-13 21:04:23 +08:00
}