@@ -161,35 +161,80 @@ func (b *DefaultSessionManager) RemoveSession(orgID string, keyName string, hash
161161
162162// SessionDetail returns the session detail using the storage engine (either in memory or Redis)
163163func (b * DefaultSessionManager ) SessionDetail (orgID string , keyName string , hashed bool ) (user.SessionState , bool ) {
164- return b .fetchSessionDetail (nil , orgID , keyName , hashed )
164+ var jsonKeyVal string
165+ var err error
166+ keyId := keyName
167+
168+ // get session by key
169+ if hashed {
170+ jsonKeyVal , err = b .store .GetRawKey (b .store .GetKeyPrefix () + keyName )
171+ } else {
172+ if storage .TokenOrg (keyName ) != orgID {
173+ // try to get legacy and new format key at once
174+ toSearchList := []string {}
175+ if ! b .Gw .GetConfig ().DisableKeyActionsByUsername {
176+ toSearchList = append (toSearchList , b .Gw .generateToken (orgID , keyName ))
177+ }
178+
179+ toSearchList = append (toSearchList , keyName )
180+ for _ , fallback := range b .Gw .GetConfig ().HashKeyFunctionFallback {
181+ if ! b .Gw .GetConfig ().DisableKeyActionsByUsername {
182+ toSearchList = append (toSearchList , b .Gw .generateToken (orgID , keyName , fallback ))
183+ }
184+ }
185+
186+ var jsonKeyValList []string
187+
188+ jsonKeyValList , err = b .store .GetMultiKey (toSearchList )
189+ // pick the 1st non empty from the returned list
190+ for idx , val := range jsonKeyValList {
191+ if val != "" {
192+ jsonKeyVal = val
193+ keyId = toSearchList [idx ]
194+ break
195+ }
196+ }
197+ } else {
198+ // key is not an imported one
199+ jsonKeyVal , err = b .store .GetKey (keyName )
200+ }
201+ }
202+
203+ if err != nil {
204+ log .WithFields (logrus.Fields {
205+ "prefix" : "auth-mgr" ,
206+ "inbound-key" : b .Gw .obfuscateKey (keyName ),
207+ "err" : err ,
208+ }).Debug ("Could not get session detail, key not found" )
209+ return user.SessionState {}, false
210+ }
211+ session := & user.SessionState {}
212+ if err := json .Unmarshal ([]byte (jsonKeyVal ), & session ); err != nil {
213+ log .Error ("Couldn't unmarshal session object (may be cache miss): " , err )
214+ return user.SessionState {}, false
215+ }
216+ session .KeyID = keyId
217+ return session .Clone (), true
165218}
166219
167220// SessionDetailContext returns the session detail using the storage engine with context support for cancellation
168221func (b * DefaultSessionManager ) SessionDetailContext (ctx context.Context , orgID string , keyName string , hashed bool ) (user.SessionState , bool ) {
169- return b .fetchSessionDetail (ctx , orgID , keyName , hashed )
170- }
171-
172- // fetchSessionDetail is the internal implementation shared by SessionDetail and SessionDetailContext
173- func (b * DefaultSessionManager ) fetchSessionDetail (ctx context.Context , orgID string , keyName string , hashed bool ) (user.SessionState , bool ) {
174- if ctx != nil {
175- select {
176- case <- ctx .Done ():
177- log .WithFields (logrus.Fields {
178- "prefix" : "auth-mgr" ,
179- "inbound-key" : b .Gw .obfuscateKey (keyName ),
180- }).Debug ("Context cancelled" )
181- return user.SessionState {}, false
182- default :
183- }
222+ select {
223+ case <- ctx .Done ():
224+ log .WithFields (logrus.Fields {
225+ "prefix" : "auth-mgr" ,
226+ "inbound-key" : b .Gw .obfuscateKey (keyName ),
227+ }).Debug ("Context cancelled" )
228+ return user.SessionState {}, false
229+ default :
184230 }
185231
186232 var jsonKeyVal string
187233 var err error
188234 keyId := keyName
189235
190- // get session by key
191236 if hashed {
192- jsonKeyVal , err = b .store .GetRawKey ( b .store .GetKeyPrefix () + keyName )
237+ jsonKeyVal , err = b .store .GetRawKeyContext ( ctx , b .store .GetKeyPrefix ()+ keyName )
193238 } else {
194239 if storage .TokenOrg (keyName ) != orgID {
195240 // try to get legacy and new format key at once
@@ -207,7 +252,7 @@ func (b *DefaultSessionManager) fetchSessionDetail(ctx context.Context, orgID st
207252
208253 var jsonKeyValList []string
209254
210- jsonKeyValList , err = b .store .GetMultiKey ( toSearchList )
255+ jsonKeyValList , err = b .store .GetMultiKeyContext ( ctx , toSearchList )
211256 // pick the 1st non empty from the returned list
212257 for idx , val := range jsonKeyValList {
213258 if val != "" {
@@ -218,7 +263,7 @@ func (b *DefaultSessionManager) fetchSessionDetail(ctx context.Context, orgID st
218263 }
219264 } else {
220265 // key is not an imported one
221- jsonKeyVal , err = b .store .GetKey ( keyName )
266+ jsonKeyVal , err = b .store .GetKeyContext ( ctx , keyName )
222267 }
223268 }
224269
0 commit comments