diff --git a/app/gitspace/scm/types.go b/app/gitspace/scm/types.go index d64801e30..4c5ce820e 100644 --- a/app/gitspace/scm/types.go +++ b/app/gitspace/scm/types.go @@ -43,14 +43,14 @@ type ( // by an automated login process. Credentials struct { Email string - Name *types.MaskSecret - Password *types.MaskSecret + Name types.MaskSecret + Password types.MaskSecret } ResolvedCredentials struct { Branch string // CloneURL contains credentials for private repositories in url prefix - CloneURL *types.MaskSecret + CloneURL types.MaskSecret Credentials *Credentials RepoName string } diff --git a/types/mask_secret.go b/types/mask_secret.go index 50e71ec96..a5805bc13 100644 --- a/types/mask_secret.go +++ b/types/mask_secret.go @@ -29,13 +29,13 @@ type MaskSecret struct { hashedValue string } -func NewMaskSecret(val string) *MaskSecret { +func NewMaskSecret(val string) MaskSecret { hash := sha256.New() hash.Write([]byte(val)) hashedValueStr := fmt.Sprintf("%x", hash.Sum(nil)) - return &MaskSecret{ + return MaskSecret{ value: val, hashedValue: hashedValueStr[:maxTruncatedLen], } @@ -43,25 +43,28 @@ func NewMaskSecret(val string) *MaskSecret { // Value returns the unmasked value of the MaskSecret. // Use cautiously to avoid exposing sensitive data. -func (s *MaskSecret) Value() string { - if s == nil { - return "" - } +func (s MaskSecret) Value() string { return s.value } -func (s *MaskSecret) String() string { - if s == nil { - return "" - } +func (s MaskSecret) String() string { + if s.hashedValue == "" && s.value != "" { + // this case can arise when MarkSecret is created by UnmarshalJSON func where we do not + // use NewMaskSecret constructor. + hash := sha256.New() + hash.Write([]byte(s.value)) + hashedValueStr := fmt.Sprintf("%x", hash.Sum(nil)) + s.hashedValue = hashedValueStr[:maxTruncatedLen] + } return s.hashedValue } -func (s *MaskSecret) MarshalJSON() ([]byte, error) { +func (s MaskSecret) MarshalJSON() ([]byte, error) { return json.Marshal(s.value) } +// UnmarshalJSON needs pointer receiver as it modify the receiver. func (s *MaskSecret) UnmarshalJSON(data []byte) error { var input string if err := json.Unmarshal(data, &input); err != nil {