mirror of
https://github.com/router-for-me/CLIProxyAPIPlus.git
synced 2026-03-21 00:22:35 +00:00
55 lines
1.4 KiB
Go
55 lines
1.4 KiB
Go
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"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"
|
|
)
|
|
|
|
// LoginOptions contains options for the login processes.
|
|
type LoginOptions struct {
|
|
// NoBrowser indicates whether to skip opening the browser automatically.
|
|
NoBrowser bool
|
|
// Prompt allows the caller to provide interactive input when needed.
|
|
Prompt func(prompt string) (string, error)
|
|
}
|
|
|
|
// DoCodexLogin triggers the Codex OAuth flow through the shared authentication manager.
|
|
func DoCodexLogin(cfg *config.Config, options *LoginOptions) {
|
|
if options == nil {
|
|
options = &LoginOptions{}
|
|
}
|
|
|
|
manager := newAuthManager()
|
|
|
|
authOpts := &sdkAuth.LoginOptions{
|
|
NoBrowser: options.NoBrowser,
|
|
Metadata: map[string]string{},
|
|
Prompt: options.Prompt,
|
|
}
|
|
|
|
_, 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
|
|
}
|
|
log.Fatalf("Codex authentication failed: %v", err)
|
|
return
|
|
}
|
|
|
|
if savedPath != "" {
|
|
log.Infof("Authentication saved to %s", savedPath)
|
|
}
|
|
log.Info("Codex authentication successful!")
|
|
}
|