Add github oauth.

This commit is contained in:
Yang Luo
2021-02-14 00:22:24 +08:00
parent ea09beffe2
commit c81118feff
14 changed files with 419 additions and 8 deletions

View File

@ -42,13 +42,14 @@ export function getAuthLogo(provider) {
}
export function getAuthUrl(provider, method) {
const redirectUri = `${Setting.ClientUrl}/callback/${provider.type}/${provider.name}/${method}`;
if (provider.type === "google") {
return `${GoogleAuthUri}?client_id=${provider.clientId}&redirect_uri=${Setting.ClientUrl}/callback/google/${method}&scope=${GoogleAuthScope}&response_type=code&state=${AuthState}`;
return `${GoogleAuthUri}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&scope=${GoogleAuthScope}&response_type=code&state=${AuthState}`;
} else if (provider.type === "github") {
return `${GithubAuthUri}?client_id=${provider.clientId}&redirect_uri=${Setting.ClientUrl}/callback/github/${method}&scope=${GithubAuthScope}&response_type=code&state=${AuthState}`;
return `${GithubAuthUri}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&scope=${GithubAuthScope}&response_type=code&state=${AuthState}`;
} else if (provider.type === "qq") {
return `${QqAuthUri}?client_id=${provider.clientId}&redirect_uri=${Setting.ClientUrl}/callback/qq/${method}&scope=${QqAuthScope}&response_type=code&state=${AuthState}`;
return `${QqAuthUri}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&scope=${QqAuthScope}&response_type=code&state=${AuthState}`;
} else if (provider.type === "wechat") {
return `${WeChatAuthUri}?appid=${provider.clientId}&redirect_uri=${Setting.ClientUrl}/callback/wechat/${method}&scope=${WeChatAuthScope}&response_type=code&state=${AuthState}#wechat_redirect`;
return `${WeChatAuthUri}?appid=${provider.clientId}&redirect_uri=${redirectUri}&scope=${WeChatAuthScope}&response_type=code&state=${AuthState}#wechat_redirect`;
}
}

71
web/src/common/AuthBox.js Normal file
View File

@ -0,0 +1,71 @@
// 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.
import React from "react";
import {withRouter} from "react-router-dom";
import * as Setting from "../Setting";
import * as AccountBackend from "../backend/AccountBackend";
class CallbackBox extends React.Component {
constructor(props) {
super(props);
this.state = {
classes: props,
providerType: props.match.params.providerType,
providerName: props.match.params.providerName,
addition: props.match.params.addition,
state: "",
code: "",
isAuthenticated: false,
isSignedUp: false,
email: ""
};
const params = new URLSearchParams(this.props.location.search);
this.state.code = params.get("code");
this.state.state = params.get("state");
}
getAuthenticatedInfo() {
let redirectUrl;
redirectUrl = `${Setting.ClientUrl}/callback/${this.state.providerType}/${this.state.providerName}/${this.state.addition}`;
switch (this.state.providerType) {
case "github":
AccountBackend.githubLogin(this.state.providerName, this.state.code, this.state.state, redirectUrl, this.state.addition)
.then((res) => {
if (res.status === "ok") {
window.location.href = '/';
}else {
Setting.showMessage("error", res?.msg);
}
});
break;
}
}
componentDidMount() {
this.getAuthenticatedInfo();
}
render() {
return (
<div>
<h3>
Logging in ...
</h3>
</div>
)
}
}
export default withRouter(CallbackBox);