mirror of
https://github.com/sky22333/hubproxy.git
synced 2026-06-24 00:54:35 +08:00
fix 修复返回了过期的匿名token
This commit is contained in:
@@ -102,10 +102,18 @@ func ExtractTTLFromResponse(responseBody []byte) time.Duration {
|
||||
defaultTTL := 30 * time.Minute
|
||||
|
||||
if json.Unmarshal(responseBody, &tokenResp) == nil && tokenResp.ExpiresIn > 0 {
|
||||
safeTTL := time.Duration(tokenResp.ExpiresIn-300) * time.Second
|
||||
if safeTTL > 5*time.Minute {
|
||||
return safeTTL
|
||||
expires := time.Duration(tokenResp.ExpiresIn) * time.Second
|
||||
skew := expires / 10
|
||||
if skew > 5*time.Minute {
|
||||
skew = 5 * time.Minute
|
||||
}
|
||||
if skew < 10*time.Second {
|
||||
skew = 10 * time.Second
|
||||
}
|
||||
if expires > skew {
|
||||
return expires - skew
|
||||
}
|
||||
return expires / 2
|
||||
}
|
||||
|
||||
return defaultTTL
|
||||
|
||||
@@ -34,6 +34,10 @@ func TestExtractTTLFromResponse(t *testing.T) {
|
||||
t.Fatalf("TTL = %s, want 55m", ttl)
|
||||
}
|
||||
|
||||
if ttl := ExtractTTLFromResponse([]byte(`{"expires_in":300}`)); ttl != 270*time.Second {
|
||||
t.Fatalf("short TTL = %s, want 270s", ttl)
|
||||
}
|
||||
|
||||
if ttl := ExtractTTLFromResponse([]byte(`{}`)); ttl != 30*time.Minute {
|
||||
t.Fatalf("default TTL = %s", ttl)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user