11package cli
22
33import (
4+ "context"
45 "fmt"
56 "net/url"
67
78 "github.com/spf13/cobra"
9+ "github.com/thand-io/agent/internal/common"
10+ "github.com/thand-io/agent/internal/models"
811)
912
1013var loginCmd = & cobra.Command {
@@ -27,12 +30,26 @@ var loginCmd = &cobra.Command{
2730}
2831
2932func runLogin (cmd * cobra.Command , args []string ) error {
33+ return authKickStart ()
34+ }
35+
36+ func authKickStart () error {
37+ // Set up signal handling for graceful cancellation
38+ ctx , cleanup := common .WithInterrupt (context .Background ())
39+ defer cleanup ()
3040
3141 hostname := cfg .GetLoginServerHostname ()
3242 fmt .Println ("Login server hostname:" , hostname )
3343
44+ // Prepare callback URL with local server endpoint
45+
46+ if ! cfg .GetServices ().HasEncryption () {
47+ return fmt .Errorf ("encryption service is not configured" )
48+ }
49+
3450 callbackUrl := url.Values {
3551 "callback" : {cfg .GetLocalServerUrl ()},
52+ "code" : {createAuthCode ()},
3653 }
3754
3855 // Use the configured login server if no override provided
@@ -50,10 +67,15 @@ func runLogin(cmd *cobra.Command, args []string) error {
5067
5168 // Wait for the session to be established (using empty provider for general login)
5269 session := sessionManager .AwaitRefresh (
70+ ctx ,
5371 cfg .GetLoginServerHostname (),
5472 )
5573
5674 if session == nil {
75+ // Check if context was cancelled
76+ if ctx .Err () != nil {
77+ return fmt .Errorf ("login cancelled" )
78+ }
5779 return fmt .Errorf ("authentication failed or timed out" )
5880 }
5981
@@ -69,3 +91,16 @@ func init() {
6991 // Add the command to the root
7092 rootCmd .AddCommand (loginCmd )
7193}
94+
95+ func createAuthCode () string {
96+ code := models.EncodingWrapper {
97+ Type : models .ENCODED_SESSION_CODE ,
98+ Data : models .NewCodeWrapper (
99+ cfg .GetLoginServerUrl (),
100+ ),
101+ }.EncodeAndEncrypt (
102+ cfg .GetServices ().GetEncryption (),
103+ )
104+
105+ return code
106+ }
0 commit comments