mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-03 20:50:19 +08:00
feat: improve record content masking (#2923)
* feat: hide password in record * feat: improve code format * feat: improve code format
This commit is contained in:
@ -17,6 +17,7 @@ package object
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/beego/beego/context"
|
"github.com/beego/beego/context"
|
||||||
@ -25,10 +26,14 @@ import (
|
|||||||
"github.com/casvisor/casvisor-go-sdk/casvisorsdk"
|
"github.com/casvisor/casvisor-go-sdk/casvisorsdk"
|
||||||
)
|
)
|
||||||
|
|
||||||
var logPostOnly bool
|
var (
|
||||||
|
logPostOnly bool
|
||||||
|
passwordRegex *regexp.Regexp
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
logPostOnly = conf.GetConfigBool("logPostOnly")
|
logPostOnly = conf.GetConfigBool("logPostOnly")
|
||||||
|
passwordRegex = regexp.MustCompile("\"password\":\".+\"")
|
||||||
}
|
}
|
||||||
|
|
||||||
type Record struct {
|
type Record struct {
|
||||||
@ -40,6 +45,10 @@ type Response struct {
|
|||||||
Msg string `json:"msg"`
|
Msg string `json:"msg"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func maskPassword(recordString string) string {
|
||||||
|
return passwordRegex.ReplaceAllString(recordString, "\"password\":\"***\"")
|
||||||
|
}
|
||||||
|
|
||||||
func NewRecord(ctx *context.Context) (*casvisorsdk.Record, error) {
|
func NewRecord(ctx *context.Context) (*casvisorsdk.Record, error) {
|
||||||
ip := strings.Replace(util.GetIPFromRequest(ctx.Request), ": ", "", -1)
|
ip := strings.Replace(util.GetIPFromRequest(ctx.Request), ": ", "", -1)
|
||||||
action := strings.Replace(ctx.Request.URL.Path, "/api/", "", -1)
|
action := strings.Replace(ctx.Request.URL.Path, "/api/", "", -1)
|
||||||
@ -51,6 +60,7 @@ func NewRecord(ctx *context.Context) (*casvisorsdk.Record, error) {
|
|||||||
object := ""
|
object := ""
|
||||||
if ctx.Input.RequestBody != nil && len(ctx.Input.RequestBody) != 0 {
|
if ctx.Input.RequestBody != nil && len(ctx.Input.RequestBody) != 0 {
|
||||||
object = string(ctx.Input.RequestBody)
|
object = string(ctx.Input.RequestBody)
|
||||||
|
object = maskPassword(object)
|
||||||
}
|
}
|
||||||
|
|
||||||
respBytes, err := json.Marshal(ctx.Input.Data()["json"])
|
respBytes, err := json.Marshal(ctx.Input.Data()["json"])
|
||||||
@ -99,6 +109,8 @@ func AddRecord(record *casvisorsdk.Record) bool {
|
|||||||
|
|
||||||
record.Owner = record.Organization
|
record.Owner = record.Organization
|
||||||
|
|
||||||
|
record.Object = maskPassword(record.Object)
|
||||||
|
|
||||||
errWebhook := SendWebhooks(record)
|
errWebhook := SendWebhooks(record)
|
||||||
if errWebhook == nil {
|
if errWebhook == nil {
|
||||||
record.IsTriggered = true
|
record.IsTriggered = true
|
||||||
|
Reference in New Issue
Block a user