mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-04 21:30:24 +08:00
feat: support stateless mfa setup
This commit is contained in:
@ -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
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user