chore: 并入 iotlcm-upstream-chan 历史到 upstream-chan/(原提交 1e74497)
This commit is contained in:
@@ -0,0 +1,145 @@
|
||||
package upstreamimpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
sharedtoken "git.5iot.com/iotlcm-public/iotlcm-upstream-chan/internal/token"
|
||||
)
|
||||
|
||||
const ziboOneLinkTokenPath = "/ec/get/token"
|
||||
|
||||
// ZiboTokenConfig contains the official OneLink token endpoint and the
|
||||
// per-channel credentials stored in t_sim_channels.
|
||||
type ZiboTokenConfig struct {
|
||||
BaseURL string
|
||||
Path string
|
||||
AppID string
|
||||
Password string
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
// ZiboTokenProvider fetches a token from the official OneLink endpoint.
|
||||
type ZiboTokenProvider struct {
|
||||
baseURL string
|
||||
path string
|
||||
appID string
|
||||
password string
|
||||
client *http.Client
|
||||
sequence atomic.Uint64
|
||||
}
|
||||
|
||||
type ziboTokenResponse struct {
|
||||
Status string `json:"status"`
|
||||
Message string `json:"message"`
|
||||
Result []struct {
|
||||
Token string `json:"token"`
|
||||
TTL int `json:"ttl"`
|
||||
} `json:"result"`
|
||||
}
|
||||
|
||||
type ziboFetcher struct {
|
||||
provider *ZiboTokenProvider
|
||||
}
|
||||
|
||||
func NewZiboTokenProvider(config ZiboTokenConfig) *ZiboTokenProvider {
|
||||
timeout := config.Timeout
|
||||
if timeout <= 0 {
|
||||
timeout = defaultUpstreamHTTPTimeout
|
||||
}
|
||||
return &ZiboTokenProvider{
|
||||
baseURL: strings.TrimRight(strings.TrimSpace(config.BaseURL), "/"),
|
||||
path: normalizeProviderPath(config.Path, ziboOneLinkTokenPath),
|
||||
appID: strings.TrimSpace(config.AppID),
|
||||
password: config.Password,
|
||||
client: &http.Client{Timeout: timeout},
|
||||
}
|
||||
}
|
||||
|
||||
func (f ziboFetcher) FetchToken(ctx context.Context, _ string) (sharedtoken.OneLinkToken, error) {
|
||||
token, err := f.provider.Fetch(ctx)
|
||||
if err != nil {
|
||||
return sharedtoken.OneLinkToken{}, err
|
||||
}
|
||||
return sharedtoken.OneLinkToken{
|
||||
Value: token.Value,
|
||||
CreatedAt: token.CreatedAt,
|
||||
ExpiresAt: token.ExpiresAt,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (p *ZiboTokenProvider) Fetch(ctx context.Context) (OneLinkToken, error) {
|
||||
if p == nil || p.client == nil {
|
||||
return OneLinkToken{}, fmt.Errorf("zibo token provider is not initialized")
|
||||
}
|
||||
if p.baseURL == "" {
|
||||
return OneLinkToken{}, fmt.Errorf("zibo token base URL is required")
|
||||
}
|
||||
if p.appID == "" || strings.TrimSpace(p.password) == "" {
|
||||
return OneLinkToken{}, fmt.Errorf("zibo appid and password are required")
|
||||
}
|
||||
|
||||
endpoint, err := url.Parse(joinProviderURL(p.baseURL, p.path))
|
||||
if err != nil {
|
||||
return OneLinkToken{}, fmt.Errorf("parse zibo token URL: %w", err)
|
||||
}
|
||||
query := endpoint.Query()
|
||||
query.Set("appid", p.appID)
|
||||
query.Set("password", p.password)
|
||||
query.Set("transid", p.nextTransID())
|
||||
query.Set("refresh", "0")
|
||||
endpoint.RawQuery = query.Encode()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), nil)
|
||||
if err != nil {
|
||||
return OneLinkToken{}, fmt.Errorf("create zibo token request: %w", err)
|
||||
}
|
||||
req.Header.Set("Accept", "application/json")
|
||||
|
||||
resp, err := p.client.Do(req)
|
||||
if err != nil {
|
||||
return OneLinkToken{}, fmt.Errorf("request zibo token: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return OneLinkToken{}, fmt.Errorf("read zibo token response: %w", err)
|
||||
}
|
||||
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
|
||||
return OneLinkToken{}, fmt.Errorf("zibo token HTTP status %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
var output ziboTokenResponse
|
||||
if err := json.Unmarshal(body, &output); err != nil {
|
||||
return OneLinkToken{}, fmt.Errorf("decode zibo token response: %w", err)
|
||||
}
|
||||
if strings.TrimSpace(output.Status) != "0" {
|
||||
return OneLinkToken{}, fmt.Errorf("zibo token failed: status=%s, message=%s", output.Status, output.Message)
|
||||
}
|
||||
if len(output.Result) == 0 || strings.TrimSpace(output.Result[0].Token) == "" {
|
||||
return OneLinkToken{}, fmt.Errorf("zibo token response is empty")
|
||||
}
|
||||
if output.Result[0].TTL <= 0 {
|
||||
return OneLinkToken{}, fmt.Errorf("zibo token ttl must be positive")
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
return OneLinkToken{
|
||||
Value: strings.TrimSpace(output.Result[0].Token),
|
||||
CreatedAt: now,
|
||||
ExpiresAt: now.Add(time.Duration(output.Result[0].TTL) * time.Second),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (p *ZiboTokenProvider) nextTransID() string {
|
||||
return p.appID + time.Now().Format("20060102150405") +
|
||||
fmt.Sprintf("%08d", p.sequence.Add(1)%100000000)
|
||||
}
|
||||
Reference in New Issue
Block a user