Skip to content

Commit 624ccf6

Browse files
committed
feat(oauth): make session TTL configurable via QODER2API_OAUTH_TIMEOUT
The 10-minute window is easy to miss when switching to a browser and logging in. Default stays 10m; operators can set e.g. 30m.
1 parent afe1d11 commit 624ccf6

1 file changed

Lines changed: 16 additions & 3 deletions

File tree

‎account/oauth.go‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"io"
1111
"net/http"
1212
"net/url"
13+
"os"
1314
"strings"
1415
"sync"
1516
"time"
@@ -24,6 +25,18 @@ import (
2425

2526
const oauthClientID = "e883ade2-e6e3-4d6d-adf7-f92ceff5fdcb"
2627

28+
// oauthSessionTTL 是待授权 OAuth 会话的有效期。默认 10 分钟(用户需在
29+
// 此时间内完成浏览器授权),可用 QODER2API_OAUTH_TIMEOUT 覆盖(如 "30m")。
30+
func oauthSessionTTL() time.Duration {
31+
if v := strings.TrimSpace(os.Getenv("QODER2API_OAUTH_TIMEOUT")); v != "" {
32+
if d, err := time.ParseDuration(v); err == nil && d > 0 {
33+
return d
34+
}
35+
logger.Error("invalid QODER2API_OAUTH_TIMEOUT=%q, falling back to 10m", v)
36+
}
37+
return 10 * time.Minute
38+
}
39+
2740
type pendingOAuth struct {
2841
loginID string
2942
nonce string
@@ -70,7 +83,7 @@ func StartLogin(region Region) (*OAuthSession, error) {
7083
region: region,
7184
ctx: ctx,
7285
cancel: cancel,
73-
deadline: time.Now().Add(10 * time.Minute),
86+
deadline: time.Now().Add(oauthSessionTTL()),
7487
}
7588
pendingMu.Unlock()
7689

@@ -280,10 +293,10 @@ func FetchQuota(token string, region Region) (*QuotaInfo, error) {
280293
}
281294
defer resp.Body.Close()
282295
raw, _ := io.ReadAll(resp.Body)
283-
296+
284297
fmt.Printf("[QUOTA DEBUG] API URL: %s\n", ep.QuotaEndpoint)
285298
fmt.Printf("[QUOTA DEBUG] Raw response: %s\n", string(raw))
286-
299+
287300
var result map[string]interface{}
288301
if err := json.Unmarshal(raw, &result); err != nil {
289302
return nil, err

0 commit comments

Comments
 (0)