feat:0666账号密码自动登录刷新Token
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
@@ -66,6 +67,12 @@ type RockClient struct {
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
// ErrRockUnauthorized 表示上游拒绝了当前 Bearer Token(401)。
|
||||
var ErrRockUnauthorized = errors.New("上游 Bearer Token 已失效或未授权")
|
||||
|
||||
// 上游登录接口会校验浏览器态请求头,Go 默认 UA 会被判为异常请求。
|
||||
const rockBrowserUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/155.0.0.0 Safari/537.36"
|
||||
|
||||
func NewRockClient(baseURL string) *RockClient {
|
||||
c := &RockClient{
|
||||
httpClient: &http.Client{
|
||||
@@ -413,7 +420,7 @@ func (c *RockClient) ListPackages(ctx context.Context, bearerToken string, page,
|
||||
return nil, fmt.Errorf("读取 packages 列表响应失败: %w", err)
|
||||
}
|
||||
if resp.StatusCode == http.StatusUnauthorized {
|
||||
return nil, fmt.Errorf("上游 Bearer Token 已失效或未授权 (401)")
|
||||
return nil, fmt.Errorf("%w (401)", ErrRockUnauthorized)
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("上游 packages 列表接口返回错误码 %d: %s", resp.StatusCode, string(body))
|
||||
@@ -515,6 +522,105 @@ func (c *RockClient) GetPackageDetail(ctx context.Context, shareCode string) (*R
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
// RockLoginResponse 是 POST /api/auth/login 的返回结构。
|
||||
type RockLoginResponse struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Error string `json:"error,omitempty"`
|
||||
Token string `json:"token"`
|
||||
Role string `json:"role"`
|
||||
Username string `json:"username"`
|
||||
User struct {
|
||||
Username string `json:"username"`
|
||||
Role string `json:"role"`
|
||||
} `json:"user"`
|
||||
}
|
||||
|
||||
func (r *RockLoginResponse) ResolveUsername() string {
|
||||
if r == nil {
|
||||
return ""
|
||||
}
|
||||
if value := strings.TrimSpace(r.Username); value != "" {
|
||||
return value
|
||||
}
|
||||
return strings.TrimSpace(r.User.Username)
|
||||
}
|
||||
|
||||
func (r *RockLoginResponse) ResolveRole() string {
|
||||
if r == nil {
|
||||
return ""
|
||||
}
|
||||
if value := strings.TrimSpace(r.Role); value != "" {
|
||||
return value
|
||||
}
|
||||
return strings.TrimSpace(r.User.Role)
|
||||
}
|
||||
|
||||
// Login 使用账号密码登录上游,返回新的 Bearer Token。
|
||||
// 上游登录接口会校验浏览器态请求头(Origin/Referer/Sec-Fetch),这里显式补齐;
|
||||
// 请求体包含密码,不能交给 traceRockRequest 记录。
|
||||
func (c *RockClient) Login(ctx context.Context, username, password string) (*RockLoginResponse, error) {
|
||||
reqURL := fmt.Sprintf("%s/api/auth/login", c.BaseURL())
|
||||
payload, err := json.Marshal(map[string]interface{}{
|
||||
"username": strings.TrimSpace(username),
|
||||
"password": password,
|
||||
"publicShape": true,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqURL, bytes.NewReader(payload))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
base := c.BaseURL()
|
||||
req.Header.Set("accept", "*/*")
|
||||
req.Header.Set("accept-language", "zh-CN,zh;q=0.9")
|
||||
req.Header.Set("content-type", "application/json")
|
||||
req.Header.Set("origin", base)
|
||||
req.Header.Set("referer", base+"/")
|
||||
req.Header.Set("priority", "u=1, i")
|
||||
req.Header.Set("sec-ch-ua", `"Google Chrome";v="155", "Chromium";v="155", "Not(A:Brand";v="24"`)
|
||||
req.Header.Set("sec-ch-ua-mobile", "?0")
|
||||
req.Header.Set("sec-ch-ua-platform", `"Windows"`)
|
||||
req.Header.Set("sec-fetch-dest", "empty")
|
||||
req.Header.Set("sec-fetch-mode", "cors")
|
||||
req.Header.Set("sec-fetch-site", "same-origin")
|
||||
req.Header.Set("user-agent", rockBrowserUserAgent)
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("请求 0666 登录接口失败: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("读取登录响应失败: %w", err)
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("上游登录接口返回错误状态码 %d: %s", resp.StatusCode, string(body))
|
||||
}
|
||||
var res RockLoginResponse
|
||||
if err := json.Unmarshal(body, &res); err != nil {
|
||||
return nil, fmt.Errorf("解析登录响应失败: %w", err)
|
||||
}
|
||||
if res.Code != 0 {
|
||||
message := strings.TrimSpace(res.Message)
|
||||
if message == "" {
|
||||
message = strings.TrimSpace(res.Error)
|
||||
}
|
||||
if message == "" {
|
||||
message = "上游登录失败"
|
||||
}
|
||||
return nil, errors.New(message)
|
||||
}
|
||||
if strings.TrimSpace(res.Token) == "" {
|
||||
return nil, errors.New("上游登录成功但未返回 token")
|
||||
}
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
// GetMe 获取商户个人信息及积分
|
||||
func (c *RockClient) GetMe(ctx context.Context, bearerToken string) (*RockMeResponse, error) {
|
||||
startedAt := time.Now()
|
||||
@@ -539,7 +645,7 @@ func (c *RockClient) GetMe(ctx context.Context, bearerToken string) (*RockMeResp
|
||||
traceRockRequest(ctx, "me", http.MethodGet, reqURL, nil, resp.StatusCode, body, time.Since(startedAt), err)
|
||||
|
||||
if resp.StatusCode == http.StatusUnauthorized {
|
||||
return nil, fmt.Errorf("上游 Bearer Token 已失效或未授权 (401)")
|
||||
return nil, fmt.Errorf("%w (401)", ErrRockUnauthorized)
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("上游返回错误状态码 %d: %s", resp.StatusCode, string(body))
|
||||
@@ -618,7 +724,7 @@ func (c *RockClient) GetDashboard(ctx context.Context, bearerToken string) (*Roc
|
||||
}
|
||||
traceRockRequest(ctx, "dashboard", http.MethodGet, reqURL, nil, resp.StatusCode, body, time.Since(startedAt), err)
|
||||
if resp.StatusCode == http.StatusUnauthorized {
|
||||
return nil, fmt.Errorf("上游 Bearer Token 已失效或未授权 (401)")
|
||||
return nil, fmt.Errorf("%w (401)", ErrRockUnauthorized)
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("上游 dashboard 接口返回错误码 %d: %s", resp.StatusCode, string(body))
|
||||
@@ -668,6 +774,9 @@ func (c *RockClient) ClaimTokens(ctx context.Context, bearerToken string, color
|
||||
}
|
||||
traceRockRequest(ctx, "claim", http.MethodPost, reqURL, jsonBytes, resp.StatusCode, body, time.Since(startedAt), err)
|
||||
|
||||
if resp.StatusCode == http.StatusUnauthorized {
|
||||
return nil, fmt.Errorf("%w (401)", ErrRockUnauthorized)
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("上游 claim 接口返回错误码 %d: %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user