mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-18 04:10:51 +08:00
fix(sdk/auth): prevent OAuth manual prompt goroutine leak,Use timer-based manual prompt per provider and remove oauth_callback helper.
This commit is contained in:
@@ -99,7 +99,6 @@ func (a *CodexAuthenticator) Login(ctx context.Context, cfg *config.Config, opts
|
||||
|
||||
callbackCh := make(chan *codex.OAuthResult, 1)
|
||||
callbackErrCh := make(chan error, 1)
|
||||
manualCh, manualErrCh := promptForOAuthCallback(opts.Prompt, "Codex")
|
||||
manualDescription := ""
|
||||
|
||||
go func() {
|
||||
@@ -112,22 +111,58 @@ func (a *CodexAuthenticator) Login(ctx context.Context, cfg *config.Config, opts
|
||||
}()
|
||||
|
||||
var result *codex.OAuthResult
|
||||
select {
|
||||
case result = <-callbackCh:
|
||||
case err = <-callbackErrCh:
|
||||
if strings.Contains(err.Error(), "timeout") {
|
||||
return nil, codex.NewAuthenticationError(codex.ErrCallbackTimeout, err)
|
||||
var manualPromptTimer *time.Timer
|
||||
var manualPromptC <-chan time.Time
|
||||
if opts.Prompt != nil {
|
||||
manualPromptTimer = time.NewTimer(15 * time.Second)
|
||||
manualPromptC = manualPromptTimer.C
|
||||
defer manualPromptTimer.Stop()
|
||||
}
|
||||
|
||||
waitForCallback:
|
||||
for {
|
||||
select {
|
||||
case result = <-callbackCh:
|
||||
break waitForCallback
|
||||
case err = <-callbackErrCh:
|
||||
if strings.Contains(err.Error(), "timeout") {
|
||||
return nil, codex.NewAuthenticationError(codex.ErrCallbackTimeout, err)
|
||||
}
|
||||
return nil, err
|
||||
case <-manualPromptC:
|
||||
manualPromptC = nil
|
||||
if manualPromptTimer != nil {
|
||||
manualPromptTimer.Stop()
|
||||
}
|
||||
select {
|
||||
case result = <-callbackCh:
|
||||
break waitForCallback
|
||||
case err = <-callbackErrCh:
|
||||
if strings.Contains(err.Error(), "timeout") {
|
||||
return nil, codex.NewAuthenticationError(codex.ErrCallbackTimeout, err)
|
||||
}
|
||||
return nil, err
|
||||
default:
|
||||
}
|
||||
input, errPrompt := opts.Prompt("Paste the Codex callback URL (or press Enter to keep waiting): ")
|
||||
if errPrompt != nil {
|
||||
return nil, errPrompt
|
||||
}
|
||||
parsed, errParse := misc.ParseOAuthCallback(input)
|
||||
if errParse != nil {
|
||||
return nil, errParse
|
||||
}
|
||||
if parsed == nil {
|
||||
continue
|
||||
}
|
||||
manualDescription = parsed.ErrorDescription
|
||||
result = &codex.OAuthResult{
|
||||
Code: parsed.Code,
|
||||
State: parsed.State,
|
||||
Error: parsed.Error,
|
||||
}
|
||||
break waitForCallback
|
||||
}
|
||||
return nil, err
|
||||
case manual := <-manualCh:
|
||||
manualDescription = manual.ErrorDescription
|
||||
result = &codex.OAuthResult{
|
||||
Code: manual.Code,
|
||||
State: manual.State,
|
||||
Error: manual.Error,
|
||||
}
|
||||
case err = <-manualErrCh:
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if result.Error != "" {
|
||||
|
||||
Reference in New Issue
Block a user