Login initiates the Kimi device flow authentication.
(ctx context.Context, cfg *config.Config, opts *LoginOptions)
| 37 | |
| 38 | // Login initiates the Kimi device flow authentication. |
| 39 | func (a KimiAuthenticator) Login(ctx context.Context, cfg *config.Config, opts *LoginOptions) (*coreauth.Auth, error) { |
| 40 | if cfg == nil { |
| 41 | return nil, fmt.Errorf("cliproxy auth: configuration is required") |
| 42 | } |
| 43 | if opts == nil { |
| 44 | opts = &LoginOptions{} |
| 45 | } |
| 46 | |
| 47 | authSvc := kimi.NewKimiAuth(cfg) |
| 48 | |
| 49 | // Start the device flow |
| 50 | fmt.Println("Starting Kimi authentication...") |
| 51 | deviceCode, err := authSvc.StartDeviceFlow(ctx) |
| 52 | if err != nil { |
| 53 | return nil, fmt.Errorf("kimi: failed to start device flow: %w", err) |
| 54 | } |
| 55 | |
| 56 | // Display the verification URL |
| 57 | verificationURL := deviceCode.VerificationURIComplete |
| 58 | if verificationURL == "" { |
| 59 | verificationURL = deviceCode.VerificationURI |
| 60 | } |
| 61 | |
| 62 | fmt.Printf("\nTo authenticate, please visit:\n%s\n\n", verificationURL) |
| 63 | if deviceCode.UserCode != "" { |
| 64 | fmt.Printf("User code: %s\n\n", deviceCode.UserCode) |
| 65 | } |
| 66 | |
| 67 | // Try to open the browser automatically |
| 68 | if !opts.NoBrowser { |
| 69 | if browser.IsAvailable() { |
| 70 | if errOpen := browser.OpenURL(verificationURL); errOpen != nil { |
| 71 | log.Warnf("Failed to open browser automatically: %v", errOpen) |
| 72 | } else { |
| 73 | fmt.Println("Browser opened automatically.") |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | fmt.Println("Waiting for authorization...") |
| 79 | if deviceCode.ExpiresIn > 0 { |
| 80 | fmt.Printf("(This will timeout in %d seconds if not authorized)\n", deviceCode.ExpiresIn) |
| 81 | } |
| 82 | |
| 83 | // Wait for user authorization |
| 84 | authBundle, err := authSvc.WaitForAuthorization(ctx, deviceCode) |
| 85 | if err != nil { |
| 86 | return nil, fmt.Errorf("kimi: %w", err) |
| 87 | } |
| 88 | |
| 89 | // Create the token storage |
| 90 | tokenStorage := authSvc.CreateTokenStorage(authBundle) |
| 91 | |
| 92 | // Build metadata with token information |
| 93 | metadata := map[string]any{ |
| 94 | "type": "kimi", |
| 95 | "access_token": authBundle.TokenData.AccessToken, |
| 96 | "refresh_token": authBundle.TokenData.RefreshToken, |
nothing calls this directly
no test coverage detected