forked from Open-CT/openscore
125 lines
3.1 KiB
Go
125 lines
3.1 KiB
Go
// Copyright 2022 The OpenCT 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 (
|
|
_ "embed"
|
|
"encoding/json"
|
|
|
|
"github.com/astaxie/beego"
|
|
auth "github.com/casdoor/casdoor-go-sdk/casdoorsdk"
|
|
"github.com/open-ct/openscore/service/user"
|
|
)
|
|
|
|
//go:embed token_jwt_key.pem
|
|
var JwtPublicKey string
|
|
|
|
func init() {
|
|
InitAuthConfig()
|
|
}
|
|
|
|
func InitAuthConfig() {
|
|
casdoorEndpoint := beego.AppConfig.String("casdoorEndpoint")
|
|
clientId := beego.AppConfig.String("clientId")
|
|
clientSecret := beego.AppConfig.String("clientSecret")
|
|
casdoorOrganization := beego.AppConfig.String("casdoorOrganization")
|
|
casdoorApplication := beego.AppConfig.String("casdoorApplication")
|
|
|
|
auth.InitConfig(casdoorEndpoint, clientId, clientSecret, JwtPublicKey, casdoorOrganization, casdoorApplication)
|
|
}
|
|
|
|
// UserLogin 用户登录
|
|
func (c *ApiController) UserLogin() {
|
|
var req LoginRequest
|
|
|
|
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &req); err != nil {
|
|
c.ResponseError("cannot unmarshal", err.Error())
|
|
return
|
|
}
|
|
|
|
id, userType, err := user.Login(req.Account, req.Password)
|
|
if err != nil {
|
|
c.ResponseError("cannot login", err.Error())
|
|
return
|
|
}
|
|
|
|
c.SetSession("userId", id)
|
|
c.SetSession("userType", userType)
|
|
|
|
resp := struct {
|
|
UserType int64 `json:"user_type"`
|
|
}{userType}
|
|
|
|
c.ResponseOk(resp)
|
|
}
|
|
|
|
/*
|
|
// @Title Signin
|
|
// @Description sign in as a member
|
|
// @Param code QueryString string true "The code to sign in"
|
|
// @Param state QueryString string true "The state"
|
|
// @Success 200 {object} controllers.api_controller.Response The Response object
|
|
// @router /signin [post]
|
|
// @Tag Account API*/
|
|
func (c *ApiController) SignIn() {
|
|
code := c.Input().Get("code")
|
|
state := c.Input().Get("state")
|
|
|
|
token, err := auth.GetOAuthToken(code, state)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
claims, err := auth.ParseJwtToken(token.AccessToken)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
claims.AccessToken = token.AccessToken
|
|
c.SetSessionClaims(claims)
|
|
|
|
c.ResponseOk(claims)
|
|
}
|
|
|
|
/*
|
|
// @Title Signout
|
|
// @Description sign out the current member
|
|
// @Success 200 {object} controllers.api_controller.Response The Response object
|
|
// @router /signout [post]
|
|
// @Tag Account API*/
|
|
func (c *ApiController) SignOut() {
|
|
c.SetSessionClaims(nil)
|
|
|
|
c.ResponseOk()
|
|
}
|
|
|
|
/*
|
|
// @Title GetAccount
|
|
// @Description Get current account
|
|
// @Success 200 {object} controllers.api_controller.Response The Response object
|
|
// @router /get-account [get]
|
|
// @Tag Account API*/
|
|
func (c *ApiController) GetAccount() {
|
|
if c.RequireSignedIn() {
|
|
return
|
|
}
|
|
|
|
claims := c.GetSessionClaims()
|
|
|
|
c.ResponseOk(claims)
|
|
}
|