feat: support stateless mfa setup

This commit is contained in:
dacongda
2024-11-28 15:55:16 +08:00
parent 2d1736f13a
commit bd843b2ff3
4 changed files with 102 additions and 39 deletions

View File

@ -16,7 +16,9 @@ package object
import (
"fmt"
"sync"
"github.com/beego/beego/session"
"github.com/casdoor/casdoor/util"
)
@ -49,6 +51,8 @@ const (
RequiredMfa = "RequiredMfa"
)
var MfaCache = sync.Map{}
func GetMfaUtil(mfaType string, config *MfaProps) MfaInterface {
switch mfaType {
case SmsType:
@ -183,3 +187,41 @@ func SetPreferredMultiFactorAuth(user *User, mfaType string) error {
}
return nil
}
func GetPropsFromContext(key string, curSession session.Store, mfaCacheKey string) string {
if mfaCacheKey != "" {
propMap, exist := MfaCache.Load(mfaCacheKey)
if !exist {
return ""
}
if propMap == nil {
return ""
}
return propMap.(map[string]string)[key]
}
val := curSession.Get(key)
if val != nil {
return val.(string)
}
return ""
}
func SetPropsFromContext(key string, value string, curSession session.Store, mfaCacheKey string) {
if mfaCacheKey != "" {
propMap, exist := MfaCache.Load(mfaCacheKey)
if !exist {
return
}
if propMap == nil {
return
}
propMap.(map[string]string)[key] = value
MfaCache.Store(mfaCacheKey, propMap)
}
err := curSession.Set(key, value)
if err != nil {
return
}
}