diff --git a/authz/authz.go b/authz/authz.go index 1ee7ef5c..883d731a 100644 --- a/authz/authz.go +++ b/authz/authz.go @@ -68,6 +68,7 @@ m = (r.subOwner == p.subOwner || p.subOwner == "*") && (r.subName == p.subName | p, built-in, *, *, *, *, * p, *, *, POST, /api/register, *, * p, *, *, POST, /api/login, *, * +p, *, *, GET, /api/get-app-login, *, * p, *, *, POST, /api/logout, *, * p, *, *, GET, /api/get-account, *, * p, *, *, POST, /api/login/oauth/access_token, *, * diff --git a/web/src/App.js b/web/src/App.js index 77cc9ad6..8eaed485 100644 --- a/web/src/App.js +++ b/web/src/App.js @@ -334,7 +334,7 @@ class App extends Component { if (this.isDoorPages()) { return ( - this.renderLoginIfNotLoggedIn()}/> + }/> ) } diff --git a/web/src/auth/AuthCallback.js b/web/src/auth/AuthCallback.js index bb32d3ca..8c1d6b5c 100644 --- a/web/src/auth/AuthCallback.js +++ b/web/src/auth/AuthCallback.js @@ -17,6 +17,7 @@ import {Spin} from "antd"; import {withRouter} from "react-router-dom"; import * as AuthBackend from "./AuthBackend"; import * as Util from "./Util"; +import {authConfig} from "./Auth"; class AuthCallback extends React.Component { 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() { const params = new URLSearchParams(this.props.location.search); let redirectUri = `${window.location.origin}/callback/${this.state.applicationName}/${this.state.providerName}/${this.state.method}`; const body = { + type: this.getResponseType(), application: this.state.applicationName, provider: this.state.providerName, code: params.get("code"), @@ -43,10 +63,19 @@ class AuthCallback extends React.Component { const oAuthParams = Util.getOAuthGetParameters(); AuthBackend.login(body, oAuthParams) .then((res) => { - if (res.status === "ok") { - window.location.href = '/'; + if (res.status === 'ok') { + 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 { - Util.showMessage("error", res?.msg); + Util.showMessage("error", `Log in failed:${res.msg}`); } }); } diff --git a/web/src/auth/Face.js b/web/src/auth/Face.js index 96acdac2..901b1586 100644 --- a/web/src/auth/Face.js +++ b/web/src/auth/Face.js @@ -86,11 +86,12 @@ class Face extends React.Component { AuthBackend.login(values, oAuthParams) .then((res) => { if (res.status === 'ok') { - if (this.state.type === "login") { + const responseType = this.state.type; + if (responseType === "login") { this.props.onLoggedIn(); Util.showMessage("success", `Logged in successfully`); Util.goToLink("/"); - } else if (this.state.type === "code") { + } else if (responseType === "code") { const code = res.data; Util.goToLink(`${oAuthParams.redirectUri}?code=${code}&state=${oAuthParams.state}`); // Util.showMessage("success", `Authorization code: ${res.data}`); @@ -221,7 +222,7 @@ class Face extends React.Component { { application.providerObjs.map(provider => { return ( - + {provider.displayName} );