@@ -1150,126 +1150,50 @@ func (h *Handler) RequestIFlowToken(c *gin.Context) {
11501150 c .JSON (http .StatusInternalServerError , gin.H {"status" : "error" , "error" : "failed to start callback server" })
11511151 return
11521152 }
1153+ }
11531154
1154- go func () {
1155+ go func () {
1156+ if isWebUI {
11551157 defer stopCallbackForwarder (iflowauth .CallbackPort )
1156- fmt .Println ("Waiting for authentication..." )
1157-
1158- waitFile := filepath .Join (h .cfg .AuthDir , fmt .Sprintf (".oauth-iflow-%s.oauth" , state ))
1159- deadline := time .Now ().Add (5 * time .Minute )
1160- var resultMap map [string ]string
1161- for {
1162- if time .Now ().After (deadline ) {
1163- oauthStatus [state ] = "Authentication failed"
1164- fmt .Println ("Authentication failed: timeout waiting for callback" )
1165- return
1166- }
1167- if data , errR := os .ReadFile (waitFile ); errR == nil {
1168- _ = os .Remove (waitFile )
1169- _ = json .Unmarshal (data , & resultMap )
1170- break
1171- }
1172- time .Sleep (500 * time .Millisecond )
1173- }
1174-
1175- if errStr := strings .TrimSpace (resultMap ["error" ]); errStr != "" {
1176- oauthStatus [state ] = "Authentication failed"
1177- fmt .Printf ("Authentication failed: %s\n " , errStr )
1178- return
1179- }
1180- if resultState := strings .TrimSpace (resultMap ["state" ]); resultState != state {
1181- oauthStatus [state ] = "Authentication failed"
1182- fmt .Println ("Authentication failed: state mismatch" )
1183- return
1184- }
1185-
1186- code := strings .TrimSpace (resultMap ["code" ])
1187- if code == "" {
1188- oauthStatus [state ] = "Authentication failed"
1189- fmt .Println ("Authentication failed: code missing" )
1190- return
1191- }
1158+ }
1159+ fmt .Println ("Waiting for authentication..." )
11921160
1193- tokenData , errExchange := authSvc .ExchangeCodeForTokens (ctx , code , redirectURI )
1194- if errExchange != nil {
1161+ waitFile := filepath .Join (h .cfg .AuthDir , fmt .Sprintf (".oauth-iflow-%s.oauth" , state ))
1162+ deadline := time .Now ().Add (5 * time .Minute )
1163+ var resultMap map [string ]string
1164+ for {
1165+ if time .Now ().After (deadline ) {
11951166 oauthStatus [state ] = "Authentication failed"
1196- fmt .Printf ("Authentication failed: %v \n " , errExchange )
1167+ fmt .Println ("Authentication failed: timeout waiting for callback" )
11971168 return
11981169 }
1199-
1200- tokenStorage := authSvc .CreateTokenStorage (tokenData )
1201- identifier := strings .TrimSpace (tokenStorage .Email )
1202- if identifier == "" {
1203- identifier = fmt .Sprintf ("iflow-%d" , time .Now ().UnixMilli ())
1204- tokenStorage .Email = identifier
1205- }
1206- record := & coreauth.Auth {
1207- ID : fmt .Sprintf ("iflow-%s.json" , identifier ),
1208- Provider : "iflow" ,
1209- FileName : fmt .Sprintf ("iflow-%s.json" , identifier ),
1210- Storage : tokenStorage ,
1211- Metadata : map [string ]any {"email" : identifier , "api_key" : tokenStorage .APIKey },
1212- Attributes : map [string ]string {"api_key" : tokenStorage .APIKey },
1213- }
1214-
1215- savedPath , errSave := h .saveTokenRecord (ctx , record )
1216- if errSave != nil {
1217- oauthStatus [state ] = "Failed to save authentication tokens"
1218- log .Fatalf ("Failed to save authentication tokens: %v" , errSave )
1219- return
1220- }
1221-
1222- fmt .Printf ("Authentication successful! Token saved to %s\n " , savedPath )
1223- if tokenStorage .APIKey != "" {
1224- fmt .Println ("API key obtained and saved" )
1225- }
1226- fmt .Println ("You can now use iFlow services through this CLI" )
1227- delete (oauthStatus , state )
1228- }()
1229-
1230- oauthStatus [state ] = ""
1231- c .JSON (http .StatusOK , gin.H {"status" : "ok" , "url" : authURL , "state" : state })
1232- return
1233- }
1234-
1235- oauthServer := iflowauth .NewOAuthServer (iflowauth .CallbackPort )
1236- if err := oauthServer .Start (); err != nil {
1237- oauthStatus [state ] = "Failed to start authentication server"
1238- log .Errorf ("Failed to start iFlow OAuth server: %v" , err )
1239- c .JSON (http .StatusInternalServerError , gin.H {"status" : "error" , "error" : "failed to start local oauth server" })
1240- return
1241- }
1242-
1243- go func () {
1244- fmt .Println ("Waiting for authentication..." )
1245- defer func () {
1246- stopCtx , cancel := context .WithTimeout (context .Background (), 2 * time .Second )
1247- defer cancel ()
1248- if err := oauthServer .Stop (stopCtx ); err != nil {
1249- log .Warnf ("Failed to stop iFlow OAuth server: %v" , err )
1170+ if data , errR := os .ReadFile (waitFile ); errR == nil {
1171+ _ = os .Remove (waitFile )
1172+ _ = json .Unmarshal (data , & resultMap )
1173+ break
12501174 }
1251- }()
1175+ time .Sleep (500 * time .Millisecond )
1176+ }
12521177
1253- result , err := oauthServer .WaitForCallback (5 * time .Minute )
1254- if err != nil {
1178+ if errStr := strings .TrimSpace (resultMap ["error" ]); errStr != "" {
12551179 oauthStatus [state ] = "Authentication failed"
1256- fmt .Printf ("Authentication failed: %v \n " , err )
1180+ fmt .Printf ("Authentication failed: %s \n " , errStr )
12571181 return
12581182 }
1259-
1260- if result .Error != "" {
1183+ if resultState := strings .TrimSpace (resultMap ["state" ]); resultState != state {
12611184 oauthStatus [state ] = "Authentication failed"
1262- fmt .Printf ("Authentication failed: %s \n " , result . Error )
1185+ fmt .Println ("Authentication failed: state mismatch" )
12631186 return
12641187 }
12651188
1266- if result .State != state {
1189+ code := strings .TrimSpace (resultMap ["code" ])
1190+ if code == "" {
12671191 oauthStatus [state ] = "Authentication failed"
1268- fmt .Println ("Authentication failed: state mismatch " )
1192+ fmt .Println ("Authentication failed: code missing " )
12691193 return
12701194 }
12711195
1272- tokenData , errExchange := authSvc .ExchangeCodeForTokens (ctx , result . Code , redirectURI )
1196+ tokenData , errExchange := authSvc .ExchangeCodeForTokens (ctx , code , redirectURI )
12731197 if errExchange != nil {
12741198 oauthStatus [state ] = "Authentication failed"
12751199 fmt .Printf ("Authentication failed: %v\n " , errExchange )
0 commit comments