-
Notifications
You must be signed in to change notification settings - Fork 1
/
store.go
61 lines (47 loc) · 1.06 KB
/
store.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
package main
import (
"encoding/json"
"os"
"path"
"golang.org/x/oauth2"
)
type FileTokenStore struct {
filename string
dir string
}
func NewFileTokenStore(filename string) *FileTokenStore {
// Ideally we store tokens in ~/.decanter
path, _ := os.UserHomeDir()
dynamicLocation := path + "/.decanter"
os.MkdirAll(dynamicLocation, 0755)
return &FileTokenStore{
filename: filename,
dir: dynamicLocation,
}
}
func (f FileTokenStore) Load() (oauth2.Token, error) {
return LoadAuthFromFile(path.Join(f.dir, f.filename))
}
func (f FileTokenStore) Save(r oauth2.Token) error {
return SaveAuthToFile(r, path.Join(f.dir, f.filename))
}
func SaveAuthToFile(r oauth2.Token, f string) error {
bytes, err := json.Marshal(r)
if err != nil {
return err
}
os.WriteFile(f, bytes, 0644)
return nil
}
func LoadAuthFromFile(f string) (oauth2.Token, error) {
bytes, err := os.ReadFile(f)
if err != nil {
return oauth2.Token{}, err
}
var r oauth2.Token
err = json.Unmarshal(bytes, &r)
if err != nil {
return oauth2.Token{}, err
}
return r, nil
}