(ctx context.Context, cfg *config.Config, opts *LoginOptions)
| 36 | } |
| 37 | |
| 38 | func (a *ClaudeAuthenticator) 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 | callbackPort := a.CallbackPort |
| 50 | if opts.CallbackPort > 0 { |
| 51 | callbackPort = opts.CallbackPort |
| 52 | } |
| 53 | |
| 54 | pkceCodes, err := claude.GeneratePKCECodes() |
| 55 | if err != nil { |
| 56 | return nil, fmt.Errorf("claude pkce generation failed: %w", err) |
| 57 | } |
| 58 | |
| 59 | state, err := misc.GenerateRandomState() |
| 60 | if err != nil { |
| 61 | return nil, fmt.Errorf("claude state generation failed: %w", err) |
| 62 | } |
| 63 | |
| 64 | oauthServer := claude.NewOAuthServer(callbackPort) |
| 65 | if err = oauthServer.Start(); err != nil { |
| 66 | if strings.Contains(err.Error(), "already in use") { |
| 67 | return nil, claude.NewAuthenticationError(claude.ErrPortInUse, err) |
| 68 | } |
| 69 | return nil, claude.NewAuthenticationError(claude.ErrServerStartFailed, err) |
| 70 | } |
| 71 | defer func() { |
| 72 | stopCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second) |
| 73 | defer cancel() |
| 74 | if stopErr := oauthServer.Stop(stopCtx); stopErr != nil { |
| 75 | log.Warnf("claude oauth server stop error: %v", stopErr) |
| 76 | } |
| 77 | }() |
| 78 | |
| 79 | authSvc := claude.NewClaudeAuth(cfg) |
| 80 | |
| 81 | authURL, returnedState, err := authSvc.GenerateAuthURL(state, pkceCodes) |
| 82 | if err != nil { |
| 83 | return nil, fmt.Errorf("claude authorization url generation failed: %w", err) |
| 84 | } |
| 85 | state = returnedState |
| 86 | |
| 87 | if !opts.NoBrowser { |
| 88 | fmt.Println("Opening browser for Claude authentication") |
| 89 | if !browser.IsAvailable() { |
| 90 | log.Warn("No browser available; please open the URL manually") |
| 91 | util.PrintSSHTunnelInstructions(callbackPort) |
| 92 | fmt.Printf("Visit the following URL to continue authentication:\n%s\n", authURL) |
| 93 | } else if err = browser.OpenURL(authURL); err != nil { |
| 94 | log.Warnf("Failed to open browser automatically: %v", err) |
| 95 | util.PrintSSHTunnelInstructions(callbackPort) |
nothing calls this directly
no test coverage detected