diff --git a/object/application.go b/object/application.go index dcec5a9c..8d9e810f 100644 --- a/object/application.go +++ b/object/application.go @@ -49,6 +49,21 @@ func GetApplications(owner string) []*Application { return applications } +func extendApplication(application *Application) { + providers := GetProviders(application.Owner) + m := map[string]*Provider{} + for _, provider := range providers { + provider.ClientSecret = "" + provider.ProviderUrl = "" + m[provider.Name] = provider + } + + application.ProviderObjs = []*Provider{} + for _, providerName := range application.Providers { + application.ProviderObjs = append(application.ProviderObjs, m[providerName]) + } +} + func getApplication(owner string, name string) *Application { application := Application{Owner: owner, Name: name} existed, err := adapter.engine.Get(&application) @@ -57,18 +72,7 @@ func getApplication(owner string, name string) *Application { } if existed { - providers := GetProviders(owner) - m := map[string]*Provider{} - for _, provider := range providers { - provider.ClientSecret = "" - provider.ProviderUrl = "" - m[provider.Name] = provider - } - - application.ProviderObjs = []*Provider{} - for _, providerName := range application.Providers { - application.ProviderObjs = append(application.ProviderObjs, m[providerName]) - } + extendApplication(&application) return &application } else { return nil @@ -83,6 +87,7 @@ func getApplicationByClientId(clientId string) *Application { } if existed { + extendApplication(&application) return &application } else { return nil diff --git a/web/src/App.js b/web/src/App.js index d5586286..51950181 100644 --- a/web/src/App.js +++ b/web/src/App.js @@ -327,14 +327,14 @@ class App extends Component { } isDoorPages() { - return window.location.pathname.startsWith('/doors/'); + return window.location.pathname.startsWith('/login/oauth'); } render() { if (this.isDoorPages()) { return ( - this.renderLoginIfNotLoggedIn()}/> + this.renderLoginIfNotLoggedIn()}/> ) } diff --git a/web/src/ApplicationEditPage.js b/web/src/ApplicationEditPage.js index 4a5fa71a..20a9917d 100644 --- a/web/src/ApplicationEditPage.js +++ b/web/src/ApplicationEditPage.js @@ -247,15 +247,15 @@ class ApplicationEditPage extends React.Component { {i18next.t("application:Face Preview")}: - + { - `${window.location.host}/doors/${this.state.application.name}` + `${window.location.host}/login/oauth?client_id=${this.state.application.clientId}&response_type=code&redirect_uri=${this.state.application.redirectUris[0]}&scope=read&state=casdoor` }

- +
diff --git a/web/src/auth/AuthBackend.js b/web/src/auth/AuthBackend.js index 51cf2c24..88271239 100644 --- a/web/src/auth/AuthBackend.js +++ b/web/src/auth/AuthBackend.js @@ -29,6 +29,13 @@ export function register(values) { }).then(res => res.json()); } +export function getApplicationLogin(clientId, responseType, redirectUri, scope, state) { + return fetch(`${authConfig.serverUrl}/api/get-app-login?clientId=${clientId}&responseType=${responseType}&redirectUri=${redirectUri}&scope=${scope}&state=${state}`, { + method: 'GET', + credentials: 'include', + }).then(res => res.json()); +} + export function login(values) { return fetch(`${authConfig.serverUrl}/api/login`, { method: 'POST', diff --git a/web/src/auth/Face.js b/web/src/auth/Face.js index 036f19de..ff41ecd0 100644 --- a/web/src/auth/Face.js +++ b/web/src/auth/Face.js @@ -13,7 +13,7 @@ // limitations under the License. import React from "react"; -import {Button, Checkbox, Col, Form, Input, Row} from "antd"; +import {Alert, Button, Checkbox, Col, Form, Input, Row} from "antd"; import {LockOutlined, UserOutlined} from "@ant-design/icons"; import * as AuthBackend from "./AuthBackend"; import * as Provider from "./Provider"; @@ -22,21 +22,46 @@ import * as Util from "./Util"; class Face extends React.Component { constructor(props) { super(props); - const queries = new URLSearchParams(window.location.search); this.state = { classes: props, + type: props.type, applicationName: props.applicationName !== undefined ? props.applicationName : (props.match === undefined ? null : props.match.params.applicationName), application: null, - clientId: queries.get("client_id"), - responseType: queries.get("response_type"), - redirectUri: queries.get("redirect_uri"), - scope: queries.get("scope"), - state: queries.get("state"), + msg: null, }; } componentWillMount() { - this.getApplication(); + if (this.state.type === "login") { + this.getApplication(); + } else if (this.state.type === "code") { + this.getApplicationLogin(); + } else { + Util.showMessage("error", `Unknown authentication type: ${this.state.type}`); + } + } + + getApplicationLogin() { + const queries = new URLSearchParams(window.location.search); + const clientId = queries.get("client_id"); + const responseType = queries.get("response_type"); + const redirectUri = queries.get("redirect_uri"); + const scope = queries.get("scope"); + const state = queries.get("state"); + AuthBackend.getApplicationLogin(clientId, responseType, redirectUri, scope, state) + .then((res) => { + if (res.status === "ok") { + this.setState({ + application: res.data, + }); + } else { + // Util.showMessage("error", res.msg); + this.setState({ + application: res.data, + msg: res.msg, + }); + } + }); } getApplication() { @@ -74,6 +99,24 @@ class Face extends React.Component { }; renderForm(application) { + if (this.state.msg !== null) { + return ( +
+ + Detail + + } + /> +
+ ) + } + return (
- +
{ this.renderLogo(application) diff --git a/web/src/auth/LoginPage.js b/web/src/auth/LoginPage.js index d66a18ea..fe3fd0cc 100644 --- a/web/src/auth/LoginPage.js +++ b/web/src/auth/LoginPage.js @@ -19,7 +19,7 @@ import {authConfig} from "./Auth"; class LoginPage extends React.Component { render() { return ( - + ) } }