-
Notifications
You must be signed in to change notification settings - Fork 3
/
token.go
157 lines (142 loc) · 4.44 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// QCLauncher by syncore <[email protected]> 2017
// https://github.com/syncore/qclauncher
package qclauncher
import (
"bytes"
"encoding/gob"
"fmt"
bolt "github.com/coreos/bbolt"
)
type TokenAuth struct {
Token string
}
type TokenKey struct {
Key []byte
}
func (t *TokenAuth) get(ls *LauncherStore) error {
ls.checkDataFile(false)
if err := ls.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucketSettings))
decerr := t.decode(b.Get([]byte(keyTokenAuth)), b.Get([]byte(keyTokenKey)))
if decerr != nil {
logger.Errorw(fmt.Sprintf("%s: error decoding auth token from datastore during get operation", GetCaller()),
"error", decerr)
return decerr
}
return nil
}); err != nil {
logger.Errorw(fmt.Sprintf("%s: error getting auth token from datastore", GetCaller()), "error", err)
return err
}
return nil
}
func (t *TokenAuth) save(ls *LauncherStore) error {
return ls.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte(bucketSettings))
if err != nil {
logger.Errorw(fmt.Sprintf("%s: error creating settings bucket in datastore during save operation",
GetCaller()), "error", err)
return err
}
key := b.Get([]byte(keyTokenKey))
encoded, err := t.encode(key)
if err != nil {
logger.Errorw(fmt.Sprintf("%s: error encoding auth token during datastore save operation", GetCaller()),
"error", err)
return err
}
return b.Put([]byte(keyTokenAuth), encoded)
})
}
func (t *TokenAuth) decode(data, key []byte) error {
if err := gob.NewDecoder(bytes.NewBuffer(data)).Decode(&t); err != nil {
logger.Errorw(fmt.Sprintf("%s: error decoding auth token data", GetCaller()), "error", err)
return err
}
qcDecToken, err := decrypt(t.Token, &key)
if err != nil {
logger.Errorw(fmt.Sprintf("%s: error decrypting auth token credential", GetCaller()), "error", err)
return err
}
t.Token = qcDecToken
return nil
}
func (t *TokenAuth) encode(existingKey []byte) ([]byte, error) {
buf := new(bytes.Buffer)
encToken, err := encrypt(t.Token, &existingKey)
if err != nil {
logger.Errorw(fmt.Sprintf("%s: error encrypting auth token credential", GetCaller()), "error", err)
return nil, err
}
t.Token = encToken
if err = gob.NewEncoder(buf).Encode(t); err != nil {
logger.Errorw(fmt.Sprintf("%s: error encoding auth token data", GetCaller()), "error", err)
return nil, err
}
return buf.Bytes(), nil
}
func (t *TokenKey) get(ls *LauncherStore) error {
if err := ls.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucketSettings))
decerr := t.decode(b.Get([]byte(keyTokenKey)))
if decerr != nil {
logger.Errorw(fmt.Sprintf("%s: error decoding credential key from datastore during get operation", GetCaller()),
"error", decerr)
return decerr
}
return nil
}); err != nil {
logger.Errorw(fmt.Sprintf("%s: error getting credential key from datastore", GetCaller()), "error", err)
return err
}
return nil
}
func (t *TokenKey) save(ls *LauncherStore) error {
return ls.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte(bucketSettings))
if err != nil {
logger.Errorw(fmt.Sprintf("%s: error creating settings bucket in datastore during save operation",
GetCaller()), "error", err)
return err
}
encoded, err := t.encode()
if err != nil {
logger.Errorw(fmt.Sprintf("%s: error encoding credential key during datastore save operation", GetCaller()),
"error", err)
return err
}
return b.Put([]byte(keyTokenKey), encoded)
})
}
func (t *TokenKey) decode(data []byte) error {
dec := gob.NewDecoder(bytes.NewBuffer(data))
if err := dec.Decode(&t); err != nil {
logger.Errorw(fmt.Sprintf("%s: error decoding credential key data", GetCaller()), "error", err)
return err
}
return nil
}
func (t *TokenKey) encode() ([]byte, error) {
buf := new(bytes.Buffer)
enc := gob.NewEncoder(buf)
err := enc.Encode(t)
if err != nil {
logger.Errorw(fmt.Sprintf("%s: error encoding credential key data", GetCaller()), "error", err)
return nil, err
}
return buf.Bytes(), nil
}
func updateAuthToken(isPreSaveVerification bool, token string) error {
if isPreSaveVerification {
// Data file won't exist on first-run credential verification; which is the entry point into
// the data store, so save token & key in temp vars so they will be applied when the core
// settings are saved.
tmpToken = token
tmpKey = genKey()
return nil
}
return Save(&TokenAuth{Token: token})
}
func clearAuthToken() error {
return Save(&TokenAuth{Token: ""})
}