-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshellicator.go
More file actions
451 lines (381 loc) · 12.8 KB
/
shellicator.go
File metadata and controls
451 lines (381 loc) · 12.8 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// Package shellicator is a simple library to get oauth2 tokens for shell commands.
//
// This library opens a local http server to receive a callback from an oauth2 provider and stores the received token locally.
// It prints out the URL of the provider and if configured, opens a browser pointing to the oauth2 authcode URL.
package shellicator
import (
"context"
"crypto/rand"
"crypto/subtle"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"html/template"
"io/ioutil"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"time"
serr "github.com/go-sharp/shellicator/errors"
"golang.org/x/oauth2"
)
// Provider is an interface that holds the configuration for the shellicator.
type Provider interface {
OAuth2Config() oauth2.Config
DeviceAuthURL() string
}
// Storager persists tokens for later use.
type Storager interface {
RetrieveToken(key string) (*oauth2.Token, error)
StoreToken(key string, token *oauth2.Token) error
}
// PrinterCtx passed to the printer function.
type PrinterCtx struct {
URL string
UserCode string
}
// VerificationURL returns the verification url and can be used to generate a QR code.
func (p PrinterCtx) VerificationURL() string {
return fmt.Sprintf("%v?user_code=%v", p.URL, p.UserCode)
}
// IsDeviceGrant returns true if device grant flow is used
func (p PrinterCtx) IsDeviceGrant() bool {
return p.UserCode != ""
}
// NewAuthenticator returns a new Authenticator.
func NewAuthenticator(opts ...AuthOptions) Authenticator {
t, _ := template.New("callback").Parse("Successfully received oauth token, you may close this window now.")
authenticator := Authenticator{
providers: make(map[string]provConfig),
timeout: time.Minute * 5,
ports: []int{42000, 42001, 42002, 42003, 42004, 42005, 42006, 42007, 42008, 42009},
cbTemplate: t,
}
for _, fn := range opts {
fn(&authenticator)
}
if len(authenticator.providers) == 0 {
panic("shellicator: NewAuthenticator needs at least one provider option")
}
if authenticator.store == nil {
authenticator.store = &MemoryStorage{}
}
if authenticator.printer == nil {
authenticator.printer = defaultPrinter(authenticator.openBrowser)
}
return authenticator
}
// AuthOptions sets options for the Authenticator.
type AuthOptions func(*Authenticator)
// Authenticator handles oauth tokens for a command line application.
type Authenticator struct {
openBrowser bool
ports []int
timeout time.Duration
store Storager
providers map[string]provConfig
listener net.Listener
printer func(PrinterCtx)
cbTemplate *template.Template
}
// Authenticate opens a browser windows and navigates to the configured oauth provider (if configured).
// Otherwise it prints only the URL to the oauth provider.
func (a Authenticator) Authenticate(key string) error {
prov, ok := a.providers[key]
if !ok {
return serr.ErrProviderNotFound.WithMessage(fmt.Sprintf("Authenticator: no provider with the given key %v found", key))
}
// Check requirments for all grant types
cfg := prov.provider.OAuth2Config()
if cfg.ClientID == "" || cfg.Endpoint.TokenURL == "" {
return serr.ErrProviderCfgInvalid.WithMessage(
fmt.Sprintf("Authenticator: invalid provider configuration for key %v, missing ClientID or TokenURL", key))
}
var token *oauth2.Token
var err error
if prov.useDeviceGrant {
token, err = a.handleDeviceGrantFlow(key, prov.provider)
} else {
token, err = a.handleAuthFlow(key, cfg)
}
if err != nil {
return err
}
return a.store.StoreToken(key, token)
}
// NewClient returns a new http client with a stored oauth token.
// If no valid token was found, an ErrTokenNotFound error is returned.
func (a Authenticator) NewClient(ctx context.Context, key string) (*http.Client, error) {
if p, ok := a.providers[key]; ok {
t, err := a.GetToken(key)
if err != nil {
return nil, err
}
c := p.provider.OAuth2Config()
return c.Client(ctx, t), nil
}
return nil, serr.ErrProviderNotFound.WithMessage(fmt.Sprintf("Authenticator: no provider with the given key %v found", key))
}
// GetToken gets a stored oauth token.
func (a Authenticator) GetToken(key string) (*oauth2.Token, error) {
t, err := a.store.RetrieveToken(key)
if err != nil {
return nil, err
}
return t, nil
}
// WithProvider configures an oauth provider with the specified key.
// RedirectURI will be overwritten and must not be set.
func WithProvider(key string, prov Provider) AuthOptions {
return func(a *Authenticator) {
if prov == nil {
panic("Authenticator: provider must not be nil")
}
cfg := a.providers[key]
cfg.provider = prov
a.providers[key] = cfg
}
}
// WithUseDeviceGrant configures the authenticator to use the device grant flow.
func WithUseDeviceGrant(key string, useDeviceGrant bool) AuthOptions {
return func(a *Authenticator) {
cfg := a.providers[key]
cfg.useDeviceGrant = useDeviceGrant
a.providers[key] = cfg
}
}
// WithPorts configures the Authenticator to use the specified ports.
// Default: 42000 - 42009
func WithPorts(ports ...int) AuthOptions {
return func(a *Authenticator) {
if len(ports) > 0 {
a.ports = ports
}
}
}
// WithTimeout configures the timeout for the Authenticator.
// If timeout reached the Authenticator closes the local server
// and returns an error. Default: 5 minutes.
func WithTimeout(d time.Duration) AuthOptions {
return func(a *Authenticator) {
a.timeout = d
}
}
// WithUseOpenBrowserFeature configures if a browser should automatically openend
// and navigate to the oauth AuthCodeURL. Default: false.
func WithUseOpenBrowserFeature(openBrowser bool) AuthOptions {
return func(a *Authenticator) {
a.openBrowser = openBrowser
}
}
// WithUsePrinter configures to use the supplied function to print out the oauth AuthCodeURL.
// The function receives the AuthCodeURL of the chosen oauth provider.
func WithUsePrinter(printer func(ctx PrinterCtx)) AuthOptions {
return func(a *Authenticator) {
if printer == nil {
panic("shellicator: WithUsePrinter expects a function not nil")
}
a.printer = printer
}
}
// WithCallbackTemplate configures the authenticator to use the specified html
// for the callback page.
func WithCallbackTemplate(html string) AuthOptions {
return func(a *Authenticator) {
t, err := template.New("callback").Parse(html)
if err != nil {
panic("shellicator: WithCallbackTemplate received an invalid template: " + err.Error())
}
a.cbTemplate = t
}
}
// WithStore configures the authenticator to use the provided storager
// to save and restore tokens.
func WithStore(store Storager) AuthOptions {
return func(a *Authenticator) {
if store == nil {
panic("shellicator: WithStore expects a not nil Storager")
}
a.store = store
}
}
func (a Authenticator) handleDeviceGrantFlow(key string, prov Provider) (*oauth2.Token, error) {
if prov.DeviceAuthURL() == "" {
return nil, serr.ErrProviderCfgInvalid.WithMessage(
fmt.Sprintf("Authenticator: invalid provider configuration for key %v, missing device auth url", key))
}
resp, err := http.PostForm(prov.DeviceAuthURL(), url.Values{
"client_id": {prov.OAuth2Config().ClientID},
"scope": {strings.Join(prov.OAuth2Config().Scopes, " ")},
})
if err != nil {
return nil, serr.ErrGeneric.WithMessageAndError("Authenticator: failed to get device code", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
fmt.Println(prov.OAuth2Config())
return nil, serr.ErrGeneric.WithMessage("Authenticator: failed to get device code: " + resp.Status)
}
var res deviceGrantResponse
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
return nil, serr.ErrGeneric.WithMessageAndError("Authenticator: failed to decode answer", err)
}
a.printer(PrinterCtx{URL: res.VerificationURI, UserCode: res.UserCode})
return a.requestAccessToken(prov, res)
}
func (a Authenticator) requestAccessToken(prov Provider, res deviceGrantResponse) (*oauth2.Token, error) {
interval := res.getInterval()
timeout := time.Second * time.Duration(res.Expires)
if timeout > a.timeout {
timeout = a.timeout
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
for {
resp, err := http.PostForm(prov.OAuth2Config().Endpoint.TokenURL, url.Values{
"grant_type": {"urn:ietf:params:oauth:grant-type:device_code"},
"device_code": {res.DeviceCode},
"client_id": {prov.OAuth2Config().ClientID},
})
if err != nil {
// If the error is only temporary or timeout is reached,
// increase the wait interval.
if !(err.(*url.Error).Temporary() || err.(*url.Error).Timeout()) {
return nil, serr.ErrGeneric.WithWrappedError(err)
}
interval *= 2
} else {
data, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, serr.ErrGeneric.WithWrappedError(err)
}
// If we get a 200 status code, decode token and return it
if resp.StatusCode == 200 {
var token tokenJSON
if err := json.Unmarshal(data, &token); err != nil {
return nil, serr.ErrGeneric.WithWrappedError(err)
}
return &oauth2.Token{
AccessToken: token.AccessToken,
TokenType: token.TokenType,
RefreshToken: token.RefreshToken,
Expiry: token.expiry(),
}, nil
}
var errResp devAccessTokenErrResponse
if err := json.Unmarshal(data, &errResp); err != nil {
return nil, serr.ErrGeneric.WithWrappedError(err)
}
switch errResp.Error {
case devTokRespSlowDown:
interval += 5 * time.Second
case devTokRespAuthorizationPending:
// Do nothing and poll again
default:
return nil, serr.ErrGeneric.WithMessage(fmt.Sprintf("Authenticator: %v: %v ", errResp.Error, errResp.ErrorDescription))
}
}
// Wait for timeout or interval elapses
select {
case <-time.After(interval):
continue
case <-ctx.Done():
return nil, serr.ErrTimeout.WithMessage("Authenticator: timeout while waiting for oauth response")
}
}
}
func (a Authenticator) handleAuthFlow(key string, cfg oauth2.Config) (*oauth2.Token, error) {
if cfg.Endpoint.AuthURL == "" || cfg.Endpoint.TokenURL == "" {
return nil, serr.ErrProviderCfgInvalid.WithMessage(fmt.Sprintf("Authenticator: invalid provider configuration for key %v", key))
}
ln, port, err := getListener(a.ports)
if err != nil {
return nil, err
}
cfg.RedirectURL = fmt.Sprintf("http://localhost:%v/callback", port)
ctx, cancel := context.WithTimeout(context.Background(), a.timeout)
defer cancel()
return a.handleTokenExchange(ctx, ln, cfg)
}
func (a Authenticator) handleTokenExchange(ctx context.Context, ln net.Listener, cfg oauth2.Config) (*oauth2.Token, error) {
b := make([]byte, 32)
if _, err := rand.Read(b); err != nil {
return nil, fmt.Errorf("Authenticator: failed to create state parameter: %v", err)
}
state := base64.StdEncoding.EncodeToString(b)
okCh := make(chan string)
errCh := make(chan error)
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
rstate := r.URL.Query().Get("state")
rcode := r.URL.Query().Get("code")
if rstate == "" || rcode == "" {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, http.StatusText(http.StatusBadRequest))
errCh <- errors.New("did not receive a proper oauth response")
return
}
if subtle.ConstantTimeCompare([]byte(state), []byte(rstate)) == 0 {
w.WriteHeader(http.StatusForbidden)
fmt.Fprint(w, http.StatusText(http.StatusForbidden))
errCh <- errors.New("state parameter mismatch")
return
}
a.cbTemplate.Execute(w, nil)
okCh <- rcode
})
server := http.Server{Handler: mux}
go func() {
if err := server.Serve(ln); err != nil && err != http.ErrServerClosed {
errCh <- err
}
}()
defer server.Close()
// Open browser and print Auth URL
// TODO: Implement PKCE
url := cfg.AuthCodeURL(state, oauth2.AccessTypeOffline)
a.printer(PrinterCtx{URL: url})
if a.openBrowser {
openURL(url)
}
var code string
select {
case code = <-okCh:
// Do nothing
case err := <-errCh:
return nil, err
case <-ctx.Done():
return nil, serr.ErrTimeout.WithMessage("Authenticator: timeout while waiting for oauth response")
}
t, err := cfg.Exchange(ctx, code, oauth2.AccessTypeOffline)
if err != nil {
return nil, err
}
return t, nil
}
func getListener(ports []int) (net.Listener, int, error) {
for _, p := range ports {
l, err := net.Listen("tcp", "localhost:"+strconv.Itoa(p))
if err == nil {
return l, p, nil
}
}
return nil, -1, fmt.Errorf("could not bind any of these local ports: %v", ports)
}
func defaultPrinter(openBrowser bool) func(PrinterCtx) {
return func(ctx PrinterCtx) {
if ctx.IsDeviceGrant() {
fmt.Printf("Open your browser and navigate to the following url: '%v'\nEnter this code: '%v'\n", ctx.URL, ctx.UserCode)
return
}
if openBrowser {
fmt.Printf("If your browser doesn't open automatically, navigate to the following url and authenticate yourself:\n%v\n", ctx.URL)
return
}
fmt.Printf("Open your browser and navigate to the following url to authenticate yourself:\n%v\n", ctx.URL)
}
}