Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bobgroup-go-utils
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bob Public Utils
bobgroup-go-utils
Commits
2499d761
Commit
2499d761
authored
2 years ago
by
Jano Hendriks
Browse files
Options
Downloads
Patches
Plain Diff
Update function to remove expired session tokens to also remove an invalidated token
parent
aa796974
Branches
Branches containing commit
Tags
v1.105.0
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
auth/session.go
+13
-11
13 additions, 11 deletions
auth/session.go
with
13 additions
and
11 deletions
auth/session.go
+
13
−
11
View file @
2499d761
...
...
@@ -86,23 +86,25 @@ func FindAndRemoveCurrentSessionToken(jsonWebTokenString string, secretKey strin
return
""
,
sessionTokens
}
// RemoveOldSessionTokens checks the age of the session tokens and removes the ones that are older than the provided age.
func
RemoveOldSessionTokens
(
sessionTokens
[]
string
,
age
time
.
Duration
)
[]
string
{
var
validTokens
[]
string
// RemoveInvalidatedAndOldSessionTokens removes the provided invalidated session token, and checks the age of the
// other session tokens and removes the ones that are older than the provided age.
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
err
:=
json
.
Unmarshal
([]
byte
(
sessionTokenString
),
&
sessionToken
)
if
err
!=
nil
{
// If we can't unmarshal the token then it is not valid
continu
e
return
fals
e
}
// Keep the token if it was created in the past week
if
sessionToken
.
TimeCreated
.
In
(
date_utils
.
CurrentLocation
())
.
After
(
oneWeekAgo
)
{
validTokens
=
append
(
validTokens
,
sessionTokenString
)
}
}
// Keep the token if it hasn't expired yet
return
sessionToken
.
TimeCreated
.
In
(
date_utils
.
CurrentLocation
())
.
After
(
ageDurationAgo
)
})
return
validTokens
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment