Skip to content

Commit ae63560

Browse files
committed
as per review
Signed-off-by: Andrew Thornton <[email protected]>
1 parent 3c2e609 commit ae63560

File tree

1 file changed

+8
-20
lines changed

1 file changed

+8
-20
lines changed

services/auth/source/oauth2/store.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,19 @@ type SessionsStore struct {
1818

1919
// Get should return a cached session.
2020
func (st *SessionsStore) Get(r *http.Request, name string) (*sessions.Session, error) {
21-
chiStore := chiSession.GetSession(r)
22-
23-
rawData := chiStore.Get(name)
24-
if rawData == nil {
25-
return st.New(r, name)
26-
}
27-
28-
oldSession, ok := rawData.(*sessions.Session)
29-
if !ok {
30-
return nil, fmt.Errorf("unexpected object in session: %v at name: %s", rawData, name)
31-
}
32-
33-
// Copy over the old data into the session
34-
session := sessions.NewSession(st, name)
35-
session.ID = oldSession.ID
36-
session.IsNew = oldSession.IsNew
37-
session.Options = oldSession.Options
38-
session.Values = oldSession.Values
39-
40-
return session, nil
21+
return st.getOrNew(r, name, false)
4122
}
4223

4324
// New should create and return a new session.
4425
//
4526
// Note that New should never return a nil session, even in the case of
4627
// an error if using the Registry infrastructure to cache the session.
4728
func (st *SessionsStore) New(r *http.Request, name string) (*sessions.Session, error) {
29+
return st.getOrNew(r, name, true)
30+
}
31+
32+
// getOrNew gets the session from the chi-session if it exists. Override permits the overriding of an unexpected object.
33+
func (st *SessionsStore) getOrNew(r *http.Request, name string, override bool) (*sessions.Session, error) {
4834
chiStore := chiSession.GetSession(r)
4935

5036
session := sessions.NewSession(st, name)
@@ -60,6 +46,8 @@ func (st *SessionsStore) New(r *http.Request, name string) (*sessions.Session, e
6046
session.Values = oldSession.Values
6147

6248
return session, nil
49+
} else if !override {
50+
return nil, fmt.Errorf("unexpected object in session: %v at name: %s", rawData, name)
6351
}
6452
}
6553

0 commit comments

Comments
 (0)