(ctx context.Context, cfg *config.Config, opts *LoginOptions)
| 36 | } |
| 37 | |
| 38 | func (a *CodexAuthenticator) Login(ctx context.Context, cfg *config.Config, opts *LoginOptions) (*coreauth.Auth, error) { |
| 39 | if cfg == nil { |
| 40 | return nil, fmt.Errorf("cliproxy auth: configuration is required") |
| 41 | } |
| 42 | if ctx == nil { |
| 43 | ctx = context.Background() |
| 44 | } |
| 45 | if opts == nil { |
| 46 | opts = &LoginOptions{} |
| 47 | } |
| 48 | |
| 49 | if shouldUseCodexDeviceFlow(opts) { |
| 50 | return a.loginWithDeviceFlow(ctx, cfg, opts) |
| 51 | } |
| 52 | |
| 53 | callbackPort := a.CallbackPort |
| 54 | if opts.CallbackPort > 0 { |
| 55 | callbackPort = opts.CallbackPort |
| 56 | } |
| 57 | |
| 58 | pkceCodes, err := codex.GeneratePKCECodes() |
| 59 | if err != nil { |
| 60 | return nil, fmt.Errorf("codex pkce generation failed: %w", err) |
| 61 | } |
| 62 | |
| 63 | state, err := misc.GenerateRandomState() |
| 64 | if err != nil { |
| 65 | return nil, fmt.Errorf("codex state generation failed: %w", err) |
| 66 | } |
| 67 | |
| 68 | oauthServer := codex.NewOAuthServer(callbackPort) |
| 69 | if err = oauthServer.Start(); err != nil { |
| 70 | if strings.Contains(err.Error(), "already in use") { |
| 71 | return nil, codex.NewAuthenticationError(codex.ErrPortInUse, err) |
| 72 | } |
| 73 | return nil, codex.NewAuthenticationError(codex.ErrServerStartFailed, err) |
| 74 | } |
| 75 | defer func() { |
| 76 | stopCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second) |
| 77 | defer cancel() |
| 78 | if stopErr := oauthServer.Stop(stopCtx); stopErr != nil { |
| 79 | log.Warnf("codex oauth server stop error: %v", stopErr) |
| 80 | } |
| 81 | }() |
| 82 | |
| 83 | authSvc := codex.NewCodexAuth(cfg) |
| 84 | |
| 85 | authURL, err := authSvc.GenerateAuthURL(state, pkceCodes) |
| 86 | if err != nil { |
| 87 | return nil, fmt.Errorf("codex authorization url generation failed: %w", err) |
| 88 | } |
| 89 | |
| 90 | if !opts.NoBrowser { |
| 91 | fmt.Println("Opening browser for Codex authentication") |
| 92 | if !browser.IsAvailable() { |
| 93 | log.Warn("No browser available; please open the URL manually") |
| 94 | util.PrintSSHTunnelInstructions(callbackPort) |
| 95 | fmt.Printf("Visit the following URL to continue authentication:\n%s\n", authURL) |
nothing calls this directly
no test coverage detected