refactor: 重构 token 管理,新增 NATS adapter 和多示例
按照 TOKEN_REFACTOR.md / FIX_CONFIG.md 方案重构核心层: - miot: Init() 优先读 storage token,新增 RefreshAuthInfo() 和刷新回调 - xiaomi: Config 改用 AuthInfo 结构体(含 UUID/UID/DataDir/OnTokenRefreshed) - xiaomi: NewClient(ctx, Config) 新签名,新增 RefreshToken(),后台自动刷新 - xiaomi/devices: 设备控制接口重构 - 新增示例 12_ac_subscribe,其余示例适配新 Config
This commit is contained in:
+6
-2
@@ -5,15 +5,17 @@ import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"xiaomihome/miot"
|
||||
)
|
||||
|
||||
// TokenResult OAuth token 交换结果
|
||||
// TokenResult OAuth token 交换/刷新结果
|
||||
type TokenResult struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
RefreshToken string `json:"refresh_token"`
|
||||
ExpiresIn int `json:"expires_in"`
|
||||
ExpiresTS int64 `json:"expires_ts"`
|
||||
UID string `json:"uid"`
|
||||
}
|
||||
|
||||
@@ -30,10 +32,12 @@ func ExchangeCode(ctx context.Context, cfg Config, code string) (*TokenResult, e
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("xiaomi: exchange token: %w", err)
|
||||
}
|
||||
expiresIn := intVal(result, "expires_in")
|
||||
return &TokenResult{
|
||||
AccessToken: strVal(result, "access_token"),
|
||||
RefreshToken: strVal(result, "refresh_token"),
|
||||
ExpiresIn: intVal(result, "expires_in"),
|
||||
ExpiresIn: expiresIn,
|
||||
ExpiresTS: time.Now().Unix() + int64(expiresIn),
|
||||
UID: strVal(result, "uid"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user