fix: simplify subject.session key parsing
This commit is contained in:
committed by
Christian Richter
parent
e8ecbd7af1
commit
b69b9cd569
@@ -68,24 +68,21 @@ var ErrInvalidSubjectOrSession = errors.New("invalid subject or session")
|
|||||||
// NewSuSe parses the subject and session id from the given key and returns a SuSe struct
|
// NewSuSe parses the subject and session id from the given key and returns a SuSe struct
|
||||||
func NewSuSe(key string) (SuSe, error) {
|
func NewSuSe(key string) (SuSe, error) {
|
||||||
suse := SuSe{}
|
suse := SuSe{}
|
||||||
switch keys := strings.Split(strings.Join(strings.Fields(key), ""), "."); {
|
keys := strings.Split(key, ".")
|
||||||
// key: '.session'
|
switch len(keys) {
|
||||||
case len(keys) == 2 && keys[0] == "" && keys[1] != "":
|
case 1:
|
||||||
suse.encodedSession = keys[1]
|
|
||||||
// key: 'subject.'
|
|
||||||
case len(keys) == 2 && keys[0] != "" && keys[1] == "":
|
|
||||||
suse.encodedSubject = keys[0]
|
|
||||||
// key: 'subject.session'
|
|
||||||
case len(keys) == 2 && keys[0] != "" && keys[1] != "":
|
|
||||||
suse.encodedSubject = keys[0]
|
|
||||||
suse.encodedSession = keys[1]
|
|
||||||
// key: 'session'
|
|
||||||
case len(keys) == 1 && keys[0] != "":
|
|
||||||
suse.encodedSession = keys[0]
|
suse.encodedSession = keys[0]
|
||||||
|
case 2:
|
||||||
|
suse.encodedSubject = keys[0]
|
||||||
|
suse.encodedSession = keys[1]
|
||||||
default:
|
default:
|
||||||
return suse, ErrInvalidSubjectOrSession
|
return suse, ErrInvalidSubjectOrSession
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if suse.encodedSubject == "" && suse.encodedSession == "" {
|
||||||
|
return suse, ErrInvalidSubjectOrSession
|
||||||
|
}
|
||||||
|
|
||||||
if _, err := suse.Subject(); err != nil {
|
if _, err := suse.Subject(); err != nil {
|
||||||
return suse, errors.Join(ErrInvalidSubjectOrSession, err)
|
return suse, errors.Join(ErrInvalidSubjectOrSession, err)
|
||||||
}
|
}
|
||||||
@@ -134,13 +131,13 @@ func GetLogoutRecords(suse SuSe, store microstore.Store) ([]*microstore.Record,
|
|||||||
|
|
||||||
var key string
|
var key string
|
||||||
var opts []microstore.ReadOption
|
var opts []microstore.ReadOption
|
||||||
switch mode {
|
switch {
|
||||||
case logoutModeSubject:
|
case mode == logoutModeSubject && suse.encodedSubject != "":
|
||||||
// the dot at the end prevents prefix exploration in the cache,
|
// the dot at the end prevents prefix exploration in the cache,
|
||||||
// so only keys that start with 'subject.*' will be returned, but not 'sub*'.
|
// so only keys that start with 'subject.*' will be returned, but not 'sub*'.
|
||||||
key = suse.encodedSubject + "."
|
key = suse.encodedSubject + "."
|
||||||
opts = append(opts, microstore.ReadPrefix())
|
opts = append(opts, microstore.ReadPrefix())
|
||||||
case logoutModeSession:
|
case mode == logoutModeSession && suse.encodedSession != "":
|
||||||
// the dot at the beginning prevents sufix exploration in the cache,
|
// the dot at the beginning prevents sufix exploration in the cache,
|
||||||
// so only keys that end with '*.session' will be returned, but not '*sion'.
|
// so only keys that end with '*.session' will be returned, but not '*sion'.
|
||||||
key = "." + suse.encodedSession
|
key = "." + suse.encodedSession
|
||||||
|
|||||||
Reference in New Issue
Block a user