feat: Implement browser-based OAuth authentication using chromedp and rod, and add a team processing API.

This commit is contained in:
2026-02-02 05:05:07 +08:00
parent ee5b69af13
commit 4cd9f2b2b7
3 changed files with 66 additions and 122 deletions

View File

@@ -208,67 +208,63 @@ func (r *RodAuth) CompleteOAuthLogged(authURL, email, password, teamID string, l
ThisObj: nil,
})
// 增加超时时间到 90 秒
page = page.Timeout(90 * time.Second)
// 设置合理的超时时间 60 秒
page = page.Timeout(60 * time.Second)
logStep(StepNavigate, "正在访问授权页面...")
if err := page.Navigate(authURL); err != nil {
logError(StepNavigate, "访问失败: %v", err)
logError(StepNavigate, "访问授权页失败: %v", err)
return "", fmt.Errorf("访问授权URL失败: %v", err)
}
page.MustWaitDOMStable()
if code := r.checkForCode(page); code != "" {
logStep(StepExtractCode, "已捕获授权码回调")
logStep(StepComplete, "授权成功(快速通道)")
return code, nil
}
logStep(StepInputEmail, "正在查找邮箱输入框...")
emailInput, err := page.Timeout(5 * time.Second).Element("input[name='email'], input[type='email'], input[name='username']")
// 使用10秒超时查找邮箱输入框
emailInput, err := page.Timeout(10 * time.Second).Element("input[name='email'], input[type='email'], input[name='username'], input[id='email'], input[autocomplete='email']")
if err != nil {
logError(StepInputEmail, "未找到邮箱输入框")
return "", fmt.Errorf("未找到邮箱输入框")
}
emailInput.MustSelectAllText().MustInput(email)
logStep(StepInputEmail, "已输入邮箱")
time.Sleep(200 * time.Millisecond)
logStep(StepSubmitEmail, "正在提交邮箱...")
if btn, _ := page.Timeout(2 * time.Second).Element("button[type='submit']"); btn != nil {
if btn, _ := page.Timeout(2 * time.Second).Element("button[type='submit'], button[name='action']"); btn != nil {
btn.MustClick()
}
time.Sleep(1500 * time.Millisecond)
if code := r.checkForCode(page); code != "" {
logStep(StepExtractCode, "已获取授权码")
logStep(StepComplete, "授权成功")
return code, nil
}
logStep(StepInputPassword, "正在查找密码输入框...")
passwordInput, err := page.Timeout(8 * time.Second).Element("input[type='password']")
// 使用10秒超时查找密码输入框
passwordInput, err := page.Timeout(10 * time.Second).Element("input[type='password'], input[name='password'], input[id='password']")
if err != nil {
logError(StepInputPassword, "未找到密码输入框")
return "", fmt.Errorf("未找到密码输入框")
}
passwordInput.MustSelectAllText().MustInput(password)
logStep(StepInputPassword, "已输入密码")
time.Sleep(200 * time.Millisecond)
logStep(StepSubmitPassword, "正在提交密码...")
if btn, _ := page.Timeout(2 * time.Second).Element("button[type='submit']"); btn != nil {
logStep(StepSubmitPassword, "正在登录...")
if btn, _ := page.Timeout(2 * time.Second).Element("button[type='submit'], button[name='action']"); btn != nil {
btn.MustClick()
}
logStep(StepWaitCallback, "等待授权回调...")
for i := 0; i < 66; i++ {
time.Sleep(300 * time.Millisecond)
// 等待授权回调最多20秒
for i := 0; i < 40; i++ {
time.Sleep(500 * time.Millisecond)
if code := r.checkForCode(page); code != "" {
logStep(StepComplete, "授权成")
logStep(StepComplete, "授权成")
return code, nil
}
@@ -276,14 +272,14 @@ func (r *RodAuth) CompleteOAuthLogged(authURL, email, password, teamID string, l
currentURL := info.URL
if strings.Contains(currentURL, "consent") {
logStep(StepConsent, "正在处理授权同意页面...")
logStep(StepConsent, "处理授权同意...")
if btn, _ := page.Timeout(500 * time.Millisecond).Element("button[type='submit']"); btn != nil {
btn.Click(proto.InputMouseButtonLeft, 1)
}
}
if strings.Contains(currentURL, "authorize") && teamID != "" {
logStep(StepSelectWorkspace, "正在选择工作区: %s", teamID)
logStep(StepSelectWorkspace, "选择工作区...")
wsSelector := fmt.Sprintf("[data-workspace-id='%s'], [data-account-id='%s']", teamID, teamID)
if wsBtn, _ := page.Timeout(500 * time.Millisecond).Element(wsSelector); wsBtn != nil {
wsBtn.Click(proto.InputMouseButtonLeft, 1)