-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
secrets.go
56 lines (39 loc) · 1.42 KB
/
secrets.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package models
import (
"encoding"
"fmt"
"net/url"
)
const redacted = "xxxxx"
var (
_ fmt.Stringer = (*Secret)(nil)
_ encoding.TextMarshaler = (*Secret)(nil)
)
// Secret is a string that formats and encodes redacted, as "xxxxx".
//
// Use Value to get the actual secret.
type Secret string
func NewSecret(s string) *Secret { return (*Secret)(&s) }
func (s Secret) String() string { return redacted }
func (s Secret) GoString() string { return redacted }
func (s Secret) MarshalText() ([]byte, error) { return []byte(redacted), nil }
var (
_ fmt.Stringer = (*SecretURL)(nil)
_ encoding.TextMarshaler = (*SecretURL)(nil)
_ encoding.TextUnmarshaler = (*SecretURL)(nil)
)
// SecretURL is a URL that formats and encodes redacted, as "xxxxx".
type SecretURL URL
func NewSecretURL(u *URL) *SecretURL { return (*SecretURL)(u) }
func MustSecretURL(u string) *SecretURL { return NewSecretURL(MustParseURL(u)) }
func (s *SecretURL) String() string { return redacted }
func (s *SecretURL) GoString() string { return redacted }
func (s *SecretURL) URL() *url.URL { return (*URL)(s).URL() }
func (s *SecretURL) MarshalText() ([]byte, error) { return []byte(redacted), nil }
func (s *SecretURL) UnmarshalText(text []byte) error {
if err := (*URL)(s).UnmarshalText(text); err != nil {
//opt: if errors.Is(url.Error), just redact the err.URL field?
return fmt.Errorf("failed to parse url: %s", redacted)
}
return nil
}