This repository has been archived by the owner on May 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
login.go
168 lines (137 loc) · 3.73 KB
/
login.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
package commands
import (
"net/http"
"time"
"github.com/99designs/keyring"
"github.com/AlecAivazis/survey/v2"
"github.com/common-fate/clio"
"github.com/common-fate/common-fate/pkg/authflow"
"github.com/common-fate/common-fate/pkg/cliconfig"
"github.com/common-fate/common-fate/pkg/tokenstore"
"github.com/pkg/browser"
"github.com/urfave/cli/v2"
"golang.org/x/sync/errgroup"
)
var Login = cli.Command{
Name: "login",
Usage: "Log in to Common Fate",
Flags: []cli.Flag{
&cli.BoolFlag{Name: "lazy", Usage: "the lazy flag lets granted decide whether a new login flow should be initiated based on the token expiry"},
},
Action: defaultLoginFlow.LoginAction,
}
// defaultLoginFlow is the login flow without any
// customisations to token storage.
var defaultLoginFlow LoginFlow
type LoginFlow struct {
// Keyring optionally overrides the keyring that auth tokens are saved to.
Keyring keyring.Keyring
// ForceInteractive forces the survey prompt to appear
ForceInteractive bool
}
func (lf LoginFlow) LoginAction(c *cli.Context) error {
cfg, err := cliconfig.Load()
if err != nil {
return err
}
var url string
if !lf.ForceInteractive {
// try and read the URL from the first provided argument
url = c.Args().First()
}
if url == "" {
prompt := &survey.Input{
Message: "Your Common Fate dashboard URL",
Default: cfg.CurrentOrEmpty().DashboardURL,
}
err = survey.AskOne(prompt, &url, survey.WithValidator(survey.Required))
if err != nil {
return err
}
}
ctx := c.Context
//check expire for current token if it exists
ts := tokenstore.New(cfg.CurrentContext, tokenstore.WithKeyring(lf.Keyring))
token, err := ts.Token()
if err != nil && err != tokenstore.ErrNotFound {
return err
}
//what do we consider to be 'close to expiry' for now ill set it at 5 minutes?
if token != nil && c.Bool("lazy") {
now := time.Now()
shouldRefresh := tokenstore.ShouldRefreshToken(*token, now)
if !shouldRefresh {
clio.Debug("Auth token still valid, skipping login flow.")
return nil
}
}
authResponse := make(chan authflow.Response)
var g errgroup.Group
authServer, err := authflow.FromDashboardURL(ctx, authflow.Opts{
Response: authResponse,
DashboardURL: url,
})
if err != nil {
return err
}
server := &http.Server{
Addr: ":18900",
Handler: authServer.Handler(),
}
// run the auth server on localhost
g.Go(func() error {
clio.Debugw("starting HTTP server", "address", server.Addr)
if err := server.ListenAndServe(); err != http.ErrServerClosed {
return err
}
clio.Debugw("auth server closed")
return nil
})
// read the returned ID token from Cognito
g.Go(func() error {
res := <-authResponse
err := server.Shutdown(ctx)
if err != nil {
return err
}
// check that the auth flow didn't error out
if res.Err != nil {
return err
}
// update the config file
cfg.CurrentContext = "default"
// is it a new URL if so, add it and reset config
// otherwise it stays the same (which will preserve existing config; api_url)
if cfg.Contexts["default"].DashboardURL != res.DashboardURL {
cfg.Contexts["default"] = cliconfig.Context{
DashboardURL: res.DashboardURL,
}
}
err = cliconfig.Save(cfg)
if err != nil {
return err
}
ts := tokenstore.New(cfg.CurrentContext, tokenstore.WithKeyring(lf.Keyring))
err = ts.Save(res.Token)
if err != nil {
return err
}
clio.Successf("logged in")
return nil
})
// open the browser and read the token
g.Go(func() error {
u := "http://localhost:18900/auth/cognito/login"
clio.Infof("Opening your web browser to: %s", u)
err := browser.OpenURL(u)
if err != nil {
clio.Errorf("error opening browser: %s", err)
}
return nil
})
err = g.Wait()
if err != nil {
return err
}
return nil
}