MCPcopy Create free account
hub / github.com/chainloop-dev/chainloop / interactiveAuth

Function interactiveAuth

app/cli/cmd/auth_login.go:56–110  ·  view source on GitHub ↗
(forceHeadless bool)

Source from the content-addressed store, hash-verified

54}
55
56func interactiveAuth(forceHeadless bool) error {
57 var a app
58
59 listener, callbackURL, err := localListenerAndCallbackURL()
60 if err != nil {
61 return err
62 }
63 // Get the auth login from the control plane directly
64 authURL, err := retrieveLoginURL()
65 if err != nil {
66 return err
67 }
68
69 serverLoginURL, err := url.Parse(authURL)
70 if err != nil {
71 return err
72 }
73 // Append local callback URL
74 q := serverLoginURL.Query()
75 q.Set(oauth.QueryParamCallback, callbackURL.String())
76 q.Set(oauth.QueryParamLongLived, "true")
77 serverLoginURL.RawQuery = q.Encode()
78
79 if forceHeadless || isRemoteSession() {
80 return headlessAuth(serverLoginURL)
81 }
82
83 err = openbrowser(serverLoginURL.String())
84 if err != nil {
85 logger.Debug().Err(err).Msg("falling back to manual login")
86 return headlessAuth(serverLoginURL)
87 }
88
89 a.serverChan = make(chan error)
90
91 // Run server in background
92 http.HandleFunc(callbackURL.Path, a.handleCallback)
93 go func() {
94 logger.Info().Msg("waiting for the authentication to be completed, please check your browser")
95
96 server := &http.Server{ReadHeaderTimeout: time.Second}
97
98 err := server.Serve(listener)
99 if err != nil {
100 log.Fatal(err)
101 }
102 }()
103
104 err = <-a.serverChan
105 if err != nil {
106 return err
107 }
108
109 return nil
110}
111
112func headlessAuth(loginURL *url.URL) error {
113 // Remove cli-callback query parameter to indicate the server to show it inline

Callers 1

newAuthLoginCmdFunction · 0.85

Calls 11

retrieveLoginURLFunction · 0.85
isRemoteSessionFunction · 0.85
headlessAuthFunction · 0.85
openbrowserFunction · 0.85
ParseMethod · 0.80
SetMethod · 0.65
StringMethod · 0.65
InfoMethod · 0.65
QueryMethod · 0.45
DebugMethod · 0.45

Tested by

no test coverage detected