-
Notifications
You must be signed in to change notification settings - Fork 3
/
token_helper.go
127 lines (111 loc) · 2.63 KB
/
token_helper.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package ensweb
import (
"bytes"
"io"
"os"
"path/filepath"
"strings"
homedir "github.com/mitchellh/go-homedir"
"github.com/natefinch/atomic"
)
var _ TokenHelper = (*InternalTokenHelper)(nil)
type InternalTokenHelper struct {
accessTokenPath string
refreshTokenPath string
homeDir string
accessFileName string
refreshFileName string
}
func NewInternalTokenHelper(dir string, accessFileName string, refreshFileName string) (*InternalTokenHelper, error) {
homeDir := dir
if homeDir == "" {
var err error
homeDir, err = homedir.Dir()
if err != nil {
return nil, err
}
}
ifh := &InternalTokenHelper{
homeDir: homeDir,
accessFileName: accessFileName,
refreshFileName: refreshFileName,
accessTokenPath: filepath.Join(homeDir, accessFileName),
refreshTokenPath: filepath.Join(homeDir, refreshFileName),
}
return ifh, nil
}
func (i *InternalTokenHelper) GetAccessToken() (string, error) {
f, err := os.Open(i.accessTokenPath)
if os.IsNotExist(err) {
return "", nil
}
if err != nil {
return "", err
}
defer f.Close()
buf := bytes.NewBuffer(nil)
if _, err := io.Copy(buf, f); err != nil {
return "", err
}
return strings.TrimSpace(buf.String()), nil
}
func (i *InternalTokenHelper) StoreAccessToken(input string) error {
tmpFile := i.accessTokenPath + ".tmp"
f, err := os.OpenFile(tmpFile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
if err != nil {
return err
}
defer f.Close()
defer os.Remove(tmpFile)
_, err = io.WriteString(f, input)
if err != nil {
return err
}
err = f.Close()
if err != nil {
return err
}
return atomic.ReplaceFile(tmpFile, i.accessTokenPath)
}
func (i *InternalTokenHelper) GetRefreshToken() (string, error) {
f, err := os.Open(i.refreshTokenPath)
if os.IsNotExist(err) {
return "", nil
}
if err != nil {
return "", err
}
defer f.Close()
buf := bytes.NewBuffer(nil)
if _, err := io.Copy(buf, f); err != nil {
return "", err
}
return strings.TrimSpace(buf.String()), nil
}
func (i *InternalTokenHelper) StoreRefreshToken(input string) error {
tmpFile := i.refreshTokenPath + ".tmp"
f, err := os.OpenFile(tmpFile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
if err != nil {
return err
}
defer f.Close()
defer os.Remove(tmpFile)
_, err = io.WriteString(f, input)
if err != nil {
return err
}
err = f.Close()
if err != nil {
return err
}
return atomic.ReplaceFile(tmpFile, i.refreshTokenPath)
}
func (i *InternalTokenHelper) Erase() error {
if err := os.Remove(i.accessTokenPath); err != nil && !os.IsNotExist(err) {
return err
}
if err := os.Remove(i.refreshTokenPath); err != nil && !os.IsNotExist(err) {
return err
}
return nil
}