2022-02-13 23:39:27 +08:00
|
|
|
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
|
2021-03-14 15:50:36 +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.
|
|
|
|
|
2021-02-11 22:56:08 +08:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-04-28 19:02:01 +08:00
|
|
|
"fmt"
|
2023-01-17 22:57:05 +08:00
|
|
|
"net/http"
|
2021-06-17 00:49:02 +08:00
|
|
|
"strconv"
|
2022-03-13 00:30:18 +08:00
|
|
|
"strings"
|
2021-03-20 00:23:37 +08:00
|
|
|
|
2023-04-25 23:05:53 +08:00
|
|
|
"github.com/casdoor/casdoor/form"
|
2022-01-20 14:11:46 +08:00
|
|
|
"github.com/casdoor/casdoor/object"
|
|
|
|
"github.com/casdoor/casdoor/util"
|
2021-02-11 22:56:08 +08:00
|
|
|
)
|
|
|
|
|
2021-03-20 10:51:00 +08:00
|
|
|
const (
|
2022-03-01 19:09:59 +08:00
|
|
|
ResponseTypeLogin = "login"
|
|
|
|
ResponseTypeCode = "code"
|
|
|
|
ResponseTypeToken = "token"
|
|
|
|
ResponseTypeIdToken = "id_token"
|
2022-04-08 23:06:48 +08:00
|
|
|
ResponseTypeSaml = "saml"
|
2022-04-04 00:09:04 +08:00
|
|
|
ResponseTypeCas = "cas"
|
2021-03-20 10:51:00 +08:00
|
|
|
)
|
|
|
|
|
2021-02-11 22:56:08 +08:00
|
|
|
type Response struct {
|
|
|
|
Status string `json:"status"`
|
|
|
|
Msg string `json:"msg"`
|
2021-12-15 21:42:16 +08:00
|
|
|
Sub string `json:"sub"`
|
2021-12-31 19:55:34 +08:00
|
|
|
Name string `json:"name"`
|
2021-02-11 22:56:08 +08:00
|
|
|
Data interface{} `json:"data"`
|
2021-04-29 19:51:03 +08:00
|
|
|
Data2 interface{} `json:"data2"`
|
2021-02-11 22:56:08 +08:00
|
|
|
}
|
|
|
|
|
2022-06-18 16:00:31 +08:00
|
|
|
type Captcha struct {
|
2022-06-29 11:31:32 +08:00
|
|
|
Type string `json:"type"`
|
|
|
|
AppKey string `json:"appKey"`
|
|
|
|
Scene string `json:"scene"`
|
|
|
|
CaptchaId string `json:"captchaId"`
|
|
|
|
CaptchaImage []byte `json:"captchaImage"`
|
|
|
|
ClientId string `json:"clientId"`
|
|
|
|
ClientSecret string `json:"clientSecret"`
|
|
|
|
ClientId2 string `json:"clientId2"`
|
|
|
|
ClientSecret2 string `json:"clientSecret2"`
|
|
|
|
SubType string `json:"subType"`
|
2021-05-22 20:57:55 +08:00
|
|
|
}
|
|
|
|
|
2021-08-07 22:02:56 +08:00
|
|
|
// Signup
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag Login API
|
2021-04-27 22:47:44 +08:00
|
|
|
// @Title Signup
|
|
|
|
// @Description sign up a new user
|
|
|
|
// @Param username formData string true "The username to sign up"
|
2021-02-11 22:56:08 +08:00
|
|
|
// @Param password formData string true "The password"
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Success 200 {object} controllers.Response The Response object
|
2021-04-27 22:47:44 +08:00
|
|
|
// @router /signup [post]
|
|
|
|
func (c *ApiController) Signup() {
|
2021-07-18 07:15:22 +08:00
|
|
|
if c.GetSessionUsername() != "" {
|
2023-03-19 21:47:49 +08:00
|
|
|
c.ResponseError(c.T("account:Please sign out first"), c.GetSessionUsername())
|
2021-02-11 22:56:08 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-25 23:05:53 +08:00
|
|
|
var authForm form.AuthForm
|
|
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &authForm)
|
2021-02-11 22:56:08 +08:00
|
|
|
if err != nil {
|
2022-08-20 21:09:32 +08:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2021-02-11 22:56:08 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
application, err := object.GetApplication(fmt.Sprintf("admin/%s", authForm.Application))
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-17 00:49:02 +08:00
|
|
|
if !application.EnableSignUp {
|
2022-12-07 13:13:23 +08:00
|
|
|
c.ResponseError(c.T("account:The application does not allow to sign up new account"))
|
2021-05-18 20:11:03 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
organization, err := object.GetOrganization(util.GetId("admin", authForm.Organization))
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(c.T(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-25 23:05:53 +08:00
|
|
|
msg := object.CheckUserSignup(application, organization, &authForm, c.GetAcceptLanguage())
|
2021-11-09 20:28:27 +08:00
|
|
|
if msg != "" {
|
|
|
|
c.ResponseError(msg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-25 23:05:53 +08:00
|
|
|
if application.IsSignupItemVisible("Email") && application.GetSignupItemRule("Email") != "No verification" && authForm.Email != "" {
|
|
|
|
checkResult := object.CheckVerificationCode(authForm.Email, authForm.EmailCode, c.GetAcceptLanguage())
|
2023-03-15 23:44:38 +08:00
|
|
|
if checkResult.Code != object.VerificationSuccess {
|
|
|
|
c.ResponseError(checkResult.Msg)
|
2021-06-17 00:49:02 +08:00
|
|
|
return
|
|
|
|
}
|
2021-05-18 20:11:03 +08:00
|
|
|
}
|
|
|
|
|
2021-06-17 00:49:02 +08:00
|
|
|
var checkPhone string
|
2023-04-25 23:05:53 +08:00
|
|
|
if application.IsSignupItemVisible("Phone") && application.GetSignupItemRule("Phone") != "No verification" && authForm.Phone != "" {
|
|
|
|
checkPhone, _ = util.GetE164Number(authForm.Phone, authForm.CountryCode)
|
|
|
|
checkResult := object.CheckVerificationCode(checkPhone, authForm.PhoneCode, c.GetAcceptLanguage())
|
2023-03-15 23:44:38 +08:00
|
|
|
if checkResult.Code != object.VerificationSuccess {
|
|
|
|
c.ResponseError(checkResult.Msg)
|
2021-06-17 00:49:02 +08:00
|
|
|
return
|
|
|
|
}
|
2021-05-08 00:23:08 +08:00
|
|
|
}
|
|
|
|
|
2021-06-17 00:49:02 +08:00
|
|
|
id := util.GenerateId()
|
|
|
|
if application.GetSignupItemRule("ID") == "Incremental" {
|
2023-05-30 15:49:39 +08:00
|
|
|
lastUser, err := object.GetLastUser(authForm.Organization)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2022-02-14 22:58:13 +08:00
|
|
|
|
|
|
|
lastIdInt := -1
|
|
|
|
if lastUser != nil {
|
|
|
|
lastIdInt = util.ParseInt(lastUser.Id)
|
|
|
|
}
|
|
|
|
|
2021-06-17 00:49:02 +08:00
|
|
|
id = strconv.Itoa(lastIdInt + 1)
|
|
|
|
}
|
2021-02-11 22:56:08 +08:00
|
|
|
|
2023-04-25 23:05:53 +08:00
|
|
|
username := authForm.Username
|
2021-06-17 00:49:02 +08:00
|
|
|
if !application.IsSignupItemVisible("Username") {
|
|
|
|
username = id
|
2021-02-11 22:56:08 +08:00
|
|
|
}
|
|
|
|
|
2023-06-17 00:01:20 +08:00
|
|
|
password := authForm.Password
|
|
|
|
msg = object.CheckPasswordComplexityByOrg(organization, password)
|
|
|
|
if msg != "" {
|
|
|
|
c.ResponseError(msg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-19 02:34:25 +08:00
|
|
|
initScore, err := organization.GetInitScore()
|
2022-08-20 21:09:32 +08:00
|
|
|
if err != nil {
|
2022-12-07 13:13:23 +08:00
|
|
|
c.ResponseError(fmt.Errorf(c.T("account:Get init score failed, error: %w"), err).Error())
|
2022-08-20 21:09:32 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-17 00:49:02 +08:00
|
|
|
user := &object.User{
|
2023-04-25 23:05:53 +08:00
|
|
|
Owner: authForm.Organization,
|
2021-06-21 10:22:47 +08:00
|
|
|
Name: username,
|
|
|
|
CreatedTime: util.GetCurrentTime(),
|
|
|
|
Id: id,
|
|
|
|
Type: "normal-user",
|
2023-04-25 23:05:53 +08:00
|
|
|
Password: authForm.Password,
|
|
|
|
DisplayName: authForm.Name,
|
2021-06-21 10:22:47 +08:00
|
|
|
Avatar: organization.DefaultAvatar,
|
2023-04-25 23:05:53 +08:00
|
|
|
Email: authForm.Email,
|
|
|
|
Phone: authForm.Phone,
|
|
|
|
CountryCode: authForm.CountryCode,
|
2021-06-21 10:22:47 +08:00
|
|
|
Address: []string{},
|
2023-04-25 23:05:53 +08:00
|
|
|
Affiliation: authForm.Affiliation,
|
|
|
|
IdCard: authForm.IdCard,
|
|
|
|
Region: authForm.Region,
|
2022-08-20 21:09:32 +08:00
|
|
|
Score: initScore,
|
2021-06-21 10:22:47 +08:00
|
|
|
IsAdmin: false,
|
|
|
|
IsGlobalAdmin: false,
|
|
|
|
IsForbidden: false,
|
2021-11-06 15:52:03 +08:00
|
|
|
IsDeleted: false,
|
2021-06-21 10:22:47 +08:00
|
|
|
SignupApplication: application.Name,
|
|
|
|
Properties: map[string]string{},
|
2022-02-28 16:25:09 +08:00
|
|
|
Karma: 0,
|
2021-06-17 00:49:02 +08:00
|
|
|
}
|
2021-06-20 13:27:26 +08:00
|
|
|
|
2022-03-13 00:30:18 +08:00
|
|
|
if len(organization.Tags) > 0 {
|
|
|
|
tokens := strings.Split(organization.Tags[0], "|")
|
|
|
|
if len(tokens) > 0 {
|
|
|
|
user.Tag = tokens[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-27 14:02:52 +08:00
|
|
|
if application.GetSignupItemRule("Display name") == "First, last" {
|
2023-04-25 23:05:53 +08:00
|
|
|
if authForm.FirstName != "" || authForm.LastName != "" {
|
|
|
|
user.DisplayName = fmt.Sprintf("%s %s", authForm.FirstName, authForm.LastName)
|
|
|
|
user.FirstName = authForm.FirstName
|
|
|
|
user.LastName = authForm.LastName
|
2022-02-28 13:17:05 +08:00
|
|
|
}
|
2022-02-27 14:02:52 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
affected, err := object.AddUser(user)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-12 19:59:55 +08:00
|
|
|
if !affected {
|
2023-03-19 21:47:49 +08:00
|
|
|
c.ResponseError(c.T("account:Failed to add user"), util.StructToJson(user))
|
2021-12-12 19:59:55 +08:00
|
|
|
return
|
2021-06-20 13:27:26 +08:00
|
|
|
}
|
2021-06-17 00:49:02 +08:00
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
err = object.AddUserToOriginalDatabase(user)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2021-12-12 19:59:55 +08:00
|
|
|
|
2021-06-20 09:46:06 +08:00
|
|
|
if application.HasPromptPage() {
|
|
|
|
// The prompt page needs the user to be signed in
|
2021-07-18 07:15:22 +08:00
|
|
|
c.SetSessionUsername(user.GetId())
|
2021-06-20 09:46:06 +08:00
|
|
|
}
|
2021-06-17 00:49:02 +08:00
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
err = object.DisableVerificationCode(authForm.Email)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = object.DisableVerificationCode(checkPhone)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2021-06-17 00:49:02 +08:00
|
|
|
|
2023-05-20 10:56:21 +03:00
|
|
|
isSignupFromPricing := authForm.Plan != "" && authForm.Pricing != ""
|
|
|
|
if isSignupFromPricing {
|
2023-05-30 15:49:39 +08:00
|
|
|
_, err = object.Subscribe(organization.Name, user.Name, authForm.Plan, authForm.Pricing)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2023-05-20 10:56:21 +03:00
|
|
|
}
|
|
|
|
|
2022-03-01 18:25:48 +08:00
|
|
|
record := object.NewRecord(c.Ctx)
|
|
|
|
record.Organization = application.Organization
|
|
|
|
record.User = user.Name
|
2022-04-26 14:32:04 +08:00
|
|
|
util.SafeGoroutine(func() { object.AddRecord(record) })
|
2022-03-01 18:25:48 +08:00
|
|
|
|
2022-07-30 17:40:30 +08:00
|
|
|
userId := user.GetId()
|
2021-06-17 00:49:02 +08:00
|
|
|
util.LogInfo(c.Ctx, "API: [%s] is signed up as new user", userId)
|
|
|
|
|
2021-08-08 16:00:19 +08:00
|
|
|
c.ResponseOk(userId)
|
2021-02-11 22:56:08 +08:00
|
|
|
}
|
|
|
|
|
2021-08-07 22:02:56 +08:00
|
|
|
// Logout
|
2021-02-11 22:56:08 +08:00
|
|
|
// @Title Logout
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag Login API
|
2021-02-11 22:56:08 +08:00
|
|
|
// @Description logout the current user
|
2023-01-17 22:57:05 +08:00
|
|
|
// @Param id_token_hint query string false "id_token_hint"
|
|
|
|
// @Param post_logout_redirect_uri query string false "post_logout_redirect_uri"
|
|
|
|
// @Param state query string false "state"
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Success 200 {object} controllers.Response The Response object
|
2022-07-22 21:13:49 +08:00
|
|
|
// @router /logout [get,post]
|
2021-02-11 22:56:08 +08:00
|
|
|
func (c *ApiController) Logout() {
|
2023-01-17 22:57:05 +08:00
|
|
|
// https://openid.net/specs/openid-connect-rpinitiated-1_0-final.html
|
|
|
|
accessToken := c.Input().Get("id_token_hint")
|
|
|
|
redirectUri := c.Input().Get("post_logout_redirect_uri")
|
|
|
|
state := c.Input().Get("state")
|
2021-02-11 22:56:08 +08:00
|
|
|
|
2023-03-08 21:31:55 +08:00
|
|
|
user := c.GetSessionUsername()
|
|
|
|
|
2023-01-17 22:57:05 +08:00
|
|
|
if accessToken == "" && redirectUri == "" {
|
2023-02-12 09:33:24 +08:00
|
|
|
// TODO https://github.com/casdoor/casdoor/pull/1494#discussion_r1095675265
|
2023-03-08 21:31:55 +08:00
|
|
|
if user == "" {
|
|
|
|
c.ResponseOk()
|
|
|
|
return
|
|
|
|
}
|
2023-02-13 22:58:26 +08:00
|
|
|
|
2023-03-08 21:31:55 +08:00
|
|
|
c.ClearUserSession()
|
|
|
|
owner, username := util.GetOwnerAndNameFromId(user)
|
2023-05-30 15:49:39 +08:00
|
|
|
_, err := object.DeleteSessionId(util.GetSessionId(owner, username, object.CasdoorApplication), c.Ctx.Input.CruSession.SessionID())
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2023-03-08 21:31:55 +08:00
|
|
|
|
2023-01-17 22:57:05 +08:00
|
|
|
util.LogInfo(c.Ctx, "API: [%s] logged out", user)
|
|
|
|
|
|
|
|
application := c.GetSessionApplication()
|
|
|
|
if application == nil || application.Name == "app-built-in" || application.HomepageUrl == "" {
|
|
|
|
c.ResponseOk(user)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.ResponseOk(user, application.HomepageUrl)
|
2022-03-19 19:50:05 +08:00
|
|
|
return
|
2023-01-17 22:57:05 +08:00
|
|
|
} else {
|
|
|
|
if redirectUri == "" {
|
|
|
|
c.ResponseError(c.T("general:Missing parameter") + ": post_logout_redirect_uri")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if accessToken == "" {
|
|
|
|
c.ResponseError(c.T("general:Missing parameter") + ": id_token_hint")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
affected, application, token, err := object.ExpireTokenByAccessToken(accessToken)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-17 22:57:05 +08:00
|
|
|
if !affected {
|
|
|
|
c.ResponseError(c.T("token:Token not found, invalid accessToken"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if application == nil {
|
|
|
|
c.ResponseError(fmt.Sprintf(c.T("auth:The application: %s does not exist")), token.Application)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if application.IsRedirectUriValid(redirectUri) {
|
|
|
|
if user == "" {
|
|
|
|
user = util.GetId(token.Organization, token.User)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.ClearUserSession()
|
2023-02-12 09:33:24 +08:00
|
|
|
// TODO https://github.com/casdoor/casdoor/pull/1494#discussion_r1095675265
|
2023-03-03 14:26:31 +08:00
|
|
|
owner, username := util.GetOwnerAndNameFromId(user)
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
_, err := object.DeleteSessionId(util.GetSessionId(owner, username, object.CasdoorApplication), c.Ctx.Input.CruSession.SessionID())
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-17 22:57:05 +08:00
|
|
|
util.LogInfo(c.Ctx, "API: [%s] logged out", user)
|
|
|
|
|
|
|
|
c.Ctx.Redirect(http.StatusFound, fmt.Sprintf("%s?state=%s", strings.TrimRight(redirectUri, "/"), state))
|
|
|
|
} else {
|
|
|
|
c.ResponseError(fmt.Sprintf(c.T("token:Redirect URI: %s doesn't exist in the allowed Redirect URI list"), redirectUri))
|
|
|
|
return
|
|
|
|
}
|
2022-03-19 19:50:05 +08:00
|
|
|
}
|
2021-02-11 22:56:08 +08:00
|
|
|
}
|
|
|
|
|
2021-08-07 22:02:56 +08:00
|
|
|
// GetAccount
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Title GetAccount
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag Account API
|
2021-03-29 23:40:25 +08:00
|
|
|
// @Description get the details of the current account
|
|
|
|
// @Success 200 {object} controllers.Response The Response object
|
|
|
|
// @router /get-account [get]
|
2021-02-11 22:56:08 +08:00
|
|
|
func (c *ApiController) GetAccount() {
|
2023-05-30 15:49:39 +08:00
|
|
|
var err error
|
2022-09-18 15:43:49 +08:00
|
|
|
user, ok := c.RequireSignedInUser()
|
2021-05-17 23:25:28 +08:00
|
|
|
if !ok {
|
2021-02-11 22:56:08 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-30 00:57:27 +08:00
|
|
|
managedAccounts := c.Input().Get("managedAccounts")
|
|
|
|
if managedAccounts == "1" {
|
2023-05-30 15:49:39 +08:00
|
|
|
user, err = object.ExtendManagedAccountsWithUser(user)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2022-08-30 00:57:27 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
err = object.ExtendUserWithRolesAndPermissions(user)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2023-01-04 19:23:57 +07:00
|
|
|
|
2023-01-05 23:02:52 +07:00
|
|
|
user.Permissions = object.GetMaskedPermissions(user.Permissions)
|
|
|
|
user.Roles = object.GetMaskedRoles(user.Roles)
|
2023-06-21 18:56:37 +08:00
|
|
|
user.MultiFactorAuths = object.GetAllMfaProps(user, true)
|
2023-01-05 23:02:52 +07:00
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
organization, err := object.GetMaskedOrganization(object.GetOrganizationByUser(user))
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := object.GetMaskedUser(user)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-15 21:42:16 +08:00
|
|
|
resp := Response{
|
|
|
|
Status: "ok",
|
2021-12-16 11:10:25 +08:00
|
|
|
Sub: user.Id,
|
2021-12-31 19:55:34 +08:00
|
|
|
Name: user.Name,
|
2023-05-30 15:49:39 +08:00
|
|
|
Data: u,
|
2021-12-15 21:42:16 +08:00
|
|
|
Data2: organization,
|
|
|
|
}
|
|
|
|
c.Data["json"] = resp
|
|
|
|
c.ServeJSON()
|
2021-02-11 22:56:08 +08:00
|
|
|
}
|
2021-03-14 15:50:36 +08:00
|
|
|
|
2022-08-09 16:50:49 +08:00
|
|
|
// GetUserinfo
|
2022-01-26 11:56:01 +08:00
|
|
|
// UserInfo
|
|
|
|
// @Title UserInfo
|
|
|
|
// @Tag Account API
|
|
|
|
// @Description return user information according to OIDC standards
|
2022-02-18 12:36:11 +08:00
|
|
|
// @Success 200 {object} object.Userinfo The Response object
|
2022-01-26 11:56:01 +08:00
|
|
|
// @router /userinfo [get]
|
|
|
|
func (c *ApiController) GetUserinfo() {
|
2022-09-18 15:43:49 +08:00
|
|
|
user, ok := c.RequireSignedInUser()
|
2022-01-26 11:56:01 +08:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2022-09-18 15:43:49 +08:00
|
|
|
|
2022-01-26 11:56:01 +08:00
|
|
|
scope, aud := c.GetSessionOidc()
|
2022-02-18 12:36:11 +08:00
|
|
|
host := c.Ctx.Request.Host
|
2022-09-18 15:43:49 +08:00
|
|
|
userInfo := object.GetUserInfo(user, scope, aud, host)
|
|
|
|
|
|
|
|
c.Data["json"] = userInfo
|
2022-01-26 11:56:01 +08:00
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
2023-03-24 01:02:04 +08:00
|
|
|
// GetUserinfo2
|
|
|
|
// LaravelResponse
|
|
|
|
// @Title UserInfo2
|
|
|
|
// @Tag Account API
|
|
|
|
// @Description return Laravel compatible user information according to OAuth 2.0
|
|
|
|
// @Success 200 {object} LaravelResponse The Response object
|
|
|
|
// @router /user [get]
|
|
|
|
func (c *ApiController) GetUserinfo2() {
|
|
|
|
user, ok := c.RequireSignedInUser()
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// this API is used by "Api URL" of Flarum's FoF Passport plugin
|
|
|
|
// https://github.com/FriendsOfFlarum/passport
|
|
|
|
type LaravelResponse struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
EmailVerifiedAt string `json:"email_verified_at"`
|
|
|
|
CreatedAt string `json:"created_at"`
|
|
|
|
UpdatedAt string `json:"updated_at"`
|
|
|
|
}
|
|
|
|
|
|
|
|
response := LaravelResponse{
|
|
|
|
Id: user.Id,
|
|
|
|
Name: user.Name,
|
|
|
|
Email: user.Email,
|
|
|
|
EmailVerifiedAt: user.CreatedTime,
|
|
|
|
CreatedAt: user.CreatedTime,
|
|
|
|
UpdatedAt: user.UpdatedTime,
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Data["json"] = response
|
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
2022-06-18 16:00:31 +08:00
|
|
|
// GetCaptcha ...
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag Login API
|
2022-06-18 16:00:31 +08:00
|
|
|
// @Title GetCaptcha
|
|
|
|
// @router /api/get-captcha [get]
|
|
|
|
func (c *ApiController) GetCaptcha() {
|
|
|
|
applicationId := c.Input().Get("applicationId")
|
|
|
|
isCurrentProvider := c.Input().Get("isCurrentProvider")
|
2021-05-22 20:57:55 +08:00
|
|
|
|
2022-10-23 15:16:24 +08:00
|
|
|
captchaProvider, err := object.GetCaptchaProviderByApplication(applicationId, isCurrentProvider, c.GetAcceptLanguage())
|
2022-06-18 16:00:31 +08:00
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
2021-05-22 20:57:55 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-21 12:22:46 +08:00
|
|
|
if captchaProvider != nil {
|
|
|
|
if captchaProvider.Type == "Default" {
|
2023-05-30 15:49:39 +08:00
|
|
|
id, img, err := object.GetCaptcha()
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-21 12:22:46 +08:00
|
|
|
c.ResponseOk(Captcha{Type: captchaProvider.Type, CaptchaId: id, CaptchaImage: img})
|
|
|
|
return
|
|
|
|
} else if captchaProvider.Type != "" {
|
2022-06-29 11:31:32 +08:00
|
|
|
c.ResponseOk(Captcha{
|
|
|
|
Type: captchaProvider.Type,
|
|
|
|
SubType: captchaProvider.SubType,
|
|
|
|
ClientId: captchaProvider.ClientId,
|
|
|
|
ClientSecret: captchaProvider.ClientSecret,
|
|
|
|
ClientId2: captchaProvider.ClientId2,
|
|
|
|
ClientSecret2: captchaProvider.ClientSecret2,
|
|
|
|
})
|
2022-06-21 12:22:46 +08:00
|
|
|
return
|
|
|
|
}
|
2022-06-18 16:00:31 +08:00
|
|
|
}
|
2022-06-21 12:22:46 +08:00
|
|
|
|
2022-06-18 16:00:31 +08:00
|
|
|
c.ResponseOk(Captcha{Type: "none"})
|
2021-05-22 20:57:55 +08:00
|
|
|
}
|