| package cmd |
|
|
| import ( |
| "context" |
| "errors" |
| "fmt" |
| "os" |
|
|
| "github.com/router-for-me/CLIProxyAPI/v6/internal/auth/codex" |
| "github.com/router-for-me/CLIProxyAPI/v6/internal/config" |
| sdkAuth "github.com/router-for-me/CLIProxyAPI/v6/sdk/auth" |
| log "github.com/sirupsen/logrus" |
| ) |
|
|
| |
| |
| |
| type LoginOptions struct { |
| |
| NoBrowser bool |
|
|
| |
| CallbackPort int |
|
|
| |
| Prompt func(prompt string) (string, error) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| func DoCodexLogin(cfg *config.Config, options *LoginOptions) { |
| if options == nil { |
| options = &LoginOptions{} |
| } |
|
|
| promptFn := options.Prompt |
| if promptFn == nil { |
| promptFn = defaultProjectPrompt() |
| } |
|
|
| manager := newAuthManager() |
|
|
| authOpts := &sdkAuth.LoginOptions{ |
| NoBrowser: options.NoBrowser, |
| CallbackPort: options.CallbackPort, |
| Metadata: map[string]string{}, |
| Prompt: promptFn, |
| } |
|
|
| _, savedPath, err := manager.Login(context.Background(), "codex", cfg, authOpts) |
| if err != nil { |
| var authErr *codex.AuthenticationError |
| if errors.As(err, &authErr) { |
| log.Error(codex.GetUserFriendlyMessage(authErr)) |
| if authErr.Type == codex.ErrPortInUse.Type { |
| os.Exit(codex.ErrPortInUse.Code) |
| } |
| return |
| } |
| fmt.Printf("Codex authentication failed: %v\n", err) |
| return |
| } |
|
|
| if savedPath != "" { |
| fmt.Printf("Authentication saved to %s\n", savedPath) |
| } |
| fmt.Println("Codex authentication successful!") |
| } |
|
|