|
| 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 memory |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "errors" |
| 22 | + "sync" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/kube-bind/kube-bind/backend/session" |
| 26 | +) |
| 27 | + |
| 28 | +type pkceEntry struct { |
| 29 | + verifier string |
| 30 | + expiresAt time.Time |
| 31 | +} |
| 32 | + |
| 33 | +type Store struct { |
| 34 | + lock sync.RWMutex |
| 35 | + sessions map[string]*session.State |
| 36 | + pkceVerifiers map[string]pkceEntry |
| 37 | +} |
| 38 | + |
| 39 | +func New() session.Store { |
| 40 | + return &Store{ |
| 41 | + sessions: make(map[string]*session.State), |
| 42 | + pkceVerifiers: make(map[string]pkceEntry), |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func (s *Store) Save(ctx context.Context, state *session.State) error { |
| 47 | + s.lock.Lock() |
| 48 | + defer s.lock.Unlock() |
| 49 | + s.sessions[state.SessionID] = state |
| 50 | + return nil |
| 51 | +} |
| 52 | + |
| 53 | +func (s *Store) Load(ctx context.Context, sessionID string) (*session.State, error) { |
| 54 | + s.lock.RLock() |
| 55 | + defer s.lock.RUnlock() |
| 56 | + state, exists := s.sessions[sessionID] |
| 57 | + if !exists { |
| 58 | + return nil, session.ErrSessionNotFound |
| 59 | + } |
| 60 | + return state, nil |
| 61 | +} |
| 62 | + |
| 63 | +func (s *Store) Delete(ctx context.Context, sessionID string) error { |
| 64 | + s.lock.Lock() |
| 65 | + defer s.lock.Unlock() |
| 66 | + delete(s.sessions, sessionID) |
| 67 | + return nil |
| 68 | +} |
| 69 | + |
| 70 | +func (s *Store) SavePKCEVerifier(ctx context.Context, sessionID, verifier string) error { |
| 71 | + if sessionID == "" || verifier == "" { |
| 72 | + return errors.New("sessionID and verifier cannot be empty") |
| 73 | + } |
| 74 | + s.lock.Lock() |
| 75 | + defer s.lock.Unlock() |
| 76 | + s.pkceVerifiers[sessionID] = pkceEntry{ |
| 77 | + verifier: verifier, |
| 78 | + expiresAt: time.Now().Add(session.PKCEVerifierTTL), |
| 79 | + } |
| 80 | + return nil |
| 81 | +} |
| 82 | + |
| 83 | +func (s *Store) LoadAndDeletePKCEVerifier(ctx context.Context, sessionID string) (string, error) { |
| 84 | + if sessionID == "" { |
| 85 | + return "", session.ErrPKCEVerifierNotFound |
| 86 | + } |
| 87 | + s.lock.Lock() |
| 88 | + defer s.lock.Unlock() |
| 89 | + entry, ok := s.pkceVerifiers[sessionID] |
| 90 | + if !ok { |
| 91 | + return "", session.ErrPKCEVerifierNotFound |
| 92 | + } |
| 93 | + |
| 94 | + delete(s.pkceVerifiers, sessionID) |
| 95 | + |
| 96 | + if time.Now().After(entry.expiresAt) { |
| 97 | + return "", session.ErrPKCEVerifierNotFound |
| 98 | + } |
| 99 | + |
| 100 | + return entry.verifier, nil |
| 101 | +} |
0 commit comments