@@ -126,6 +126,43 @@ func loginCmd(ctx context.Context, cfg *config, client api.Client, endpointArg s
126126 }
127127 fmt .Fprintln (out )
128128 fmt .Fprintf (out , "✔️ Authenticated as %s on %s\n " , result .CurrentUser .Username , endpointArg )
129+
130+ if p .useDeviceFlow {
131+ fmt .Fprintln (out )
132+ fmt .Fprintf (out , "To use this access token, set the following environment variables in your terminal:\n \n " )
133+ fmt .Fprintf (out , " export SRC_ENDPOINT=%s\n " , endpointArg )
134+ fmt .Fprintf (out , " export SRC_ACCESS_TOKEN=%s\n " , cfg .AccessToken )
135+ }
136+
129137 fmt .Fprintln (out )
130138 return nil
131139}
140+
141+ func runDeviceFlow (ctx context.Context , endpoint string , out io.Writer , client oauthdevice.Client ) (string , error ) {
142+ authResp , err := client .Start (ctx , endpoint , nil )
143+ if err != nil {
144+ return "" , err
145+ }
146+
147+ fmt .Fprintln (out )
148+ fmt .Fprintf (out , "To authenticate, visit %s and enter the code: %s\n " , authResp .VerificationURI , authResp .UserCode )
149+ if authResp .VerificationURIComplete != "" {
150+ fmt .Fprintln (out )
151+ fmt .Fprintf (out , "Alternatively, you can open: %s\n " , authResp .VerificationURIComplete )
152+ }
153+ fmt .Fprintln (out )
154+ fmt .Fprint (out , "Waiting for authorization..." )
155+ defer fmt .Fprintf (out , "DONE\n \n " )
156+
157+ interval := time .Duration (authResp .Interval ) * time .Second
158+ if interval <= 0 {
159+ interval = 5 * time .Second
160+ }
161+
162+ tokenResp , err := client .Poll (ctx , endpoint , authResp .DeviceCode , interval , authResp .ExpiresIn )
163+ if err != nil {
164+ return "" , err
165+ }
166+
167+ return tokenResp .AccessToken , nil
168+ }
0 commit comments