|
| 1 | +/* |
| 2 | +Copyright 2026 The Kube Bind Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package session |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/redis/go-redis/v9" |
| 26 | + "github.com/vmihailenco/msgpack/v4" |
| 27 | + "k8s.io/klog/v2" |
| 28 | +) |
| 29 | + |
| 30 | +type RedisStore struct { |
| 31 | + client *redis.Client |
| 32 | +} |
| 33 | + |
| 34 | +func NewRedisStore(redisAddr string, redisPassword string) Store { |
| 35 | + client := redis.NewClient(&redis.Options{ |
| 36 | + Addr: redisAddr, |
| 37 | + Password: redisPassword, |
| 38 | + DB: 0, |
| 39 | + }) |
| 40 | + |
| 41 | + return &RedisStore{ |
| 42 | + client: client, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func (s *RedisStore) Save(ctx context.Context, state *State) error { |
| 47 | + encoded, err := state.Encode() |
| 48 | + if err != nil { |
| 49 | + return fmt.Errorf("failed to encode state: %w", err) |
| 50 | + } |
| 51 | + |
| 52 | + key := fmt.Sprintf("session:%s", state.SessionID) |
| 53 | + |
| 54 | + var ttl time.Duration = 0 |
| 55 | + if !state.ExpiresAt.IsZero() { |
| 56 | + ttl = time.Until(state.ExpiresAt) |
| 57 | + if ttl <= 0 { |
| 58 | + klog.FromContext(context.Background()).V(4).Info("Session already expired, skipping saving to redis", "sessionID", state.SessionID) |
| 59 | + return nil |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + err = s.client.Set(ctx, key, encoded, ttl).Err() |
| 64 | + if err != nil { |
| 65 | + return fmt.Errorf("failed to save session to redis: %w", err) |
| 66 | + } |
| 67 | + return nil |
| 68 | +} |
| 69 | + |
| 70 | +func (s *RedisStore) Load(ctx context.Context, sessionID string) (*State, error) { |
| 71 | + key := fmt.Sprintf("session:%s", sessionID) |
| 72 | + |
| 73 | + val, err := s.client.Get(ctx, key).Bytes() |
| 74 | + if err != nil { |
| 75 | + if errors.Is(err, redis.Nil) { |
| 76 | + return nil, ErrSessionNotFound |
| 77 | + } |
| 78 | + return nil, fmt.Errorf("failed to load session from redis: %w", err) |
| 79 | + } |
| 80 | + |
| 81 | + var state State |
| 82 | + err = msgpack.Unmarshal(val, &state) |
| 83 | + if err != nil { |
| 84 | + return nil, fmt.Errorf("failed to decode state from redis: %w", err) |
| 85 | + } |
| 86 | + |
| 87 | + return &state, nil |
| 88 | +} |
| 89 | + |
| 90 | +func (s *RedisStore) Delete(ctx context.Context, sessionID string) error { |
| 91 | + key := fmt.Sprintf("session:%s", sessionID) |
| 92 | + err := s.client.Del(ctx, key).Err() |
| 93 | + if err != nil { |
| 94 | + return fmt.Errorf("failed to delete session from redis: %w", err) |
| 95 | + } |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +func (s *RedisStore) SavePKCEVerifier(ctx context.Context, sessionID, verifier string) error { |
| 100 | + if sessionID == "" || verifier == "" { |
| 101 | + return errors.New("sessionID and verifier cannot be empty") |
| 102 | + } |
| 103 | + |
| 104 | + key := fmt.Sprintf("pkce:%s", sessionID) |
| 105 | + |
| 106 | + err := s.client.Set(ctx, key, verifier, 10*time.Minute).Err() |
| 107 | + if err != nil { |
| 108 | + return fmt.Errorf("failed to save pkce to redis: %w", err) |
| 109 | + } |
| 110 | + return nil |
| 111 | +} |
| 112 | + |
| 113 | +func (s *RedisStore) LoadAndDeletePKCEVerifier(ctx context.Context, sessionID string) (string, error) { |
| 114 | + if sessionID == "" { |
| 115 | + return "", ErrPKCEVerifierNotFound |
| 116 | + } |
| 117 | + |
| 118 | + key := fmt.Sprintf("pkce:%s", sessionID) |
| 119 | + |
| 120 | + val, err := s.client.Get(ctx, key).Result() |
| 121 | + if err != nil { |
| 122 | + if errors.Is(err, redis.Nil) { |
| 123 | + return "", ErrPKCEVerifierNotFound |
| 124 | + } |
| 125 | + return "", fmt.Errorf("failed to load pkce from redis: %w", err) |
| 126 | + } |
| 127 | + |
| 128 | + _ = s.client.Del(ctx, key).Err() |
| 129 | + |
| 130 | + return val, nil |
| 131 | +} |
0 commit comments