-
Notifications
You must be signed in to change notification settings - Fork 22
/
token.go
195 lines (181 loc) · 4.89 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* OpenBmclAPI (Golang Edition)
* Copyright (C) 2024 Kevin Z <[email protected]>
* All rights reserved
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"bytes"
"context"
"crypto"
"crypto/hmac"
"encoding/hex"
"encoding/json"
"net/http"
"net/url"
"time"
"github.com/LiterMC/go-openbmclapi/log"
"github.com/LiterMC/go-openbmclapi/utils"
)
type ClusterToken struct {
Token string
ExpireAt time.Time
}
func (cr *Cluster) GetAuthToken(ctx context.Context) (token string, err error) {
cr.authTokenMux.RLock()
expired := cr.authToken == nil || cr.authToken.ExpireAt.Before(time.Now())
if !expired {
token = cr.authToken.Token
}
almostExpired := !expired && cr.authToken.ExpireAt.Add(-10*time.Minute).Before(time.Now())
cr.authTokenMux.RUnlock()
if expired {
cr.authTokenMux.Lock()
defer cr.authTokenMux.Unlock()
if cr.authToken == nil || cr.authToken.ExpireAt.Before(time.Now()) {
if cr.authToken, err = cr.fetchToken(ctx); err != nil {
return "", err
}
}
token = cr.authToken.Token
} else if almostExpired {
go func() {
tctx, cancel := context.WithTimeout(ctx, time.Second*30)
defer cancel()
cr.authTokenMux.Lock()
defer cr.authTokenMux.Unlock()
if cr.authToken != nil && cr.authToken.ExpireAt.Add(-10*time.Minute).Before(time.Now()) {
tk, err := cr.refreshToken(tctx, cr.authToken.Token)
if err != nil {
log.Errorf("Cannot refresh token: %v", err)
return
}
cr.authToken = tk
}
}()
}
return
}
func (cr *Cluster) fetchToken(ctx context.Context) (token *ClusterToken, err error) {
log.Infof("Fetching authorization token ...")
defer func() {
if err != nil {
log.Errorf("Cannot fetch authorization token: %v", err)
} else {
log.Infof("Authorization token fetched")
}
}()
req, err := cr.makeReq(ctx, http.MethodGet, "/openbmclapi-agent/challenge", url.Values{
"clusterId": {cr.clusterId},
})
if err != nil {
return
}
res, err := cr.client.Do(req)
if err != nil {
return
}
if res.StatusCode != http.StatusOK {
err = utils.NewHTTPStatusErrorFromResponse(res)
res.Body.Close()
return
}
var res1 struct {
Challenge string `json:"challenge"`
}
err = json.NewDecoder(res.Body).Decode(&res1)
res.Body.Close()
if err != nil {
return
}
var buf [32]byte
hs := hmac.New(crypto.SHA256.New, ([]byte)(cr.clusterSecret))
hs.Write(([]byte)(res1.Challenge))
signature := hex.EncodeToString(hs.Sum(buf[:0]))
payload, err := json.Marshal(struct {
ClusterId string `json:"clusterId"`
Challenge string `json:"challenge"`
Signature string `json:"signature"`
}{
ClusterId: cr.clusterId,
Challenge: res1.Challenge,
Signature: signature,
})
req, err = cr.makeReqWithBody(ctx, http.MethodPost, "/openbmclapi-agent/token", nil, bytes.NewReader(payload))
if err != nil {
return
}
req.Header.Set("Content-Type", "application/json")
res, err = cr.client.Do(req)
if err != nil {
return
}
if res.StatusCode/100 != 2 {
err = utils.NewHTTPStatusErrorFromResponse(res)
res.Body.Close()
return
}
var res2 struct {
Token string `json:"token"`
TTL int64 `json:"ttl"`
}
err = json.NewDecoder(res.Body).Decode(&res2)
res.Body.Close()
if err != nil {
return
}
return &ClusterToken{
Token: res2.Token,
ExpireAt: time.Now().Add((time.Duration)(res2.TTL)*time.Millisecond - 10*time.Second),
}, nil
}
func (cr *Cluster) refreshToken(ctx context.Context, oldToken string) (token *ClusterToken, err error) {
payload, err := json.Marshal(struct {
ClusterId string `json:"clusterId"`
Token string `json:"token"`
}{
ClusterId: cr.clusterId,
Token: oldToken,
})
if err != nil {
return
}
req, err := cr.makeReqWithBody(ctx, http.MethodPost, "/openbmclapi-agent/token", nil, bytes.NewReader(payload))
if err != nil {
return
}
req.Header.Set("Content-Type", "application/json")
resp, err := cr.client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
err = utils.NewHTTPStatusErrorFromResponse(resp)
return
}
var res struct {
Token string `json:"token"`
TTL int64 `json:"ttl"`
}
if err = json.NewDecoder(resp.Body).Decode(&res); err != nil {
return
}
return &ClusterToken{
Token: res.Token,
ExpireAt: time.Now().Add((time.Duration)(res.TTL)*time.Millisecond - 10*time.Second),
}, nil
}