Skip to content

Add locks around session store map #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mockoidc
import (
"errors"
"strings"
"sync"
"time"

"github.com/golang-jwt/jwt"
Expand All @@ -21,6 +22,7 @@ type Session struct {

// SessionStore manages our Session objects
type SessionStore struct {
sync.RWMutex
Store map[string]*Session
CodeQueue *CodeQueue
}
Expand Down Expand Up @@ -55,14 +57,18 @@ func (ss *SessionStore) NewSession(scope string, nonce string, user User, codeCh
CodeChallenge: codeChallenge,
CodeChallengeMethod: codeChallengeMethod,
}
ss.Lock()
ss.Store[sessionID] = session
ss.Unlock()

return session, nil
}

// GetSessionByID looks up the Session
func (ss *SessionStore) GetSessionByID(id string) (*Session, error) {
ss.RLock()
session, ok := ss.Store[id]
ss.RUnlock()
if !ok {
return nil, errors.New("session not found")
}
Expand Down
2 changes: 2 additions & 0 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ func TestSessionStore_NewSession(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, session.Scopes, []string{"openid", "email", "profile"})
assert.Equal(t, len(ss.Store), 1)
ss.RLock()
assert.Equal(t, ss.Store[session.SessionID], session)
ss.RUnlock()
assert.Equal(t, session.CodeChallenge, "sum")
assert.Equal(t, session.CodeChallengeMethod, "S256")
}
Expand Down