Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
tank
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor 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
AURA
tank
Commits
589d6ba8
Commit
589d6ba8
authored
6 years ago
by
Christian Pointner
Browse files
Options
Downloads
Patches
Plain Diff
improved session expiry... needs testing and some thougts!
parent
930736e7
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
auth/sessions.go
+30
-12
30 additions, 12 deletions
auth/sessions.go
auth/sessions_test.go
+5
-1
5 additions, 1 deletion
auth/sessions_test.go
with
35 additions
and
13 deletions
auth/sessions.go
+
30
−
12
View file @
589d6ba8
...
...
@@ -82,7 +82,7 @@ type Session struct {
ID
string
state
SessionState
E
xpires
time
.
Tim
e
e
xpires
atomic
.
Valu
e
Username
string
ReadOnly
bool
AllShows
bool
...
...
@@ -101,6 +101,7 @@ func NewSession() (s *Session, err error) {
return
}
s
.
setState
(
SessionStateNew
)
s
.
setExpires
(
&
time
.
Time
{})
return
}
...
...
@@ -115,25 +116,40 @@ func (s *Session) State() SessionState {
}
func
(
s
*
Session
)
setState
(
st
SessionState
)
{
// TODO: notify subscribers! Check for subscriber channel will be racy...
atomic
.
StoreUint32
((
*
uint32
)(
&
s
.
state
),
uint32
(
st
))
}
func
(
s
*
Session
)
updateState
(
old
,
new
SessionState
)
bool
{
// TODO: notify subscribers! Check for subscriber channel will be racy...
return
atomic
.
CompareAndSwapUint32
((
*
uint32
)(
&
s
.
state
),
uint32
(
old
),
uint32
(
new
))
}
func
(
s
*
Session
)
Expires
()
*
time
.
Time
{
exp
:=
s
.
expires
.
Load
()
if
exp
==
nil
{
return
nil
}
return
exp
.
(
*
time
.
Time
)
}
func
(
s
*
Session
)
setExpires
(
exp
*
time
.
Time
)
{
s
.
expires
.
Store
(
exp
)
}
func
(
s
*
Session
)
Expired
()
bool
{
if
s
.
Expires
.
IsZero
()
{
exp
:=
s
.
Expires
()
if
exp
==
nil
{
return
false
}
return
s
.
Expires
.
Before
(
time
.
Now
())
return
exp
.
Before
(
time
.
Now
())
}
func
(
s
*
Session
)
MarshalJSON
()
([]
byte
,
error
)
{
return
json
.
Marshal
(
struct
{
ID
string
`json:"id"`
State
SessionState
`json:"state"`
Expires
time
.
Time
`json:"expires,omitempty"`
Expires
*
time
.
Time
`json:"expires,omitempty"`
Username
string
`json:"username"`
ReadOnly
bool
`json:"readonly"`
AllShows
bool
`json:"all-shows"`
...
...
@@ -141,7 +157,7 @@ func (s *Session) MarshalJSON() ([]byte, error) {
}{
ID
:
s
.
ID
,
State
:
s
.
State
(),
Expires
:
s
.
Expires
,
Expires
:
s
.
Expires
()
,
Username
:
s
.
Username
,
ReadOnly
:
s
.
ReadOnly
,
AllShows
:
s
.
AllShows
,
...
...
@@ -228,10 +244,10 @@ func (sm *SessionManager) runMaintenance() {
}
func
(
sm
*
SessionManager
)
insert
(
s
*
Session
)
(
err
error
)
{
now
:=
time
.
Now
()
e
Ma
x
:=
now
.
Add
(
sm
.
maxAge
)
if
s
.
Expires
.
Before
(
now
)
||
s
.
Expires
.
After
(
eMax
)
{
s
.
Expires
=
eMax
eMax
:=
time
.
Now
()
.
Add
(
sm
.
maxAge
)
ex
p
:=
s
.
Expires
(
)
if
exp
!=
nil
&&
(
exp
.
IsZero
()
||
exp
.
After
(
eMax
)
)
{
s
.
set
Expires
(
&
eMax
)
}
sm
.
mutex
.
Lock
()
...
...
@@ -273,9 +289,11 @@ func (sm *SessionManager) update(id string, s *Session) error {
if
!
ok
{
return
errors
.
New
(
"session not found."
)
}
if
s
.
Expires
.
IsZero
()
{
s
.
Expires
=
old
.
Expires
}
// TODO: figure out how to handle expiry on update...
// exp := s.Expires()
// if exp != nil && exp.IsZero() {
// s.setExpires(old.Expires()) // TODO: enforce maxAge!
// }
s
.
secret
=
old
.
secret
s
.
ID
=
old
.
ID
if
old
.
subscribe
!=
nil
{
...
...
This diff is collapsed.
Click to expand it.
auth/sessions_test.go
+
5
−
1
View file @
589d6ba8
...
...
@@ -52,7 +52,11 @@ func TestSessionExpiry(t *testing.T) {
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
}
s1
:=
&
Session
{
Username
:
"test"
}
s1
,
err
:=
NewSession
()
if
err
!=
nil
{
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
}
s1
.
Username
=
"test"
if
err
=
sm
.
insert
(
s1
);
err
!=
nil
{
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
}
...
...
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