@@ -26,7 +26,7 @@ import (
2626 "golang.org/x/term"
2727)
2828
29- func NewLoginCommand () * cobra.Command {
29+ func NewLoginCommand (globalClientOpts * connectors. ClientOptions ) * cobra.Command {
3030 var (
3131 ctxName string
3232 username string
@@ -82,6 +82,13 @@ microcks login http://localhost:8080 --sso --sso-launch-browser=false
8282 var authToken = ""
8383 var refreshToken = ""
8484
85+ //Initialze Auth struct
86+ authCfg := config.Auth {
87+ Server : server ,
88+ ClientId : "" ,
89+ ClientSecret : "" ,
90+ }
91+
8592 configFile , err := config .DefaultLocalConfigPath ()
8693 errors .CheckError (err )
8794 localConfig , err := config .ReadLocalConfig (configFile )
@@ -110,13 +117,16 @@ microcks login http://localhost:8080 --sso --sso-launch-browser=false
110117 }
111118 //Perform login and retrive tokens
112119 authToken , refreshToken = passwordLogin (keycloakUrl , clientID , clientSecret , username , password )
120+ authCfg .ClientId = clientID
121+ authCfg .ClientSecret = clientSecret
113122 } else {
114123 httpClient := mc .HttpClient ()
115124 ctx = oidc .ClientContext (ctx , httpClient )
116125 kc := connectors .NewKeycloakClient (keycloakUrl , "" , "" )
117126 oauth2conf , err := kc .GetOIDCConfig ()
118127 errors .CheckError (err )
119128 authToken , refreshToken = oauth2login (ctx , ssoProt , oauth2conf , ssoLaunchBrowser )
129+ authCfg .ClientId = "microcks-app-js"
120130 }
121131
122132 parser := jwt .NewParser (jwt .WithoutClaimsValidation ())
@@ -137,6 +147,8 @@ microcks login http://localhost:8080 --sso --sso-launch-browser=false
137147 })
138148 }
139149
150+ localConfig .UpserAuth (authCfg )
151+
140152 localConfig .UpsertUser (config.User {
141153 Name : server ,
142154 AuthToken : authToken ,
@@ -203,6 +215,8 @@ func oauth2login(
203215 // Authorization redirect callback from OAuth2 auth flow.
204216 // Handles both implicit and authorization code flow
205217 callbackHandler := func (w http.ResponseWriter , r * http.Request ) {
218+ log .Printf ("Callback: %s\n " , r .URL )
219+
206220 if formErr := r .FormValue ("error" ); formErr != "" {
207221 handleErr (w , fmt .Sprintf ("%s: %s" , formErr , r .FormValue ("error_description" )))
208222 return
@@ -262,6 +276,7 @@ func oauth2login(
262276 time .Sleep (1 * time .Second )
263277 ssoAuthFlow (url , ssoLaunchBrowser )
264278 go func () {
279+ log .Printf ("Listen: %s\n " , srv .Addr )
265280 if err := srv .ListenAndServe (); err != http .ErrServerClosed {
266281 log .Fatalf ("Temporary HTTP server failed: %s" , err )
267282 }
@@ -270,21 +285,22 @@ func oauth2login(
270285 if errMsg != "" {
271286 log .Fatal (errMsg )
272287 }
273-
288+ fmt . Printf ( "Authentication successful \n " )
274289 ctx , cancel := context .WithTimeout (ctx , 1 * time .Second )
275290 defer cancel ()
276291 _ = srv .Shutdown (ctx )
277-
292+ log .Printf ("Token: %s\n " , tokenString )
293+ log .Printf ("Refresh Token: %s\n " , refreshToken )
278294 return tokenString , refreshToken
279295}
280296
281297func ssoAuthFlow (url string , ssoLaunchBrowser bool ) {
282298 if ssoLaunchBrowser {
283- log .Printf ("Opening system default browser for authentication\n " )
299+ fmt .Printf ("Opening system default browser for authentication\n " )
284300 err := open .Start (url )
285301 errors .CheckError (err )
286302 } else {
287- log .Printf ("To authenticate, copy-and-paste the following URL into your preferred browser: %s\n " , url )
303+ fmt .Printf ("To authenticate, copy-and-paste the following URL into your preferred browser: %s\n " , url )
288304 }
289305}
290306
0 commit comments