Skip to content

Commit aab3ba6

Browse files
fix: cache expo auth to prevent ENHANCE_YOUR_CALM rate limit (#58) (#60)
* fix: cache expo auth to prevent ENHANCE_YOUR_CALM rate limit (#58) * fix: hash expo auth tokens for cache key to enhance security * fix: enhance cache key for selfExpoUsername with token hash to improve uniquenes --------- Co-authored-by: amir-schmooze <amir@schmoozedating.com>
1 parent 60af680 commit aab3ba6

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

internal/services/expo.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package services
33
import (
44
"bytes"
55
"context"
6+
"crypto/sha256"
67
"encoding/json"
78
"errors"
89
"expo-open-ota/config"
@@ -258,6 +259,24 @@ func FetchExpoBranches() ([]string, error) {
258259
}
259260

260261
func FetchExpoUserAccountInformations(expoAuth types.ExpoAuth) (*ExpoUserAccount, error) {
262+
cache := cache2.GetCache()
263+
var cacheKey string
264+
if expoAuth.Token != nil {
265+
h := sha256.Sum256([]byte(*expoAuth.Token))
266+
cacheKey = fmt.Sprintf("expoUserAccount:token:%x", h)
267+
} else if expoAuth.SessionSecret != nil {
268+
h := sha256.Sum256([]byte(*expoAuth.SessionSecret))
269+
cacheKey = fmt.Sprintf("expoUserAccount:session:%x", h)
270+
}
271+
if cacheKey != "" {
272+
if cachedValue := cache.Get(cacheKey); cachedValue != "" {
273+
var account ExpoUserAccount
274+
if err := json.Unmarshal([]byte(cachedValue), &account); err == nil {
275+
return &account, nil
276+
}
277+
}
278+
}
279+
261280
query := `
262281
query GetCurrentUserAccount {
263282
me {
@@ -284,17 +303,31 @@ func FetchExpoUserAccountInformations(expoAuth types.ExpoAuth) (*ExpoUserAccount
284303
return nil, err
285304
}
286305

306+
if cacheKey != "" {
307+
if cacheValue, err := json.Marshal(resp.Data.Me); err == nil {
308+
ttl := 300
309+
_ = cache.Set(cacheKey, string(cacheValue), &ttl)
310+
}
311+
}
312+
287313
return &resp.Data.Me, nil
288314
}
289315

290316
func FetchSelfExpoUsername() string {
317+
cache := cache2.GetCache()
291318
token := GetExpoAccessToken()
319+
cacheKey := fmt.Sprintf("selfExpoUsername:%s:%x", version.Version, sha256.Sum256([]byte(token)))
320+
if cachedValue := cache.Get(cacheKey); cachedValue != "" {
321+
return cachedValue
322+
}
292323
expoAccount, err := FetchExpoUserAccountInformations(types.ExpoAuth{
293324
Token: &token,
294325
})
295326
if err != nil {
296327
return ""
297328
}
329+
ttl := 86400
330+
_ = cache.Set(cacheKey, expoAccount.Username, &ttl)
298331
return expoAccount.Username
299332
}
300333

0 commit comments

Comments
 (0)