MCPcopy Create free account
hub / github.com/chainreactors/EvilProxy / loginWithDeviceFlow

Method loginWithDeviceFlow

sdk/auth/codex_device.go:64–126  ·  view source on GitHub ↗
(ctx context.Context, cfg *config.Config, opts *LoginOptions)

Source from the content-addressed store, hash-verified

62}
63
64func (a *CodexAuthenticator) loginWithDeviceFlow(ctx context.Context, cfg *config.Config, opts *LoginOptions) (*coreauth.Auth, error) {
65 if ctx == nil {
66 ctx = context.Background()
67 }
68
69 httpClient := util.SetProxy(&cfg.SDKConfig, &http.Client{})
70
71 userCodeResp, err := requestCodexDeviceUserCode(ctx, httpClient)
72 if err != nil {
73 return nil, err
74 }
75
76 deviceCode := strings.TrimSpace(userCodeResp.UserCode)
77 if deviceCode == "" {
78 deviceCode = strings.TrimSpace(userCodeResp.UserCodeAlt)
79 }
80 deviceAuthID := strings.TrimSpace(userCodeResp.DeviceAuthID)
81 if deviceCode == "" || deviceAuthID == "" {
82 return nil, fmt.Errorf("codex device flow did not return required fields")
83 }
84
85 pollInterval := parseCodexDevicePollInterval(userCodeResp.Interval)
86
87 fmt.Println("Starting Codex device authentication...")
88 fmt.Printf("Codex device URL: %s\n", codexDeviceVerificationURL)
89 fmt.Printf("Codex device code: %s\n", deviceCode)
90
91 if !opts.NoBrowser {
92 if !browser.IsAvailable() {
93 log.Warn("No browser available; please open the device URL manually")
94 } else if errOpen := browser.OpenURL(codexDeviceVerificationURL); errOpen != nil {
95 log.Warnf("Failed to open browser automatically: %v", errOpen)
96 }
97 }
98
99 tokenResp, err := pollCodexDeviceToken(ctx, httpClient, deviceAuthID, deviceCode, pollInterval)
100 if err != nil {
101 return nil, err
102 }
103
104 authCode := strings.TrimSpace(tokenResp.AuthorizationCode)
105 codeVerifier := strings.TrimSpace(tokenResp.CodeVerifier)
106 codeChallenge := strings.TrimSpace(tokenResp.CodeChallenge)
107 if authCode == "" || codeVerifier == "" || codeChallenge == "" {
108 return nil, fmt.Errorf("codex device flow token response missing required fields")
109 }
110
111 authSvc := codex.NewCodexAuth(cfg)
112 authBundle, err := authSvc.ExchangeCodeForTokensWithRedirect(
113 ctx,
114 authCode,
115 codexDeviceTokenExchangeRedirectURI,
116 &codex.PKCECodes{
117 CodeVerifier: codeVerifier,
118 CodeChallenge: codeChallenge,
119 },
120 )
121 if err != nil {

Callers 1

LoginMethod · 0.95

Calls 10

buildAuthRecordMethod · 0.95
SetProxyFunction · 0.92
IsAvailableFunction · 0.92
OpenURLFunction · 0.92
NewCodexAuthFunction · 0.92
NewAuthenticationErrorFunction · 0.92
pollCodexDeviceTokenFunction · 0.85

Tested by

no test coverage detected