Skip to content

Commit 0af3bbd

Browse files
author
Dean Karn
committed
Initial Commit
0 parents  commit 0af3bbd

36 files changed

+2627
-0
lines changed

.travis.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
language: go
2+
go:
3+
- 1.14.1
4+
- tip
5+
matrix:
6+
allow_failures:
7+
- go: tip
8+
9+
notifications:
10+
email:
11+
recipients: dean.karn@gmail.com
12+
on_success: change
13+
on_failure: always
14+
15+
before_install:
16+
- go install github.com/mattn/goveralls
17+
18+
# Only clone the most recent commit.
19+
git:
20+
depth: 1
21+
22+
script:
23+
- go test -v ./...

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2020 Go Playground
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
## Package sensitive
2+
3+
<img align="right" src="https://raw.githubusercontent.com/go-playground/sensitive/master/logo.jpg">![Project status](https://img.shields.io/badge/version-0.0.1-green.svg)
4+
[![Build Status](https://travis-ci.org/go-playground/sensitive.svg?branch=master)](https://travis-ci.org/go-playground/sensitive)
5+
[![GoDoc](https://godoc.org/github.com/go-playground/sensitive?status.svg)](https://godoc.org/github.com/go-playground/sensitive)
6+
![License](https://img.shields.io/dub/l/vibe-d.svg)
7+
8+
Package sensitive provides base types who's values should never be seen by the human eye, but still used for configuration.
9+
10+
What? Explain
11+
12+
Sometimes you have a variable, such as a password, passed into your program via arguments or ENV variables.
13+
Some of these variables are very sensitive! and should not in any circumstance be loggged or sent via JSON, despite JSON's "-", which people may forget.
14+
These variables, which are just typed primitive types, have their overridden `fmt.Formatter`, `encoding.MarshalText` & `json.Marshal` implementations.
15+
16+
As an added bonus using them as their base type eg. String => string, you have to explicitly cast the eg. string(s) This makes you think about what you're doing and why you casting it providing additional safelty.
17+
18+
Variables:
19+
- `String` - The most useful
20+
- `Bool`
21+
- `Float32`
22+
- `Float64`
23+
- `Int`
24+
- `Int8`
25+
- `Int16`
26+
- `Int32`
27+
- `Int64`
28+
- `Uint`
29+
- `Uint8`
30+
- `Uint16`
31+
- `Uint32`
32+
- `Uint64`
33+
34+
Example
35+
-------
36+
```go
37+
// go run _examples/basic/main.go mypassword
38+
package main
39+
40+
import (
41+
"encoding/json"
42+
"fmt"
43+
"os"
44+
45+
"github.com/go-playground/sensitive"
46+
)
47+
48+
func main() {
49+
password := sensitive.String(os.Args[1])
50+
51+
fmt.Printf("%s\n", password)
52+
fmt.Printf("%v\n", password)
53+
54+
b, _ := json.Marshal(password)
55+
fmt.Println(string(b))
56+
57+
var empty *sensitive.String
58+
b, _ = json.Marshal(empty)
59+
fmt.Println(string(b))
60+
61+
// output:
62+
//
63+
//
64+
// ""
65+
// null
66+
}
67+
```
68+
69+
Custom Formatting
70+
-----------------
71+
```go
72+
package main
73+
74+
import (
75+
"encoding/json"
76+
"fmt"
77+
"io"
78+
"os"
79+
80+
"github.com/go-playground/sensitive"
81+
)
82+
83+
func init() {
84+
// override default Formatter
85+
sensitive.FormatStringFn = func(s sensitive.String, f fmt.State, c rune) {
86+
switch c {
87+
case 's':
88+
_, _ = io.WriteString(f, "redacted")
89+
case 'v':
90+
_, _ = io.WriteString(f, string(s)[:4]+"*******")
91+
}
92+
}
93+
}
94+
95+
func main() {
96+
password := sensitive.String(os.Args[1])
97+
98+
fmt.Printf("%s\n", password)
99+
fmt.Printf("%v\n", password)
100+
101+
b, _ := json.Marshal(password)
102+
fmt.Println(string(b))
103+
104+
var empty *sensitive.String
105+
b, _ = json.Marshal(empty)
106+
fmt.Println(string(b))
107+
108+
// output:
109+
// redacted
110+
// mypa*******
111+
// "redacted"
112+
// null
113+
}
114+
```
115+
116+
License
117+
------
118+
Distributed under MIT License, please see license file in code for more details.

_examples/basic/main.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"os"
7+
8+
"github.com/go-playground/sensitive"
9+
)
10+
11+
func main() {
12+
password := sensitive.String(os.Args[1])
13+
14+
fmt.Printf("%s\n", password)
15+
fmt.Printf("%v\n", password)
16+
17+
b, _ := json.Marshal(password)
18+
fmt.Println(string(b))
19+
20+
var empty *sensitive.String
21+
b, _ = json.Marshal(empty)
22+
fmt.Println(string(b))
23+
24+
// output:
25+
//
26+
//
27+
// ""
28+
// null
29+
}

_examples/custom/main.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io"
7+
"os"
8+
9+
"github.com/go-playground/sensitive"
10+
)
11+
12+
func init() {
13+
// override default Formatter
14+
sensitive.FormatStringFn = func(s sensitive.String, f fmt.State, c rune) {
15+
switch c {
16+
case 's':
17+
_, _ = io.WriteString(f, "redacted")
18+
case 'v':
19+
_, _ = io.WriteString(f, string(s)[:4]+"*******")
20+
}
21+
}
22+
}
23+
24+
func main() {
25+
password := sensitive.String(os.Args[1])
26+
27+
fmt.Printf("%s\n", password)
28+
fmt.Printf("%v\n", password)
29+
30+
b, _ := json.Marshal(password)
31+
fmt.Println(string(b))
32+
33+
var empty *sensitive.String
34+
b, _ = json.Marshal(empty)
35+
fmt.Println(string(b))
36+
37+
// output:
38+
// redacted
39+
// mypa*******
40+
// "redacted"
41+
// null
42+
}

bool.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package sensitive
2+
3+
import (
4+
"encoding"
5+
"encoding/json"
6+
"fmt"
7+
)
8+
9+
var (
10+
_ fmt.Formatter = (*Bool)(nil)
11+
_ json.Marshaler = (*Bool)(nil)
12+
_ encoding.TextMarshaler = (*Bool)(nil)
13+
FormatBoolFn = func(s Bool, f fmt.State, c rune) {}
14+
)
15+
16+
type Bool bool
17+
18+
func (s Bool) Format(f fmt.State, c rune) {
19+
FormatBoolFn(s, f, c)
20+
}
21+
22+
func (s Bool) MarshalJSON() ([]byte, error) {
23+
return json.Marshal(nil)
24+
}
25+
26+
func (s Bool) MarshalText() (text []byte, err error) {
27+
var ss State
28+
s.Format(&ss, 's')
29+
return ss.b, nil
30+
}

0 commit comments

Comments
 (0)