|
| 1 | +package irdata |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "crypto/sha512" |
| 7 | + "encoding/gob" |
| 8 | + "errors" |
| 9 | + "fmt" |
| 10 | + "github.com/redis/go-redis/v9" |
| 11 | + "log/slog" |
| 12 | + "net/http" |
| 13 | + "net/url" |
| 14 | + "time" |
| 15 | +) |
| 16 | + |
| 17 | +type RedisAuthenticator struct { |
| 18 | + Authenticator |
| 19 | + |
| 20 | + redis redis.Cmdable |
| 21 | + authenticator Authenticator |
| 22 | + |
| 23 | + Logger Logger |
| 24 | + KeyPrefix string |
| 25 | + LocalCacheDuration time.Duration |
| 26 | + |
| 27 | + localCache map[string]*AuthenticationResult |
| 28 | +} |
| 29 | + |
| 30 | +func NewRedisDefaultAuthenticator(redis redis.Cmdable, irdata IRData) (*RedisAuthenticator, error) { |
| 31 | + authenticator, err := NewDefaultAuthenticator(irdata) |
| 32 | + if err != nil { |
| 33 | + return nil, err |
| 34 | + } |
| 35 | + |
| 36 | + return NewRedisAuthenticator(redis, authenticator) |
| 37 | +} |
| 38 | + |
| 39 | +func NewRedisAuthenticator(redis redis.Cmdable, authenticator Authenticator) (*RedisAuthenticator, error) { |
| 40 | + if redis == nil { |
| 41 | + return nil, errors.New("redis is nil") |
| 42 | + } else if authenticator == nil { |
| 43 | + return nil, errors.New("authenticator is nil") |
| 44 | + } |
| 45 | + |
| 46 | + return &RedisAuthenticator{ |
| 47 | + redis: redis, |
| 48 | + authenticator: authenticator, |
| 49 | + Logger: slog.Default(), |
| 50 | + KeyPrefix: "iracing:irdata:", |
| 51 | + LocalCacheDuration: time.Minute, |
| 52 | + localCache: make(map[string]*AuthenticationResult), |
| 53 | + }, nil |
| 54 | +} |
| 55 | + |
| 56 | +type AuthenticationResult struct { |
| 57 | + Username string |
| 58 | + PasswordChecksum string |
| 59 | + URL *url.URL |
| 60 | + Cookies []*http.Cookie |
| 61 | + Expiration time.Time |
| 62 | + |
| 63 | + lastUpdated time.Time |
| 64 | +} |
| 65 | + |
| 66 | +func init() { |
| 67 | + gob.Register(AuthenticationResult{}) |
| 68 | +} |
| 69 | + |
| 70 | +func (a *RedisAuthenticator) Authenticate(username string, password string, _ bool) (*url.URL, []*http.Cookie, time.Time, error) { |
| 71 | + ctx, cancel := context.WithCancel(context.Background()) |
| 72 | + defer cancel() |
| 73 | + |
| 74 | + hasher := sha512.New() |
| 75 | + hasher.Write([]byte(password)) |
| 76 | + hashedPassword := fmt.Sprintf("%032x", hasher.Sum(nil)) |
| 77 | + |
| 78 | + key := fmt.Sprintf("%s%s-%s", a.KeyPrefix, username, hashedPassword) |
| 79 | + existing, ok := a.localCache[key] |
| 80 | + if ok && existing.lastUpdated.Before(time.Now().Add(-a.LocalCacheDuration)) { |
| 81 | + return existing.URL, existing.Cookies, existing.Expiration, nil |
| 82 | + } else if ok { |
| 83 | + delete(a.localCache, key) |
| 84 | + } |
| 85 | + |
| 86 | + existingResp := a.redis.Get(ctx, key) |
| 87 | + if existingResp == nil || existingResp.Err() != nil { |
| 88 | + if existingResp == nil || !errors.Is(existingResp.Err(), redis.Nil) { |
| 89 | + var err error |
| 90 | + if existingResp == nil { |
| 91 | + err = errors.New("no response from redis") |
| 92 | + } else { |
| 93 | + err = fmt.Errorf("unexpected response from redis: %s", existingResp.Err()) |
| 94 | + } |
| 95 | + |
| 96 | + a.Logger.Warn("unable to retrieve cached result from redis", "error", err) |
| 97 | + } |
| 98 | + |
| 99 | + return a.authenticate(username, password, hashedPassword, key) |
| 100 | + } |
| 101 | + |
| 102 | + result := &AuthenticationResult{} |
| 103 | + decoder := gob.NewDecoder(bytes.NewBuffer([]byte(existingResp.Val()))) |
| 104 | + err := decoder.Decode(result) |
| 105 | + if err != nil { |
| 106 | + a.Logger.Warn("unable to decode cached result from redis", "error", err) |
| 107 | + return a.authenticate(username, password, hashedPassword, key) |
| 108 | + } |
| 109 | + |
| 110 | + result.lastUpdated = time.Now() |
| 111 | + a.localCache[key] = result |
| 112 | + return result.URL, result.Cookies, result.Expiration, nil |
| 113 | +} |
| 114 | + |
| 115 | +func (a *RedisAuthenticator) authenticate(username, password, hashedPassword, key string) (*url.URL, []*http.Cookie, time.Time, error) { |
| 116 | + cookieUrl, cookies, expiration, err := a.authenticator.Authenticate(username, password, true) |
| 117 | + if err != nil { |
| 118 | + return nil, nil, time.Time{}, err |
| 119 | + } |
| 120 | + |
| 121 | + result := &AuthenticationResult{ |
| 122 | + Username: username, |
| 123 | + PasswordChecksum: hashedPassword, |
| 124 | + URL: cookieUrl, |
| 125 | + Cookies: cookies, |
| 126 | + Expiration: expiration, |
| 127 | + lastUpdated: time.Now(), |
| 128 | + } |
| 129 | + |
| 130 | + a.localCache[key] = result |
| 131 | + |
| 132 | + buf := new(bytes.Buffer) |
| 133 | + err = gob.NewEncoder(buf).Encode(result) |
| 134 | + if err != nil { |
| 135 | + a.Logger.Warn("unable to encode cached result for redis", "error", err) |
| 136 | + return cookieUrl, cookies, expiration, nil |
| 137 | + } |
| 138 | + |
| 139 | + setResp := a.redis.Set(context.Background(), key, buf.Bytes(), time.Until(result.Expiration)) |
| 140 | + if setResp == nil || setResp.Err() != nil { |
| 141 | + a.Logger.Warn("unable to set cached result in redis", "error", err) |
| 142 | + } |
| 143 | + |
| 144 | + return cookieUrl, cookies, expiration, nil |
| 145 | +} |
0 commit comments