1 Star 0 Fork 0

carlmax_my/console-core-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
authing_tenant.go 6.93 KB
一键复制 编辑 原始数据 按行查看 历史
carlmax_my 提交于 2024-12-02 21:32 . init project
package authing_v3
import (
"errors"
"fmt"
"log"
"net/http"
"gitee.com/carlmax_my/console-core-go/pkg/authing/authing_model"
"gitee.com/carlmax_my/authing-golang-sdk/v3/dto"
jsoniter "github.com/json-iterator/go"
)
// 获取用户池下租户列表
func (a *AuthingCli) GetTenantList() (list *[]authing_model.Tenant) {
return nil
}
func (a *AuthingCli) GetAppTenantList() (list *[]authing_model.TenantDetails, err error) {
return a.AuthCli.GetAppTenantList()
}
func (a *AuthingCli) AddTenantMembers(tenantId, userId *string) error {
return nil
}
func (a *AuthingCli) CreateTenant(tenantInfo *authing_model.CreateTenantRequest) (*authing_model.TenantDetails, error) {
return nil, nil
}
func (a *AuthingCli) UpdateTenant(id *string, tenantInfo *authing_model.CreateTenantRequest) (bool, error) {
return false, nil
}
func (a *AuthingCli) UpdateTenant_ExtraInfo(tenantId string, request *authing_model.UpdateTenantExtraReq) (bool, error) {
return false, nil
}
func (a *AuthingCli) RemoveTenant(tenantId *string) (*string, error) {
return nil, nil
}
type AuthingTenantApp struct {
TenantAppId string `json:"tenantAppId"`
AppId string `json:"appId"`
Name string `json:"name"`
ApplicationType string `json:"applicationType"`
}
// tenant id should set by cli init
func (a *AuthingCli) ListTenantApplications() ([]AuthingTenantApp, error) {
reqDto := &dto.ListTenantApplicationsDto{
Page: "1",
Limit: "100",
}
resp := a.Cli.ListTenantApplications(reqDto)
if resp.StatusCode != 200 {
log.Printf("ListTenantApplications error:%+v\n", resp)
log.Printf("ListTenantApplications UserPoolId:%s\n", a.UserPoolId)
return nil, errors.New(resp.Message)
}
ret := make([]AuthingTenantApp, 0)
list := resp.Data.List
if len(list) == 0 {
return ret, nil
}
for _, app := range list {
newApp := AuthingTenantApp{
TenantAppId: app.TenantAppId,
AppId: app.AppId,
Name: app.Name,
ApplicationType: app.ApplicationType,
}
ret = append(ret, newApp)
}
return ret, nil
}
// tenant id should set by cli init
func (a *AuthingCli) ListTenantApplication() (*AuthingTenantApp, error) {
list, err := a.ListTenantApplications()
if err != nil {
return nil, err
}
if len(list) == 0 {
return nil, nil
}
app := list[0]
return &app, nil
}
type UpdateLoginConfigDto struct {
AutoJoin bool `json:"autoJoin"`
RejectHint string `json:"rejectHint"`
}
func (a *AuthingCli) AutoJoinUserToTenant(idpId string, enable bool, tenantId string) error {
app, err := a.ListTenantApplication()
if err != nil {
return err
}
if app == nil {
return errors.New("tenant without app")
}
c := a.Cli
// https://console.authing.cn/api/v3/update-login-page-config?tenantId=63b9324e94a314c8e225140d
url := fmt.Sprintf("/api/v3/update-login-page-config?tenantId=%s", tenantId)
req := &struct {
AppId string `json:"appId"`
Update *UpdateLoginConfigDto `json:"update"`
}{
AppId: app.TenantAppId,
Update: &UpdateLoginConfigDto{
AutoJoin: enable,
RejectHint: "租户拒绝了登录,请联系租户管理员",
},
}
b, err := c.SendHttpRequest(url, http.MethodPost, req)
if err != nil {
log.Println("AutoJoinUserToTenant error")
return err
}
var resp dto.IsSuccessRespDto
jsoniter.Unmarshal(b, &resp)
if resp.StatusCode != 200 {
log.Printf("AutoJoinUserToTenant error[%s]: %+v\n", req.AppId, resp)
return errors.New(resp.Message)
}
return nil
}
// passwordMethods = "phone-password", "email-password", "username-password"
// codeMethods = phone-code, email-code
func (a *AuthingCli) UpdateTenant_loginPage(tenantId string, loginCfg *authing_model.MyAuthingLoginConfig) error {
app, err := a.ListTenantApplication()
if err != nil {
return err
}
if app == nil {
return errors.New("tenant without app")
}
c := a.Cli
// https://console.authing.cn/api/v3/update-login-page-config?tenantId=63b9324e94a314c8e225140d
url := fmt.Sprintf("/api/v3/update-login-page-config?tenantId=%s", tenantId)
loginPageReq := authing_model.LoginPageUpdateReq{
AutoRegisterThenLogin: false, // this will trigger auto add user when user not exist
// DefaultLoginTab: authing_model.LoginTabPassword, //
// LoginTabs: []string{authing_model.LoginTabPassword}, //
PasswordTabConfig: authing_model.LoginTabConfigReq{ //
EnabledLoginMethods: []string{}, // []string{"phone-password", "email-password", "username-password"},
ValidLoginMethods: []string{}, // []string{"phone-password", "email-password", "username-password"},
ValidRegisterMethods: []string{},
},
VerifyCodeTabConfig: authing_model.LoginTabConfigReq{
EnabledLoginMethods: []string{}, // phone-code, email-code
ValidLoginMethods: []string{}, // phone-code, email-code
ValidRegisterMethods: []string{},
},
}
if loginCfg.EnablePasswordLogin && loginCfg.EnableCodeVerifyLogin {
loginPageReq.LoginTabs = []string{authing_model.LoginTabPassword, authing_model.LoginTabCode}
if loginCfg.DefaultCodeVerifyLoginTab {
loginPageReq.DefaultLoginTab = authing_model.LoginTabCode
} else {
loginPageReq.DefaultLoginTab = authing_model.LoginTabPassword
}
loginPageReq.PasswordTabConfig.EnabledLoginMethods = authing_model.PasswordMethods
loginPageReq.PasswordTabConfig.ValidLoginMethods = authing_model.PasswordMethods
loginPageReq.VerifyCodeTabConfig.EnabledLoginMethods = authing_model.VerifyCodeMethods
loginPageReq.VerifyCodeTabConfig.ValidLoginMethods = authing_model.VerifyCodeMethods
} else if loginCfg.EnablePasswordLogin {
loginPageReq.LoginTabs = []string{authing_model.LoginTabPassword}
loginPageReq.DefaultLoginTab = authing_model.LoginTabPassword
loginPageReq.PasswordTabConfig.EnabledLoginMethods = authing_model.PasswordMethods
loginPageReq.PasswordTabConfig.ValidLoginMethods = authing_model.PasswordMethods
} else if loginCfg.EnableCodeVerifyLogin {
loginPageReq.LoginTabs = []string{authing_model.LoginTabCode}
loginPageReq.DefaultLoginTab = authing_model.LoginTabCode
loginPageReq.VerifyCodeTabConfig.EnabledLoginMethods = authing_model.VerifyCodeMethods
loginPageReq.VerifyCodeTabConfig.ValidLoginMethods = authing_model.VerifyCodeMethods
} else {
log.Println("UpdateTenant_loginPage error: invalid tabs config")
return errors.New("invalid tabs config")
}
req := &struct {
AppId string `json:"appId"`
Update *authing_model.LoginPageUpdateReq `json:"update"`
}{
AppId: app.TenantAppId,
Update: &loginPageReq,
}
log.Printf("UpdateTenant_loginPage.loginCfg=%+v\n", loginCfg)
log.Printf("UpdateTenant_loginPage=%+v\n", req)
log.Printf("UpdateTenant_loginPage.loginPageReq=%+v\n", loginPageReq)
b, err := c.SendHttpRequest(url, http.MethodPost, req)
if err != nil {
log.Println("UpdateTenant_loginPage error")
return err
}
var resp dto.IsSuccessRespDto
jsoniter.Unmarshal(b, &resp)
if resp.StatusCode != 200 {
log.Printf("UpdateTenant_loginPage error[%s]: %+v\n", req.AppId, resp)
return errors.New(resp.Message)
}
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/carlmax_my/console-core-go.git
git@gitee.com:carlmax_my/console-core-go.git
carlmax_my
console-core-go
console-core-go
v0.0.27

搜索帮助