Fix AuthCallback's code handling.

This commit is contained in:
Yang Luo
2021-03-21 00:38:00 +08:00
parent 8b921b2c1e
commit 7f367ae346
4 changed files with 38 additions and 7 deletions

View File

@ -68,6 +68,7 @@ m = (r.subOwner == p.subOwner || p.subOwner == "*") && (r.subName == p.subName |
p, built-in, *, *, *, *, * p, built-in, *, *, *, *, *
p, *, *, POST, /api/register, *, * p, *, *, POST, /api/register, *, *
p, *, *, POST, /api/login, *, * p, *, *, POST, /api/login, *, *
p, *, *, GET, /api/get-app-login, *, *
p, *, *, POST, /api/logout, *, * p, *, *, POST, /api/logout, *, *
p, *, *, GET, /api/get-account, *, * p, *, *, GET, /api/get-account, *, *
p, *, *, POST, /api/login/oauth/access_token, *, * p, *, *, POST, /api/login/oauth/access_token, *, *

View File

@ -334,7 +334,7 @@ class App extends Component {
if (this.isDoorPages()) { if (this.isDoorPages()) {
return ( return (
<Switch> <Switch>
<Route exact path="/login/oauth/authorize" render={(props) => this.renderLoginIfNotLoggedIn(<Face type={"code"} {...props} />)}/> <Route exact path="/login/oauth/authorize" render={(props) => <Face type={"code"} {...props} />}/>
</Switch> </Switch>
) )
} }

View File

@ -17,6 +17,7 @@ import {Spin} from "antd";
import {withRouter} from "react-router-dom"; import {withRouter} from "react-router-dom";
import * as AuthBackend from "./AuthBackend"; import * as AuthBackend from "./AuthBackend";
import * as Util from "./Util"; import * as Util from "./Util";
import {authConfig} from "./Auth";
class AuthCallback extends React.Component { class AuthCallback extends React.Component {
constructor(props) { constructor(props) {
@ -29,10 +30,29 @@ class AuthCallback extends React.Component {
}; };
} }
getResponseType() {
// "http://localhost:8000"
const authServerUrl = authConfig.serverUrl;
// For example, for Casbin-OA, realRedirectUri = "http://localhost:9000/login"
// realRedirectUrl = "http://localhost:9000"
const params = new URLSearchParams(this.props.location.search);
const realRedirectUri = params.get("redirect_uri");
const realRedirectUrl = new URL(realRedirectUri).origin;
// For Casdoor itself, we use "login" directly
if (authServerUrl === realRedirectUrl) {
return "login";
} else {
return "code";
}
}
componentWillMount() { componentWillMount() {
const params = new URLSearchParams(this.props.location.search); const params = new URLSearchParams(this.props.location.search);
let redirectUri = `${window.location.origin}/callback/${this.state.applicationName}/${this.state.providerName}/${this.state.method}`; let redirectUri = `${window.location.origin}/callback/${this.state.applicationName}/${this.state.providerName}/${this.state.method}`;
const body = { const body = {
type: this.getResponseType(),
application: this.state.applicationName, application: this.state.applicationName,
provider: this.state.providerName, provider: this.state.providerName,
code: params.get("code"), code: params.get("code"),
@ -43,10 +63,19 @@ class AuthCallback extends React.Component {
const oAuthParams = Util.getOAuthGetParameters(); const oAuthParams = Util.getOAuthGetParameters();
AuthBackend.login(body, oAuthParams) AuthBackend.login(body, oAuthParams)
.then((res) => { .then((res) => {
if (res.status === "ok") { if (res.status === 'ok') {
window.location.href = '/'; const responseType = this.getResponseType();
if (responseType === "login") {
// this.props.onLoggedIn();
Util.showMessage("success", `Logged in successfully`);
Util.goToLink("/");
} else if (responseType === "code") {
const code = res.data;
Util.goToLink(`${oAuthParams.redirectUri}?code=${code}&state=${oAuthParams.state}`);
// Util.showMessage("success", `Authorization code: ${res.data}`);
}
} else { } else {
Util.showMessage("error", res?.msg); Util.showMessage("error", `Log in failed${res.msg}`);
} }
}); });
} }

View File

@ -86,11 +86,12 @@ class Face extends React.Component {
AuthBackend.login(values, oAuthParams) AuthBackend.login(values, oAuthParams)
.then((res) => { .then((res) => {
if (res.status === 'ok') { if (res.status === 'ok') {
if (this.state.type === "login") { const responseType = this.state.type;
if (responseType === "login") {
this.props.onLoggedIn(); this.props.onLoggedIn();
Util.showMessage("success", `Logged in successfully`); Util.showMessage("success", `Logged in successfully`);
Util.goToLink("/"); Util.goToLink("/");
} else if (this.state.type === "code") { } else if (responseType === "code") {
const code = res.data; const code = res.data;
Util.goToLink(`${oAuthParams.redirectUri}?code=${code}&state=${oAuthParams.state}`); Util.goToLink(`${oAuthParams.redirectUri}?code=${code}&state=${oAuthParams.state}`);
// Util.showMessage("success", `Authorization code: ${res.data}`); // Util.showMessage("success", `Authorization code: ${res.data}`);
@ -221,7 +222,7 @@ class Face extends React.Component {
{ {
application.providerObjs.map(provider => { application.providerObjs.map(provider => {
return ( return (
<a href={Provider.getAuthUrl(application, provider, "signup")}> <a key={provider.displayName} href={Provider.getAuthUrl(application, provider, "signup")}>
<img width={30} height={30} src={Provider.getAuthLogo(provider)} alt={provider.displayName} style={{margin: "3px"}} /> <img width={30} height={30} src={Provider.getAuthLogo(provider)} alt={provider.displayName} style={{margin: "3px"}} />
</a> </a>
); );