2021-03-13 23:06:03 +08:00
// Copyright 2021 The casbin Authors. All Rights Reserved.
2021-02-14 00:22:24 +08:00
//
// 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 (
2021-03-15 00:49:16 +08:00
"encoding/json"
2021-02-14 00:22:24 +08:00
"fmt"
"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"
)
2021-03-20 22:34:22 +08:00
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 }
}
}
2021-03-20 10:51:00 +08:00
func ( c * ApiController ) HandleLoggedIn ( userId string , form * RequestForm ) * Response {
resp := & Response { }
if form . Type == ResponseTypeLogin {
c . SetSessionUser ( userId )
util . LogInfo ( c . Ctx , "API: [%s] signed in" , userId )
2021-03-20 13:05:34 +08:00
resp = & Response { Status : "ok" , Msg : "" , Data : userId }
2021-03-20 10:51:00 +08:00
} else if form . Type == ResponseTypeCode {
clientId := c . Input ( ) . Get ( "clientId" )
responseType := c . Input ( ) . Get ( "responseType" )
redirectUri := c . Input ( ) . Get ( "redirectUri" )
scope := c . Input ( ) . Get ( "scope" )
state := c . Input ( ) . Get ( "state" )
code := object . GetOAuthCode ( userId , clientId , responseType , redirectUri , scope , state )
resp = codeToResponse ( code )
} else {
2021-03-28 08:59:12 +08:00
resp = & Response { Status : "error" , Msg : fmt . Sprintf ( "Unknown response type: %s" , form . Type ) }
2021-03-20 10:51:00 +08:00
}
return resp
}
func ( c * ApiController ) GetApplicationLogin ( ) {
var resp Response
clientId := c . Input ( ) . Get ( "clientId" )
responseType := c . Input ( ) . Get ( "responseType" )
redirectUri := c . Input ( ) . Get ( "redirectUri" )
scope := c . Input ( ) . Get ( "scope" )
state := c . Input ( ) . Get ( "state" )
msg , application := object . CheckOAuthLogin ( clientId , responseType , redirectUri , scope , state )
if msg != "" {
resp = Response { Status : "error" , Msg : msg , Data : application }
} else {
resp = Response { Status : "ok" , Msg : "" , Data : application }
}
c . Data [ "json" ] = resp
c . ServeJSON ( )
2021-03-15 19:11:41 +08:00
}
2021-03-20 00:23:37 +08:00
func ( c * ApiController ) Login ( ) {
2021-03-20 13:05:34 +08:00
resp := & Response { Status : "null" , Msg : "" }
2021-03-20 09:11:03 +08:00
var form RequestForm
2021-03-15 00:49:16 +08:00
err := json . Unmarshal ( c . Ctx . Input . RequestBody , & form )
if err != nil {
panic ( err )
}
2021-02-14 00:22:24 +08:00
2021-03-20 00:23:37 +08:00
if form . Username != "" {
2021-03-20 13:05:34 +08:00
if form . Type == ResponseTypeLogin {
if c . GetSessionUser ( ) != "" {
2021-03-28 08:59:12 +08:00
resp = & Response { Status : "error" , Msg : "Please log out first before signing in" , Data : c . GetSessionUser ( ) }
2021-03-20 13:05:34 +08:00
c . Data [ "json" ] = resp
c . ServeJSON ( )
return
}
2021-03-20 00:23:37 +08:00
}
2021-02-21 22:33:53 +08:00
2021-03-20 00:23:37 +08:00
userId := fmt . Sprintf ( "%s/%s" , form . Organization , form . Username )
password := form . Password
msg := object . CheckUserLogin ( userId , password )
2021-02-14 00:22:24 +08:00
2021-03-20 00:23:37 +08:00
if msg != "" {
2021-03-20 13:05:34 +08:00
resp = & Response { Status : "error" , Msg : msg , Data : "" }
2021-03-20 00:23:37 +08:00
} else {
2021-03-20 13:05:34 +08:00
resp = c . HandleLoggedIn ( userId , & form )
2021-03-20 00:23:37 +08:00
}
} else if form . Provider != "" {
application := object . GetApplication ( fmt . Sprintf ( "admin/%s" , form . Application ) )
provider := object . GetProvider ( fmt . Sprintf ( "admin/%s" , form . Provider ) )
2021-02-14 00:22:24 +08:00
2021-03-23 23:23:59 +08:00
idProvider := idp . GetIdProvider ( provider . Type , provider . ClientId , provider . ClientSecret , form . RedirectUri )
idProvider . SetHttpClient ( httpClient )
2021-02-14 00:22:24 +08:00
2021-03-20 23:50:34 +08:00
if form . State != beego . AppConfig . String ( "AuthState" ) && form . State != application . Name {
2021-03-23 23:23:59 +08:00
resp = & Response { Status : "error" , Msg : fmt . Sprintf ( "state expected: \"%s\", but got: \"%s\"" , beego . AppConfig . String ( "AuthState" ) , form . State ) }
2021-03-20 23:50:34 +08:00
c . Data [ "json" ] = resp
2021-03-20 00:23:37 +08:00
c . ServeJSON ( )
return
}
2021-02-14 00:22:24 +08:00
2021-03-20 00:23:37 +08:00
// https://github.com/golang/oauth2/issues/123#issuecomment-103715338
2021-03-23 23:23:59 +08:00
token , err := idProvider . GetToken ( form . Code )
2021-03-20 00:23:37 +08:00
if err != nil {
2021-03-25 23:22:34 +08:00
resp = & Response { Status : "error" , Msg : err . Error ( ) }
c . Data [ "json" ] = resp
c . ServeJSON ( )
return
2021-03-20 00:23:37 +08:00
}
2021-02-14 00:22:24 +08:00
2021-03-20 00:23:37 +08:00
if ! token . Valid ( ) {
2021-03-28 08:59:12 +08:00
resp = & Response { Status : "error" , Msg : "Invalid token" }
2021-03-20 00:23:37 +08:00
c . Data [ "json" ] = resp
c . ServeJSON ( )
return
2021-02-21 23:51:40 +08:00
}
2021-03-23 23:23:59 +08:00
userInfo , err := idProvider . GetUserInfo ( token )
2021-03-20 00:23:37 +08:00
if err != nil {
2021-03-28 08:59:12 +08:00
resp = & Response { Status : "error" , Msg : fmt . Sprintf ( "Failed to login in: %s" , err . Error ( ) ) }
2021-03-20 00:23:37 +08:00
c . Data [ "json" ] = resp
c . ServeJSON ( )
return
}
2021-02-21 23:51:40 +08:00
2021-03-20 00:23:37 +08:00
if form . Method == "signup" {
2021-03-24 00:11:20 +08:00
userId := object . GetUserIdByField ( application , provider . Type , userInfo . Username )
2021-03-20 00:23:37 +08:00
if userId != "" {
//if object.IsForbidden(userId) {
// c.forbiddenAccountResp(userId)
// return
//}
2021-02-21 23:51:40 +08:00
2021-03-20 00:23:37 +08:00
//if len(object.GetMemberAvatar(userId)) == 0 {
// avatar := UploadAvatarToOSS(res.Avatar, userId)
// object.LinkMemberAccount(userId, "avatar", avatar)
//}
2021-02-21 23:51:40 +08:00
2021-03-20 13:05:34 +08:00
resp = c . HandleLoggedIn ( userId , & form )
2021-03-20 00:23:37 +08:00
} else {
2021-03-26 21:55:39 +08:00
//if userId := object.GetUserIdByField(application, "email", userInfo.Email); userId != "" {
// resp = c.HandleLoggedIn(userId, &form)
//
// object.LinkUserAccount(userId, provider.Type, userInfo.Username)
2021-03-20 00:23:37 +08:00
//}
2021-03-26 21:55:39 +08:00
if ! application . EnableSignUp {
2021-03-28 08:59:12 +08:00
resp = & Response { Status : "error" , Msg : fmt . Sprintf ( "The account for provider: %s and username: %s does not exist and is not allowed to register as new account, please contact your IT support" , provider . Type , userInfo . Username ) }
2021-03-26 21:55:39 +08:00
c . Data [ "json" ] = resp
c . ServeJSON ( )
return
} else {
2021-03-28 08:59:12 +08:00
resp = & Response { Status : "error" , Msg : fmt . Sprintf ( "The account for provider: %s and username: %s does not exist, please register an account first" , provider . Type , userInfo . Username ) }
2021-03-26 21:55:39 +08:00
c . Data [ "json" ] = resp
c . ServeJSON ( )
return
2021-02-21 23:51:40 +08:00
}
2021-02-14 00:22:24 +08:00
}
2021-03-20 13:05:34 +08:00
//resp = &Response{Status: "ok", Msg: "", Data: res}
2021-02-14 00:22:24 +08:00
} else {
2021-03-20 00:23:37 +08:00
userId := c . GetSessionUser ( )
if userId == "" {
2021-03-28 08:59:12 +08:00
resp = & Response { Status : "error" , Msg : "The account does not exist" , Data : userInfo }
2021-03-20 00:23:37 +08:00
c . Data [ "json" ] = resp
c . ServeJSON ( )
return
}
2021-03-24 00:11:20 +08:00
isLinked := object . LinkUserAccount ( userId , provider . Type , userInfo . Username )
if isLinked {
resp = & Response { Status : "ok" , Msg : "" , Data : isLinked }
2021-03-20 00:23:37 +08:00
} else {
2021-03-28 08:59:12 +08:00
resp = & Response { Status : "error" , Msg : "Failed to link user account" , Data : isLinked }
2021-03-20 00:23:37 +08:00
}
//if len(object.GetMemberAvatar(userId)) == 0 {
// avatar := UploadAvatarToOSS(tempUserAccount.AvatarUrl, userId)
// object.LinkUserAccount(userId, "avatar", avatar)
//}
2021-02-14 00:22:24 +08:00
}
2021-03-20 00:23:37 +08:00
} else {
panic ( "unknown authentication type (not password or provider)" )
2021-02-14 00:22:24 +08:00
}
c . Data [ "json" ] = resp
c . ServeJSON ( )
}