-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
213 lines (172 loc) · 4.51 KB
/
client.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* Copyright (c) 2023 Sebastian Borchers
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
package wego
import (
"context"
"errors"
"net/http"
"path/filepath"
"sync"
"time"
"github.com/desertbit/closer/v3"
"github.com/rs/zerolog/log"
)
const (
mimeJSON = "application/json"
mimeURL = "application/x-www-form-urlencoded"
)
type Options struct {
// Mandataroy fields.
// The address of the wekan server the client should connect to.
RemoteAddr string
// The username of the user that should be used to log in.
Username string
// The password of the user that should be used to log in.
Password string
// Optional fields.
// The HTTP client that should be used.
// If nil, a default client is used.
Client *http.Client
// The time the client waits between login attempts.
// Can not be shorter than 1 second.
TimeBetweenLoginAttemps time.Duration
// The closer used to manage all routines of the client.
// If nil, a default closer is created.
Closer closer.Closer
}
type Client struct {
closer.Closer
opts Options
httpc *http.Client
// Unbuffered channel that used to distribute API tokens to the request methods.
authChan chan chan string
mx sync.Mutex
mxUserID string
}
func NewClient(opts Options) (*Client, error) {
c := &Client{
Closer: opts.Closer,
opts: opts,
httpc: opts.Client,
authChan: make(chan chan string),
}
// Assign default values.
if opts.Client == nil {
c.httpc = &http.Client{
Timeout: 30 * time.Second,
}
}
if opts.TimeBetweenLoginAttemps < time.Second {
c.opts.TimeBetweenLoginAttemps = time.Second
}
if opts.Closer == nil {
c.Closer = closer.New()
}
// Start routines.
ctx, cancel := c.Context()
defer cancel()
// Request the first token.
// Error can only be a context.ErrCanceled.
token, tokenExpires, err := c.loginUntilSuccess(ctx)
if err != nil {
return nil, err
}
c.startConnectionRoutine(token, tokenExpires)
return c, nil
}
func (c *Client) startConnectionRoutine(token string, tokenExpires time.Time) {
c.CloserAddWait(1)
go c.connectionRoutine(token, tokenExpires)
}
func (c *Client) connectionRoutine(token string, tokenExpires time.Time) {
defer c.CloseAndDone_()
ctx, cancel := c.Context()
defer cancel()
var (
err error
closingChan = c.ClosingChan()
)
// Start a timer so we renew our token.
expires := time.NewTimer(time.Until(tokenExpires) - 5*time.Second)
defer expires.Stop()
for {
select {
case <-closingChan:
return
case <-expires.C:
// Token is expired, login to retrieve a new one.
token, tokenExpires, err = c.loginUntilSuccess(ctx)
if err != nil {
if !errors.Is(err, context.Canceled) {
log.Error().Err(err).Msg("connectionRoutine")
}
return
}
// Restart the timer to renew our token.
expires.Reset(time.Until(tokenExpires) - 5*time.Second)
case tokenChan := <-c.authChan:
// Buffered channel, no select needed.
tokenChan <- token
}
}
}
// loginUntilSuccess attempts to login over and over again until successful.
// If a login succeeds, the userID is saved in c and the auth token gets returned.
// The login process is aborted, when the provided context closes.
func (c *Client) loginUntilSuccess(ctx context.Context) (token string, tokenExpires time.Time, err error) {
var resp LoginResponse
for {
resp, err = c.Login(ctx, c.opts.Username, c.opts.Password)
if err != nil {
if ctx.Err() != nil {
err = ctx.Err()
return
}
log.Error().Err(err).Msg("connectionRoutine: login")
time.Sleep(c.opts.TimeBetweenLoginAttemps)
continue
}
// Successfully logged in.
token = resp.Token
tokenExpires = resp.TokenExpires
// Save the user's id.
c.mx.Lock()
c.mxUserID = resp.ID
c.mx.Unlock()
return
}
}
func (c *Client) authenticateRequest(ctx context.Context, req *http.Request) error {
token, err := c.token(ctx)
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+token)
return nil
}
func (c *Client) token(ctx context.Context) (string, error) {
// Buffered so the connection routine can immediately resume its work.
tokenChan := make(chan string, 1)
select {
case <-c.ClosingChan():
return "", closer.ErrClosed
case <-ctx.Done():
return "", ctx.Err()
case c.authChan <- tokenChan:
}
select {
case <-c.ClosingChan():
return "", closer.ErrClosed
case <-ctx.Done():
return "", ctx.Err()
case token := <-tokenChan:
return token, nil
}
}
func (c *Client) endpoint(segments ...string) string {
return "/api/" + filepath.Join(segments...)
}