|
5 | 5 | package secret |
6 | 6 |
|
7 | 7 | // Text provides a way to safely store your secret value and a corresponding redact hint. This |
8 | | -// redact hint what is used in operations like printing and serializing. The default |
9 | | -// value of Text is usable. |
| 8 | +// redact hint is what is used in operations like printing and serializing. |
10 | 9 | type Text struct { |
11 | 10 | // v is the actual secret values. |
12 | 11 | v *string |
13 | 12 | // r is the redact hint to be used in place of secret. |
14 | 13 | r *string |
15 | 14 | } |
16 | 15 |
|
17 | | -// New returns [Text] for the secret with `*****` as the redact hint. |
18 | | -// Multiple option functions can be passed to alter default behavior. |
| 16 | +// New returns [Text] for the secret with [FiveStar] as the default redact hint. Provide options |
| 17 | +// like [RedactHint] to modify default behavior. |
19 | 18 | func New(secret string, options ...func(*Text)) Text { |
20 | | - sec := Text{ |
| 19 | + tx := Text{ |
21 | 20 | v: new(string), |
22 | 21 | r: new(string), |
23 | 22 | } |
24 | 23 |
|
25 | | - *sec.v = secret |
26 | | - *sec.r = defaultRedact |
| 24 | + *tx.v = secret |
| 25 | + *tx.r = FiveStar |
27 | 26 |
|
28 | | - // Apply options to override defaults |
29 | | - for _, opt := range options { |
30 | | - opt(&sec) |
| 27 | + for _, o := range options { |
| 28 | + o(&tx) |
31 | 29 | } |
32 | 30 |
|
33 | | - return sec |
| 31 | + return tx |
34 | 32 | } |
35 | 33 |
|
36 | | -// defaultRedact is used if no other redact hint is given. |
37 | | -const defaultRedact string = "*****" |
| 34 | +// Some common redact hints. |
| 35 | +const ( |
| 36 | + FiveX string = "XXXXX" |
| 37 | + FiveStar string = "*****" |
| 38 | + Redacted string = "[REDACTED]" |
| 39 | +) |
38 | 40 |
|
39 | | -// String implements the fmt.Stringer interface and returns only the redact hint. This prevents the |
| 41 | +// RedactHint is a functional option to set r as the redact hint for the [Text]. You can use one of |
| 42 | +// the common redact hints provided with this package like [FiveX] or provide your own string. |
| 43 | +func RedactHint(r string) func(*Text) { |
| 44 | + return func(t *Text) { |
| 45 | + *t.r = r |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// String implements the [fmt.Stringer] interface and returns only the redact hint. This prevents the |
40 | 50 | // secret value from being printed to std*, logs etc. |
41 | | -func (s Text) String() string { |
42 | | - if s.r == nil { |
43 | | - return defaultRedact |
| 51 | +func (tx Text) String() string { |
| 52 | + if tx.r == nil { |
| 53 | + return FiveStar |
44 | 54 | } |
45 | | - return *s.r |
| 55 | + return *tx.r |
46 | 56 | } |
47 | 57 |
|
48 | 58 | // Value gives you access to the actual secret value stored inside Text. |
49 | | -func (s Text) Value() string { |
50 | | - if s.v == nil { |
| 59 | +func (tx Text) Value() string { |
| 60 | + if tx.v == nil { |
51 | 61 | return "" |
52 | 62 | } |
53 | | - return *s.v |
| 63 | + return *tx.v |
54 | 64 | } |
55 | 65 |
|
56 | 66 | // MarshalText implements [encoding.TextMarshaler]. It marshals redact string into bytes rather than |
57 | 67 | // the actual secret value. |
58 | | -func (s Text) MarshalText() ([]byte, error) { |
59 | | - return []byte(*s.r), nil |
| 68 | +func (tx Text) MarshalText() ([]byte, error) { |
| 69 | + return []byte(tx.String()), nil |
60 | 70 | } |
61 | 71 |
|
62 | 72 | // UnmarshalText implements [encoding.TextUnmarshaler]. It unmarshals b into receiver's new secret |
63 | 73 | // value. If redact string is present then it is reused. |
64 | | -func (s *Text) UnmarshalText(b []byte) error { |
65 | | - v := string(b) |
| 74 | +func (tx *Text) UnmarshalText(b []byte) error { |
| 75 | + s := string(b) |
66 | 76 |
|
67 | 77 | // If the original redact is not nil then use it otherwise fallback to default. |
68 | | - if s.r != nil { |
69 | | - *s = New(v, CustomRedact(*s.r)) |
| 78 | + if tx.r != nil { |
| 79 | + *tx = New(s, RedactHint(*tx.r)) |
70 | 80 | } else { |
71 | | - *s = New(v) |
| 81 | + *tx = New(s) |
72 | 82 | } |
73 | 83 | return nil |
74 | 84 | } |
75 | 85 |
|
76 | 86 | // Equals checks whether s2 has same secret string or not. |
77 | | -func (s *Text) Equals(s2 Text) bool { |
78 | | - return *s.v == *s2.v |
79 | | -} |
80 | | - |
81 | | -// Redacted option sets "[REDACTED]" as the redact hint. |
82 | | -func Redacted(s *Text) { |
83 | | - *s.r = "[REDACTED]" |
84 | | -} |
85 | | - |
86 | | -// FiveXs option sets "XXXXX" as the redact hint. |
87 | | -func FiveXs(s *Text) { |
88 | | - *s.r = "XXXXX" |
89 | | -} |
90 | | - |
91 | | -// CustomRedact option sets the value of r as the redact hint. |
92 | | -func CustomRedact(r string) func(*Text) { |
93 | | - return func(s *Text) { |
94 | | - *s.r = r |
95 | | - } |
| 87 | +func (tx *Text) Equals(s2 Text) bool { |
| 88 | + return *tx.v == *s2.v |
96 | 89 | } |
0 commit comments