mirror of
https://github.com/casdoor/casdoor.git
synced 2025-05-23 02:35:49 +08:00
Change /api/auth/login to POST.
This commit is contained in:
parent
2a481d6a2e
commit
4a170d1d56
@ -9,12 +9,21 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type RegisterForm struct {
|
type RegisterForm struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
|
||||||
Organization string `json:"organization"`
|
Organization string `json:"organization"`
|
||||||
Username string `json:"username"`
|
Username string `json:"username"`
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
Phone string `json:"phone"`
|
Phone string `json:"phone"`
|
||||||
|
|
||||||
|
Application string `json:"application"`
|
||||||
|
Provider string `json:"provider"`
|
||||||
|
Code string `json:"code"`
|
||||||
|
State string `json:"state"`
|
||||||
|
RedirectUri string `json:"redirectUri"`
|
||||||
|
Method string `json:"method"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Response struct {
|
type Response struct {
|
||||||
|
@ -16,6 +16,7 @@ package controllers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/astaxie/beego"
|
"github.com/astaxie/beego"
|
||||||
@ -26,27 +27,26 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (c *ApiController) AuthLogin() {
|
func (c *ApiController) AuthLogin() {
|
||||||
applicationName := c.Input().Get("application")
|
var form RegisterForm
|
||||||
providerName := c.Input().Get("provider")
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &form)
|
||||||
code := c.Input().Get("code")
|
if err != nil {
|
||||||
state := c.Input().Get("state")
|
panic(err)
|
||||||
method := c.Input().Get("method")
|
}
|
||||||
redirectUri := c.Input().Get("redirect_uri")
|
|
||||||
|
|
||||||
application := object.GetApplication(fmt.Sprintf("admin/%s", applicationName))
|
application := object.GetApplication(fmt.Sprintf("admin/%s", form.Application))
|
||||||
provider := object.GetProvider(fmt.Sprintf("admin/%s", providerName))
|
provider := object.GetProvider(fmt.Sprintf("admin/%s", form.Provider))
|
||||||
|
|
||||||
idProvider := idp.GetIdProvider(provider.Type)
|
idProvider := idp.GetIdProvider(provider.Type)
|
||||||
oauthConfig := idProvider.GetConfig()
|
oauthConfig := idProvider.GetConfig()
|
||||||
oauthConfig.ClientID = provider.ClientId
|
oauthConfig.ClientID = provider.ClientId
|
||||||
oauthConfig.ClientSecret = provider.ClientSecret
|
oauthConfig.ClientSecret = provider.ClientSecret
|
||||||
oauthConfig.RedirectURL = redirectUri
|
oauthConfig.RedirectURL = form.RedirectUri
|
||||||
|
|
||||||
var resp Response
|
var resp Response
|
||||||
var res authResponse
|
var res authResponse
|
||||||
res.IsAuthenticated = true
|
res.IsAuthenticated = true
|
||||||
|
|
||||||
if state != beego.AppConfig.String("AuthState") {
|
if form.State != beego.AppConfig.String("AuthState") {
|
||||||
res.IsAuthenticated = false
|
res.IsAuthenticated = false
|
||||||
resp = Response{Status: "error", Msg: "unauthorized", Data: res}
|
resp = Response{Status: "error", Msg: "unauthorized", Data: res}
|
||||||
c.ServeJSON()
|
c.ServeJSON()
|
||||||
@ -55,7 +55,7 @@ func (c *ApiController) AuthLogin() {
|
|||||||
|
|
||||||
// https://github.com/golang/oauth2/issues/123#issuecomment-103715338
|
// https://github.com/golang/oauth2/issues/123#issuecomment-103715338
|
||||||
ctx := context.WithValue(oauth2.NoContext, oauth2.HTTPClient, httpClient)
|
ctx := context.WithValue(oauth2.NoContext, oauth2.HTTPClient, httpClient)
|
||||||
token, err := oauthConfig.Exchange(ctx, code)
|
token, err := oauthConfig.Exchange(ctx, form.Code)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res.IsAuthenticated = false
|
res.IsAuthenticated = false
|
||||||
panic(err)
|
panic(err)
|
||||||
@ -76,7 +76,7 @@ func (c *ApiController) AuthLogin() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if method == "signup" {
|
if form.Method == "signup" {
|
||||||
userId := ""
|
userId := ""
|
||||||
if provider.Type == "github" {
|
if provider.Type == "github" {
|
||||||
userId = object.GetUserIdByField(application, "github", res.Method)
|
userId = object.GetUserIdByField(application, "github", res.Method)
|
||||||
|
@ -37,7 +37,7 @@ func initAPI() {
|
|||||||
beego.Router("/api/login", &controllers.ApiController{}, "POST:Login")
|
beego.Router("/api/login", &controllers.ApiController{}, "POST:Login")
|
||||||
beego.Router("/api/logout", &controllers.ApiController{}, "POST:Logout")
|
beego.Router("/api/logout", &controllers.ApiController{}, "POST:Logout")
|
||||||
beego.Router("/api/get-account", &controllers.ApiController{}, "GET:GetAccount")
|
beego.Router("/api/get-account", &controllers.ApiController{}, "GET:GetAccount")
|
||||||
beego.Router("/api/auth/login", &controllers.ApiController{}, "GET:AuthLogin")
|
beego.Router("/api/auth/login", &controllers.ApiController{}, "POST:AuthLogin")
|
||||||
|
|
||||||
beego.Router("/api/get-organizations", &controllers.ApiController{}, "GET:GetOrganizations")
|
beego.Router("/api/get-organizations", &controllers.ApiController{}, "GET:GetOrganizations")
|
||||||
beego.Router("/api/get-organization", &controllers.ApiController{}, "GET:GetOrganization")
|
beego.Router("/api/get-organization", &controllers.ApiController{}, "GET:GetOrganization")
|
||||||
|
@ -44,10 +44,11 @@ export function logout() {
|
|||||||
}).then(res => res.json());
|
}).then(res => res.json());
|
||||||
}
|
}
|
||||||
|
|
||||||
export function authLogin(applicationName, providerName, code, state, redirectUri, method) {
|
export function authLogin(values) {
|
||||||
return fetch(`${authConfig.serverUrl}/api/auth/login?application=${applicationName}&provider=${providerName}&code=${code}&state=${state}&redirect_url=${redirectUri}&method=${method}`, {
|
return fetch(`${authConfig.serverUrl}/api/auth/login`, {
|
||||||
method: 'GET',
|
method: 'POST',
|
||||||
credentials: 'include',
|
credentials: "include",
|
||||||
|
body: JSON.stringify(values),
|
||||||
}).then(res => res.json());
|
}).then(res => res.json());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,9 +13,10 @@
|
|||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import {message, Spin} from "antd";
|
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";
|
||||||
|
|
||||||
class AuthCallback extends React.Component {
|
class AuthCallback extends React.Component {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
@ -35,26 +36,22 @@ class AuthCallback extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
componentWillMount() {
|
componentWillMount() {
|
||||||
this.authLogin();
|
|
||||||
}
|
|
||||||
|
|
||||||
showMessage(type, text) {
|
|
||||||
if (type === "success") {
|
|
||||||
message.success(text);
|
|
||||||
} else if (type === "error") {
|
|
||||||
message.error(text);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
authLogin() {
|
|
||||||
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}`;
|
||||||
AuthBackend.authLogin(this.state.applicationName, this.state.providerName, this.state.code, this.state.state, redirectUri, this.state.method)
|
const body = {
|
||||||
|
application: this.state.applicationName,
|
||||||
|
provider: this.state.providerName,
|
||||||
|
code: this.state.code,
|
||||||
|
state: this.state.state,
|
||||||
|
redirectUri: redirectUri,
|
||||||
|
method: this.state.method,
|
||||||
|
};
|
||||||
|
AuthBackend.authLogin(body)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
if (res.status === "ok") {
|
if (res.status === "ok") {
|
||||||
window.location.href = '/';
|
window.location.href = '/';
|
||||||
} else {
|
} else {
|
||||||
this.showMessage("error", res?.msg);
|
Util.showMessage("error", res?.msg);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user