@@ -41,16 +41,18 @@ func (r *RootCmd) login() *clibase.Cmd {
41
41
const firstUserTrialEnv = "CODER_FIRST_USER_TRIAL"
42
42
43
43
var (
44
- email string
45
- username string
46
- password string
47
- trial bool
44
+ email string
45
+ username string
46
+ password string
47
+ trial bool
48
+ useTokenForSession bool
48
49
)
49
50
cmd := & clibase.Cmd {
50
51
Use : "login <url>" ,
51
52
Short : "Authenticate with Coder deployment" ,
52
53
Middleware : clibase .RequireRangeArgs (0 , 1 ),
53
54
Handler : func (inv * clibase.Invocation ) error {
55
+ ctx := inv .Context ()
54
56
rawURL := ""
55
57
if len (inv .Args ) == 0 {
56
58
rawURL = r .clientURL .String ()
@@ -89,7 +91,7 @@ func (r *RootCmd) login() *clibase.Cmd {
89
91
_ , _ = fmt .Fprintln (inv .Stderr , cliui .DefaultStyles .Warn .Render (err .Error ()))
90
92
}
91
93
92
- hasInitialUser , err := client .HasFirstUser (inv . Context () )
94
+ hasInitialUser , err := client .HasFirstUser (ctx )
93
95
if err != nil {
94
96
return xerrors .Errorf ("Failed to check server %q for first user, is the URL correct and is coder accessible from your browser? Error - has initial user: %w" , serverURL .String (), err )
95
97
}
@@ -182,7 +184,7 @@ func (r *RootCmd) login() *clibase.Cmd {
182
184
trial = v == "yes" || v == "y"
183
185
}
184
186
185
- _ , err = client .CreateFirstUser (inv . Context () , codersdk.CreateFirstUserRequest {
187
+ _ , err = client .CreateFirstUser (ctx , codersdk.CreateFirstUserRequest {
186
188
Email : email ,
187
189
Username : username ,
188
190
Password : password ,
@@ -191,7 +193,7 @@ func (r *RootCmd) login() *clibase.Cmd {
191
193
if err != nil {
192
194
return xerrors .Errorf ("create initial user: %w" , err )
193
195
}
194
- resp , err := client .LoginWithPassword (inv . Context () , codersdk.LoginWithPasswordRequest {
196
+ resp , err := client .LoginWithPassword (ctx , codersdk.LoginWithPasswordRequest {
195
197
Email : email ,
196
198
Password : password ,
197
199
})
@@ -235,7 +237,7 @@ func (r *RootCmd) login() *clibase.Cmd {
235
237
Secret : true ,
236
238
Validate : func (token string ) error {
237
239
client .SetSessionToken (token )
238
- _ , err := client .User (inv . Context () , codersdk .Me )
240
+ _ , err := client .User (ctx , codersdk .Me )
239
241
if err != nil {
240
242
return xerrors .New ("That's not a valid token!" )
241
243
}
@@ -245,11 +247,27 @@ func (r *RootCmd) login() *clibase.Cmd {
245
247
if err != nil {
246
248
return xerrors .Errorf ("paste token prompt: %w" , err )
247
249
}
250
+ } else if ! useTokenForSession {
251
+ // If a session token is provided on the cli, use it to generate
252
+ // a new one. This is because the cli `--token` flag provides
253
+ // a token for the command being invoked. We should not store
254
+ // this token, and `/logout` should not delete it.
255
+ // /login should generate a new token and store that.
256
+ client .SetSessionToken (sessionToken )
257
+ // Use CreateAPIKey over CreateToken because this is a session
258
+ // key that should not show on the `tokens` page. This should
259
+ // match the same behavior of the `/cli-auth` page for generating
260
+ // a session token.
261
+ key , err := client .CreateAPIKey (ctx , "me" )
262
+ if err != nil {
263
+ return xerrors .Errorf ("create api key: %w" , err )
264
+ }
265
+ sessionToken = key .Key
248
266
}
249
267
250
268
// Login to get user data - verify it is OK before persisting
251
269
client .SetSessionToken (sessionToken )
252
- resp , err := client .User (inv . Context () , codersdk .Me )
270
+ resp , err := client .User (ctx , codersdk .Me )
253
271
if err != nil {
254
272
return xerrors .Errorf ("get user: %w" , err )
255
273
}
@@ -293,6 +311,11 @@ func (r *RootCmd) login() *clibase.Cmd {
293
311
Description : "Specifies whether a trial license should be provisioned for the Coder deployment or not." ,
294
312
Value : clibase .BoolOf (& trial ),
295
313
},
314
+ {
315
+ Flag : "use-token-as-session" ,
316
+ Description : "By default, the CLI will generate a new session token when logging in. This flag will instead use the provided token as the session token." ,
317
+ Value : clibase .BoolOf (& useTokenForSession ),
318
+ },
296
319
}
297
320
return cmd
298
321
}
0 commit comments