2021-02-14 00:22:24 +08:00
|
|
|
// Copyright 2020 The casbin Authors. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/astaxie/beego"
|
2021-02-21 22:33:53 +08:00
|
|
|
"github.com/casdoor/casdoor/idp"
|
2021-02-14 00:22:24 +08:00
|
|
|
"github.com/casdoor/casdoor/object"
|
|
|
|
"github.com/casdoor/casdoor/util"
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
)
|
|
|
|
|
2021-02-14 14:34:03 +08:00
|
|
|
func (c *ApiController) AuthLogin() {
|
2021-02-14 21:21:42 +08:00
|
|
|
applicationName := c.Input().Get("application")
|
2021-02-14 00:22:24 +08:00
|
|
|
providerName := c.Input().Get("provider")
|
|
|
|
code := c.Input().Get("code")
|
|
|
|
state := c.Input().Get("state")
|
2021-02-14 15:45:48 +08:00
|
|
|
method := c.Input().Get("method")
|
2021-02-21 22:33:53 +08:00
|
|
|
redirectUrl := c.Input().Get("redirect_url")
|
2021-02-14 00:22:24 +08:00
|
|
|
|
2021-02-14 21:21:42 +08:00
|
|
|
application := object.GetApplication(fmt.Sprintf("admin/%s", applicationName))
|
2021-02-14 00:22:24 +08:00
|
|
|
provider := object.GetProvider(fmt.Sprintf("admin/%s", providerName))
|
2021-02-21 22:33:53 +08:00
|
|
|
|
|
|
|
idProvider := idp.GetIdProvider(provider.Type)
|
|
|
|
oauthConfig := idProvider.GetConfig()
|
|
|
|
oauthConfig.ClientID = provider.ClientId
|
|
|
|
oauthConfig.ClientSecret = provider.ClientSecret
|
|
|
|
oauthConfig.RedirectURL = redirectUrl
|
2021-02-14 00:22:24 +08:00
|
|
|
|
|
|
|
var resp Response
|
|
|
|
var res authResponse
|
|
|
|
res.IsAuthenticated = true
|
|
|
|
|
|
|
|
if state != beego.AppConfig.String("AuthState") {
|
|
|
|
res.IsAuthenticated = false
|
|
|
|
resp = Response{Status: "fail", Msg: "unauthorized", Data: res}
|
|
|
|
c.ServeJSON()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/golang/oauth2/issues/123#issuecomment-103715338
|
|
|
|
ctx := context.WithValue(oauth2.NoContext, oauth2.HTTPClient, httpClient)
|
2021-02-21 22:33:53 +08:00
|
|
|
token, err := oauthConfig.Exchange(ctx, code)
|
2021-02-14 00:22:24 +08:00
|
|
|
if err != nil {
|
|
|
|
res.IsAuthenticated = false
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !token.Valid() {
|
|
|
|
resp = Response{Status: "fail", Msg: "unauthorized", Data: res}
|
|
|
|
c.Data["json"] = resp
|
|
|
|
c.ServeJSON()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(2)
|
|
|
|
go func() {
|
2021-02-21 22:33:53 +08:00
|
|
|
res.Email = idProvider.GetEmail(httpClient, token)
|
2021-02-14 00:22:24 +08:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
go func() {
|
2021-02-21 22:33:53 +08:00
|
|
|
res.Method, res.Avatar = idProvider.GetLoginAndAvatar(httpClient, token)
|
2021-02-14 00:22:24 +08:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
wg.Wait()
|
|
|
|
|
2021-02-14 15:45:48 +08:00
|
|
|
if method == "signup" {
|
2021-02-21 22:33:53 +08:00
|
|
|
userId := object.HasGithub(application, res.Method)
|
2021-02-14 00:22:24 +08:00
|
|
|
if userId != "" {
|
|
|
|
//if len(object.GetMemberAvatar(userId)) == 0 {
|
|
|
|
// avatar := UploadAvatarToOSS(tempUserAccount.AvatarUrl, userId)
|
|
|
|
// object.LinkMemberAccount(userId, "avatar", avatar)
|
|
|
|
//}
|
|
|
|
c.SetSessionUser(userId)
|
|
|
|
util.LogInfo(c.Ctx, "API: [%s] signed in", userId)
|
|
|
|
res.IsSignedUp = true
|
|
|
|
} else {
|
2021-02-14 21:21:42 +08:00
|
|
|
if userId := object.HasMail(application, res.Email); userId != "" {
|
2021-02-14 00:22:24 +08:00
|
|
|
c.SetSessionUser(userId)
|
|
|
|
util.LogInfo(c.Ctx, "API: [%s] signed in", userId)
|
|
|
|
res.IsSignedUp = true
|
2021-02-21 22:33:53 +08:00
|
|
|
_ = object.LinkUserAccount(userId, "github", res.Method)
|
2021-02-14 00:22:24 +08:00
|
|
|
} else {
|
|
|
|
res.IsSignedUp = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resp = Response{Status: "ok", Msg: "success", Data: res}
|
|
|
|
} else {
|
|
|
|
memberId := c.GetSessionUser()
|
|
|
|
if memberId == "" {
|
|
|
|
resp = Response{Status: "fail", Msg: "no account exist", Data: res}
|
|
|
|
c.Data["json"] = resp
|
|
|
|
c.ServeJSON()
|
|
|
|
return
|
|
|
|
}
|
2021-02-21 22:33:53 +08:00
|
|
|
linkRes := object.LinkUserAccount(memberId, "github_account", res.Method)
|
2021-02-14 00:22:24 +08:00
|
|
|
if linkRes {
|
|
|
|
resp = Response{Status: "ok", Msg: "success", Data: linkRes}
|
|
|
|
} else {
|
|
|
|
resp = Response{Status: "fail", Msg: "link account failed", Data: linkRes}
|
|
|
|
}
|
|
|
|
//if len(object.GetMemberAvatar(memberId)) == 0 {
|
|
|
|
// avatar := UploadAvatarToOSS(tempUserAccount.AvatarUrl, memberId)
|
|
|
|
// object.LinkUserAccount(memberId, "avatar", avatar)
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Data["json"] = resp
|
|
|
|
c.ServeJSON()
|
|
|
|
}
|