Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
W
webapp
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
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
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
leap
webapp
Commits
66b05aa9
There was a problem fetching the pipeline summary.
Commit
66b05aa9
authored
7 years ago
by
azul
Browse files
Options
Downloads
Plain Diff
Merge branch 'fix/token-conflict' into 'master'
prevent token conflicts Closes
#8792
See merge request
!42
parents
38ce3a14
0a161e88
No related branches found
No related tags found
1 merge request
!42
prevent token conflicts
Pipeline
#
Changes
3
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
app/models/token.rb
+39
-12
39 additions, 12 deletions
app/models/token.rb
public/favicon.ico
+0
-0
0 additions, 0 deletions
public/favicon.ico
test/unit/token_test.rb
+25
-2
25 additions, 2 deletions
test/unit/token_test.rb
with
64 additions
and
14 deletions
app/models/token.rb
+
39
−
12
View file @
66b05aa9
...
@@ -35,8 +35,13 @@ class Token < CouchRest::Model::Base
...
@@ -35,8 +35,13 @@ class Token < CouchRest::Model::Base
by_last_seen_at
.
endkey
(
expires_after
.
minutes
.
ago
)
by_last_seen_at
.
endkey
(
expires_after
.
minutes
.
ago
)
end
end
def
self
.
to_cleanup
return
[]
unless
expires_after
by_last_seen_at
.
endkey
((
expires_after
+
5
).
minutes
.
ago
)
end
def
self
.
destroy_all_expired
def
self
.
destroy_all_expired
self
.
expired
.
each
do
|
token
|
self
.
to_cleanup
.
each
do
|
token
|
token
.
destroy
token
.
destroy
end
end
end
end
...
@@ -46,27 +51,29 @@ class Token < CouchRest::Model::Base
...
@@ -46,27 +51,29 @@ class Token < CouchRest::Model::Base
end
end
def
authenticate
def
authenticate
if
expired?
return
if
expired?
destroy
return
nil
else
touch
touch
return
user
return
user
end
rescue
CouchRest
::
NotFound
# Reload in touch failed - token has been deleted.
# That's either an active logout or account destruction.
# We don't accept the token anymore.
end
end
# Tokens can be cleaned up in different ways.
# Tokens can be cleaned up in different ways.
# So let's make sure we don't crash if they disappeared
# So let's make sure we don't crash if they disappeared
def
destroy_with_rescue
def
destroy_with_rescue
destroy_without_rescue
destroy_without_rescue
rescue
CouchRest
::
NotFound
rescue
CouchRest
::
Conflict
# do nothing - it's been updated - #7670
rescue
CouchRest
::
Conflict
# do nothing - it's been updated - #7670
try_to_reload
&&
retry
rescue
CouchRest
::
NotFound
end
end
alias_method_chain
:destroy
,
:rescue
alias_method_chain
:destroy
,
:rescue
def
touch
def
touch
self
.
last_seen_at
=
Time
.
now
update_attributes
last_seen_at:
Time
.
now
save
rescue
CouchRest
::
Conflict
reload
&&
retry
end
end
def
expired?
def
expired?
...
@@ -82,5 +89,25 @@ class Token < CouchRest::Model::Base
...
@@ -82,5 +89,25 @@ class Token < CouchRest::Model::Base
self
.
last_seen_at
=
Time
.
now
self
.
last_seen_at
=
Time
.
now
end
end
end
end
# UPGRADE: the underlying code here changes between CouchRest::Model
# 2.1.0.rc1 and 2.2.0.beta2
# Hopefully we'll also get a pr merged that pushes this workaround
# upstream:
# https://github.com/couchrest/couchrest_model/pull/223
def
reload
prepare_all_attributes
(
database
.
get!
(
id
),
:directly_set_attributes
=>
true
)
self
end
end
protected
def
try_to_reload
reload
rescue
CouchRest
::
NotFound
return
false
end
end
This diff is collapsed.
Click to expand it.
public/favicon.ico
+
0
−
0
View file @
66b05aa9
No preview for this file type
This diff is collapsed.
Click to expand it.
test/unit/token_test.rb
+
25
−
2
View file @
66b05aa9
...
@@ -59,7 +59,6 @@ class TokenTest < ActiveSupport::TestCase
...
@@ -59,7 +59,6 @@ class TokenTest < ActiveSupport::TestCase
sample
=
Token
.
new
(
user_id:
@user
.
id
)
sample
=
Token
.
new
(
user_id:
@user
.
id
)
sample
.
last_seen_at
=
2
.
hours
.
ago
sample
.
last_seen_at
=
2
.
hours
.
ago
with_config
auth:
{
token_expires_after:
60
}
do
with_config
auth:
{
token_expires_after:
60
}
do
sample
.
expects
(
:destroy
)
assert_nil
sample
.
authenticate
assert_nil
sample
.
authenticate
end
end
end
end
...
@@ -83,7 +82,6 @@ class TokenTest < ActiveSupport::TestCase
...
@@ -83,7 +82,6 @@ class TokenTest < ActiveSupport::TestCase
fresh
.
destroy
fresh
.
destroy
end
end
test
"Token.destroy_all_expired does not interfere with expired.authenticate"
do
test
"Token.destroy_all_expired does not interfere with expired.authenticate"
do
expired
=
FactoryGirl
.
create
:token
,
last_seen_at:
2
.
hours
.
ago
expired
=
FactoryGirl
.
create
:token
,
last_seen_at:
2
.
hours
.
ago
with_config
auth:
{
token_expires_after:
60
}
do
with_config
auth:
{
token_expires_after:
60
}
do
...
@@ -92,4 +90,29 @@ class TokenTest < ActiveSupport::TestCase
...
@@ -92,4 +90,29 @@ class TokenTest < ActiveSupport::TestCase
assert_nil
expired
.
authenticate
assert_nil
expired
.
authenticate
end
end
test
"active logout (destroy) prevents reuse"
do
token
=
FactoryGirl
.
create
:token
same
=
Token
.
find
(
token
.
id
)
token
.
destroy
assert_raises
CouchRest
::
NotFound
do
same
.
touch
end
end
test
"logout works on prolonged token"
do
token
=
FactoryGirl
.
create
:token
same
=
Token
.
find
(
token
.
id
)
token
.
touch
same
.
destroy
assert_nil
Token
.
find
(
same
.
id
)
end
test
'second destroy carries on'
do
token
=
FactoryGirl
.
create
:token
same
=
Token
.
find
(
token
.
id
)
token
.
destroy
same
.
destroy
assert_nil
Token
.
find
(
same
.
id
)
end
end
end
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