2021-06-01 20:51:13 +08:00
|
|
|
// Copyright 2021 The casbin Authors. All Rights Reserved.
|
2020-06-09 12:22:44 +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
|
|
|
|
|
2021-06-10 15:47:08 +08:00
|
|
|
import (
|
2021-10-19 19:08:00 +08:00
|
|
|
_ "embed"
|
2022-04-21 21:31:50 +08:00
|
|
|
"strings"
|
2021-10-19 19:08:00 +08:00
|
|
|
|
2021-08-26 00:22:28 +08:00
|
|
|
"github.com/astaxie/beego"
|
2021-06-10 15:47:08 +08:00
|
|
|
"github.com/casbin/casnode/object"
|
2021-08-21 21:07:41 +08:00
|
|
|
"github.com/casbin/casnode/util"
|
2022-09-29 00:37:37 +08:00
|
|
|
"github.com/casdoor/casdoor-go-sdk/casdoorsdk"
|
2021-06-10 15:47:08 +08:00
|
|
|
)
|
2020-07-26 23:07:20 +08:00
|
|
|
|
2021-10-19 19:08:00 +08:00
|
|
|
//go:embed token_jwt_key.pem
|
|
|
|
var JwtPublicKey string
|
|
|
|
|
2021-08-07 12:21:53 +08:00
|
|
|
func init() {
|
2021-11-20 00:29:24 +08:00
|
|
|
InitAuthConfig()
|
|
|
|
}
|
|
|
|
|
|
|
|
func InitAuthConfig() {
|
2022-04-21 21:31:50 +08:00
|
|
|
casdoorEndpoint := strings.TrimRight(beego.AppConfig.String("casdoorEndpoint"), "/")
|
2021-11-26 18:29:16 +08:00
|
|
|
clientId := beego.AppConfig.String("clientId")
|
|
|
|
clientSecret := beego.AppConfig.String("clientSecret")
|
|
|
|
casdoorOrganization := beego.AppConfig.String("casdoorOrganization")
|
|
|
|
casdoorApplication := beego.AppConfig.String("casdoorApplication")
|
2021-11-20 00:29:24 +08:00
|
|
|
|
2022-09-29 00:37:37 +08:00
|
|
|
casdoorsdk.InitConfig(casdoorEndpoint, clientId, clientSecret, JwtPublicKey, casdoorOrganization, casdoorApplication)
|
2021-08-07 12:21:53 +08:00
|
|
|
}
|
|
|
|
|
2020-06-09 12:22:44 +08:00
|
|
|
// @Title Signin
|
|
|
|
// @Description sign in as a member
|
2021-06-17 19:44:54 +08:00
|
|
|
// @Param code QueryString string true "The code to sign in"
|
|
|
|
// @Param state QueryString string true "The state"
|
2020-06-09 12:22:44 +08:00
|
|
|
// @Success 200 {object} controllers.api_controller.Response The Response object
|
|
|
|
// @router /signin [post]
|
2021-12-06 18:27:23 +08:00
|
|
|
// @Tag Account API
|
2021-05-30 00:27:10 +08:00
|
|
|
func (c *ApiController) Signin() {
|
2021-06-01 20:51:13 +08:00
|
|
|
code := c.Input().Get("code")
|
|
|
|
state := c.Input().Get("state")
|
2020-06-09 12:22:44 +08:00
|
|
|
|
2022-09-29 00:37:37 +08:00
|
|
|
token, err := casdoorsdk.GetOAuthToken(code, state)
|
2020-06-09 12:22:44 +08:00
|
|
|
if err != nil {
|
2021-12-28 20:35:51 +08:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2020-06-09 12:22:44 +08:00
|
|
|
}
|
|
|
|
|
2022-09-29 00:37:37 +08:00
|
|
|
claims, err := casdoorsdk.ParseJwtToken(token.AccessToken)
|
2021-06-01 20:51:13 +08:00
|
|
|
if err != nil {
|
2021-12-28 20:35:51 +08:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2020-07-26 23:07:20 +08:00
|
|
|
}
|
|
|
|
|
2021-08-22 10:35:04 +08:00
|
|
|
affected, err := object.UpdateMemberOnlineStatus(&claims.User, true, util.GetCurrentTime())
|
2021-08-21 21:07:41 +08:00
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2021-06-10 15:47:08 +08:00
|
|
|
|
2021-06-01 20:51:13 +08:00
|
|
|
claims.AccessToken = token.AccessToken
|
2021-08-22 10:35:04 +08:00
|
|
|
c.SetSessionClaims(claims)
|
2020-06-09 12:22:44 +08:00
|
|
|
|
2021-08-21 21:07:41 +08:00
|
|
|
c.ResponseOk(claims, affected)
|
2020-06-09 12:22:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// @Title Signout
|
|
|
|
// @Description sign out the current member
|
|
|
|
// @Success 200 {object} controllers.api_controller.Response The Response object
|
|
|
|
// @router /signout [post]
|
2021-12-06 18:27:23 +08:00
|
|
|
// @Tag Account API
|
2021-05-30 00:27:10 +08:00
|
|
|
func (c *ApiController) Signout() {
|
2021-08-22 10:35:04 +08:00
|
|
|
claims := c.GetSessionClaims()
|
|
|
|
if claims != nil {
|
|
|
|
_, err := object.UpdateMemberOnlineStatus(&claims.User, false, util.GetCurrentTime())
|
2021-08-21 21:07:41 +08:00
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2021-06-10 15:47:08 +08:00
|
|
|
}
|
|
|
|
|
2021-08-22 10:35:04 +08:00
|
|
|
c.SetSessionClaims(nil)
|
2020-06-09 12:22:44 +08:00
|
|
|
|
2021-08-21 21:07:41 +08:00
|
|
|
c.ResponseOk()
|
2020-06-09 12:22:44 +08:00
|
|
|
}
|
|
|
|
|
2021-06-17 19:44:54 +08:00
|
|
|
// @Title GetAccount
|
|
|
|
// @Description Get current account
|
|
|
|
// @Success 200 {object} controllers.api_controller.Response The Response object
|
|
|
|
// @router /get-account [get]
|
2021-12-06 18:27:23 +08:00
|
|
|
// @Tag Account API
|
2021-05-30 00:27:10 +08:00
|
|
|
func (c *ApiController) GetAccount() {
|
2021-08-07 14:08:35 +08:00
|
|
|
if c.RequireSignedIn() {
|
2020-12-28 15:09:21 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-22 10:35:04 +08:00
|
|
|
claims := c.GetSessionClaims()
|
2020-08-06 15:47:30 +08:00
|
|
|
|
2021-08-21 21:07:41 +08:00
|
|
|
c.ResponseOk(claims)
|
2021-05-06 23:27:02 +08:00
|
|
|
}
|
2021-06-16 16:06:02 +08:00
|
|
|
|
2021-08-28 14:06:13 +08:00
|
|
|
func (c *ApiController) UpdateAccountBalance(amount int) {
|
2021-08-22 10:35:04 +08:00
|
|
|
user := c.GetSessionUser()
|
2021-08-28 14:06:13 +08:00
|
|
|
user.Score += amount
|
2021-08-22 10:35:04 +08:00
|
|
|
c.SetSessionUser(user)
|
2021-06-16 16:06:02 +08:00
|
|
|
}
|
2022-03-02 19:13:58 +08:00
|
|
|
|
|
|
|
func (c *ApiController) UpdateAccountConsumptionSum(amount int) {
|
|
|
|
user := c.GetSessionUser()
|
|
|
|
user.Karma += amount
|
|
|
|
c.SetSessionUser(user)
|
|
|
|
}
|