Skip to content
Snippets Groups Projects
Commit dd5838f0 authored by Francé Wilke's avatar Francé Wilke
Browse files

Merge branch 'main' of gitlab.bob.co.za:bob-public-utils/bobgroup-go-utils

parents f4f9b54d 2499d761
No related branches found
No related tags found
No related merge requests found
...@@ -86,23 +86,25 @@ func FindAndRemoveCurrentSessionToken(jsonWebTokenString string, secretKey strin ...@@ -86,23 +86,25 @@ func FindAndRemoveCurrentSessionToken(jsonWebTokenString string, secretKey strin
return "", sessionTokens return "", sessionTokens
} }
// RemoveOldSessionTokens checks the age of the session tokens and removes the ones that are older than the provided age. // RemoveInvalidatedAndOldSessionTokens removes the provided invalidated session token, and checks the age of the
func RemoveOldSessionTokens(sessionTokens []string, age time.Duration) []string { // other session tokens and removes the ones that are older than the provided age.
var validTokens []string func RemoveInvalidatedAndOldSessionTokens(sessionTokens []string, invalidatedSessionToken string, age time.Duration) []string {
ageDurationAgo := date_utils.CurrentDate().Add(-1 * age)
validTokens := funk.FilterString(sessionTokens, func(sessionTokenString string) bool {
// Always remove the invalidated session token
if sessionTokenString == invalidatedSessionToken {
return false
}
oneWeekAgo := date_utils.CurrentDate().Add(-1 * age)
for _, sessionTokenString := range sessionTokens {
var sessionToken SessionToken var sessionToken SessionToken
err := json.Unmarshal([]byte(sessionTokenString), &sessionToken) err := json.Unmarshal([]byte(sessionTokenString), &sessionToken)
if err != nil { if err != nil {
// If we can't unmarshal the token then it is not valid // If we can't unmarshal the token then it is not valid
continue return false
} }
// Keep the token if it was created in the past week // Keep the token if it hasn't expired yet
if sessionToken.TimeCreated.In(date_utils.CurrentLocation()).After(oneWeekAgo) { return sessionToken.TimeCreated.In(date_utils.CurrentLocation()).After(ageDurationAgo)
validTokens = append(validTokens, sessionTokenString) })
}
}
return validTokens return validTokens
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment