forked from murphysecurity/murphysec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
110 lines (100 loc) · 2.86 KB
/
token.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package conf
import (
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"io/ioutil"
"murphysec-cli-simple/logger"
"os"
"path/filepath"
"strings"
"sync"
)
const tokenPath = "~/.murphysec/token"
// APITokenCliOverride 用于覆盖API TOKEN,为空串时无效
var APITokenCliOverride string
// _APITokenEnvOverride 用于环境变量覆盖 API TOKEN,为空串时无效
var _APITokenEnvOverride = func() string { return os.Getenv("API_TOKEN") }()
// tokenReader read token file from config directory, the file will only be read once time.
var tokenReader = func() func() string {
o := sync.Once{}
var t = ""
return func() string {
o.Do(func() {
dir, e := homedir.Expand(tokenPath)
if e != nil {
logger.Debug.Printf("Cannot get home path, ignore")
return
}
logger.Debug.Printf("Read token from: %s", dir)
data, e := ioutil.ReadFile(dir)
if e != nil {
logger.Debug.Printf("Read fail, ignore")
return
}
t = strings.TrimSpace(string(data))
})
return t
}
}()
func ReadTokenFile() (t string, e error) {
defer func() {
if e != nil {
e = errors.Wrap(e, "Read token failed")
}
}()
dir, e := homedir.Expand(tokenPath)
if e != nil {
logger.Err.Println("Expand home dir failed,", e.Error())
return "", e
}
logger.Debug.Println("Read token file at:", dir)
data, e := ioutil.ReadFile(dir)
if e != nil {
logger.Debug.Println("Read fail failed", e.Error())
return "", e
}
return strings.TrimSpace(string(data)), nil
}
// APIToken returns API token
func APIToken() string {
if len(strings.TrimSpace(APITokenCliOverride)) != 0 {
logger.Debug.Printf("Use API token from cli argument")
return APITokenCliOverride
}
if len(strings.TrimSpace(_APITokenEnvOverride)) != 0 {
logger.Debug.Printf("Use API token from env")
return _APITokenEnvOverride
}
logger.Debug.Printf("Use API token from config file")
return tokenReader()
}
// StoreToken store specified token to user local config
func StoreToken(token string) error {
path, err := homedir.Expand(tokenPath)
if err != nil {
return errors.Wrap(err, "Can't get your home dir.")
}
if e := os.MkdirAll(filepath.Dir(path), 0777); e != nil {
return errors.Wrap(e, "Create config dir failed.")
}
if e := ioutil.WriteFile(path, []byte(token), 0600); e != nil {
return errors.Wrap(e, "Write token file failed.")
}
return nil
}
// TokenFileNotFound will be returned when the token file not found
var TokenFileNotFound = errors.New("TokenFileNotFound")
// RemoveToken will delete local token file, return TokenFileNotFound if there is no token.
func RemoveToken() error {
path, err := homedir.Expand(tokenPath)
if err != nil {
return errors.Wrap(err, "Can't get your home dir.")
}
if stat, e := os.Stat(path); e != nil || stat.IsDir() {
return TokenFileNotFound
}
if e := os.Remove(path); e != nil {
return errors.Wrap(e, "Delete token file failed.")
}
return nil
}