Finish /login/oauth/authorize

This commit is contained in:
Yang Luo 2021-03-20 22:34:22 +08:00
parent f89f454e0e
commit 808e6c6283
9 changed files with 22 additions and 26 deletions

View File

@ -70,9 +70,7 @@ p, *, *, POST, /api/register, *, *
p, *, *, POST, /api/login, *, * p, *, *, POST, /api/login, *, *
p, *, *, POST, /api/logout, *, * p, *, *, POST, /api/logout, *, *
p, *, *, GET, /api/get-account, *, * p, *, *, GET, /api/get-account, *, *
p, *, *, GET, /api/auth/login, *, * p, *, *, POST, /api/login/oauth/access_token, *, *
p, *, *, GET, /api/oauth/code, *, *
p, *, *, GET, /api/oauth/token, *, *
p, *, *, GET, /api/get-application, *, * p, *, *, GET, /api/get-application, *, *
p, *, *, GET, /api/get-users, *, * p, *, *, GET, /api/get-users, *, *
p, *, *, GET, /api/get-user, *, * p, *, *, GET, /api/get-user, *, *

View File

@ -26,6 +26,14 @@ import (
"golang.org/x/oauth2" "golang.org/x/oauth2"
) )
func codeToResponse(code *object.Code) *Response {
if code.Code == "" {
return &Response{Status: "error", Msg: code.Message, Data: code.Code}
} else {
return &Response{Status: "ok", Msg: "", Data: code.Code}
}
}
func (c *ApiController) HandleLoggedIn(userId string, form *RequestForm) *Response { func (c *ApiController) HandleLoggedIn(userId string, form *RequestForm) *Response {
resp := &Response{} resp := &Response{}
if form.Type == ResponseTypeLogin { if form.Type == ResponseTypeLogin {

View File

@ -69,14 +69,6 @@ func (c *ApiController) DeleteToken() {
c.ServeJSON() c.ServeJSON()
} }
func codeToResponse(code *object.Code) *Response {
if code.Code == "" {
return &Response{Status: "error", Msg: code.Message, Data: code.Code}
} else {
return &Response{Status: "ok", Msg: "", Data: code.Code}
}
}
func (c *ApiController) GetOAuthToken() { func (c *ApiController) GetOAuthToken() {
grantType := c.Input().Get("grant_type") grantType := c.Input().Get("grant_type")
clientId := c.Input().Get("client_id") clientId := c.Input().Get("client_id")

View File

@ -81,7 +81,8 @@ func getObject(ctx *context.Context) (string, string) {
var obj Object var obj Object
err := json.Unmarshal(body, &obj) err := json.Unmarshal(body, &obj)
if err != nil { if err != nil {
panic(err) //panic(err)
return "", ""
} }
return obj.Owner, obj.Name return obj.Owner, obj.Name
} }

View File

@ -70,5 +70,5 @@ func initAPI() {
beego.Router("/api/update-token", &controllers.ApiController{}, "POST:UpdateToken") beego.Router("/api/update-token", &controllers.ApiController{}, "POST:UpdateToken")
beego.Router("/api/add-token", &controllers.ApiController{}, "POST:AddToken") beego.Router("/api/add-token", &controllers.ApiController{}, "POST:AddToken")
beego.Router("/api/delete-token", &controllers.ApiController{}, "POST:DeleteToken") beego.Router("/api/delete-token", &controllers.ApiController{}, "POST:DeleteToken")
beego.Router("/api/oauth/token", &controllers.ApiController{}, "GET:GetOAuthToken") beego.Router("/api/login/oauth/access_token", &controllers.ApiController{}, "POST:GetOAuthToken")
} }

View File

@ -327,14 +327,14 @@ class App extends Component {
} }
isDoorPages() { isDoorPages() {
return window.location.pathname.startsWith('/login/oauth'); return window.location.pathname.startsWith("/login/oauth/authorize");
} }
render() { render() {
if (this.isDoorPages()) { if (this.isDoorPages()) {
return ( return (
<Switch> <Switch>
<Route exact path="/login/oauth" render={(props) => this.renderLoginIfNotLoggedIn(<Face type={"code"} {...props} />)}/> <Route exact path="/login/oauth/authorize" render={(props) => this.renderLoginIfNotLoggedIn(<Face type={"code"} {...props} />)}/>
</Switch> </Switch>
) )
} }

View File

@ -247,9 +247,9 @@ class ApplicationEditPage extends React.Component {
{i18next.t("application:Face Preview")}: {i18next.t("application:Face Preview")}:
</Col> </Col>
<Col span={22} > <Col span={22} >
<a style={{marginBottom: '10px'}} target="_blank" href={`/login/oauth?client_id=${this.state.application.clientId}&response_type=code&redirect_uri=${this.state.application.redirectUris[0]}&scope=read&state=casdoor`}> <a style={{marginBottom: '10px'}} target="_blank" href={`/login/oauth/authorize?client_id=${this.state.application.clientId}&response_type=code&redirect_uri=${this.state.application.redirectUris[0]}&scope=read&state=casdoor`}>
{ {
`${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` `${window.location.host}/login/oauth/authorize?client_id=${this.state.application.clientId}&response_type=code&redirect_uri=${this.state.application.redirectUris[0]}&scope=read&state=casdoor`
} }
</a> </a>
<br/> <br/>

View File

@ -21,28 +21,23 @@ import * as Util from "./Util";
class AuthCallback extends React.Component { class AuthCallback extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
const params = new URLSearchParams(this.props.location.search);
this.state = { this.state = {
classes: props, classes: props,
applicationName: props.match.params.applicationName, applicationName: props.match.params.applicationName,
providerName: props.match.params.providerName, providerName: props.match.params.providerName,
method: props.match.params.method, method: props.match.params.method,
state: params.get("state"),
code: params.get("code"),
isAuthenticated: false,
isSignedUp: false,
email: ""
}; };
} }
componentWillMount() { componentWillMount() {
const params = new URLSearchParams(this.props.location.search);
let redirectUri; let redirectUri;
redirectUri = `${window.location.origin}/callback/${this.state.applicationName}/${this.state.providerName}/${this.state.method}`; redirectUri = `${window.location.origin}/callback/${this.state.applicationName}/${this.state.providerName}/${this.state.method}`;
const body = { const body = {
application: this.state.applicationName, application: this.state.applicationName,
provider: this.state.providerName, provider: this.state.providerName,
code: this.state.code, code: params.get("code"),
state: this.state.state, state: params.get("state"),
redirectUri: redirectUri, redirectUri: redirectUri,
method: this.state.method, method: this.state.method,
}; };

View File

@ -91,7 +91,9 @@ class Face extends React.Component {
Util.showMessage("success", `Logged in successfully`); Util.showMessage("success", `Logged in successfully`);
Util.goToLink("/"); Util.goToLink("/");
} else if (this.state.type === "code") { } else if (this.state.type === "code") {
Util.showMessage("success", `Authorization code: ${res.data}`); 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", `Log in failed${res.msg}`); Util.showMessage("error", `Log in failed${res.msg}`);