Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
cfe1ff8b
Commit
cfe1ff8b
authored
Nov 03, 2017
by
Douwe Maan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Pull API job token behavior in line with other tokens, and expand test coverage
parent
9e4c01c9
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
49 additions
and
29 deletions
+49
-29
lib/api/api_guard.rb
lib/api/api_guard.rb
+15
-11
lib/api/helpers.rb
lib/api/helpers.rb
+1
-1
spec/requests/api/helpers_spec.rb
spec/requests/api/helpers_spec.rb
+33
-17
No files found.
lib/api/api_guard.rb
View file @
cfe1ff8b
...
@@ -45,7 +45,7 @@ module API
...
@@ -45,7 +45,7 @@ module API
# Helper Methods for Grape Endpoint
# Helper Methods for Grape Endpoint
module
HelperMethods
module
HelperMethods
def
find_current_user!
def
find_current_user!
user
=
find_user_from_access_token
||
find_user_from_
warden
||
find_user_by_job_tok
en
user
=
find_user_from_access_token
||
find_user_from_
job_token
||
find_user_from_ward
en
return
unless
user
return
unless
user
forbidden!
(
'User is blocked'
)
unless
Gitlab
::
UserAccess
.
new
(
user
).
allowed?
&&
user
.
can?
(
:access_api
)
forbidden!
(
'User is blocked'
)
unless
Gitlab
::
UserAccess
.
new
(
user
).
allowed?
&&
user
.
can?
(
:access_api
)
...
@@ -82,6 +82,20 @@ module API
...
@@ -82,6 +82,20 @@ module API
access_token
.
user
||
raise
(
UnauthorizedError
)
access_token
.
user
||
raise
(
UnauthorizedError
)
end
end
def
find_user_from_job_token
return
unless
route_authentication_setting
[
:job_token_allowed
]
token
=
(
params
[
JOB_TOKEN_PARAM
]
||
env
[
JOB_TOKEN_HEADER
]).
to_s
return
unless
token
.
present?
job
=
Ci
::
Build
.
find_by
(
token:
token
)
raise
UnauthorizedError
unless
job
@job_token_authentication
=
true
job
.
user
end
# Check the Rails session for valid authentication details
# Check the Rails session for valid authentication details
def
find_user_from_warden
def
find_user_from_warden
warden
.
try
(
:authenticate
)
if
verified_request?
warden
.
try
(
:authenticate
)
if
verified_request?
...
@@ -96,16 +110,6 @@ module API
...
@@ -96,16 +110,6 @@ module API
Gitlab
::
RequestForgeryProtection
.
verified?
(
env
)
Gitlab
::
RequestForgeryProtection
.
verified?
(
env
)
end
end
def
find_user_by_job_token
return
@user_by_job_token
if
defined?
(
@user_by_job_token
)
@user_by_job_token
=
if
route_authentication_setting
[
:job_token_allowed
]
token_string
=
params
[
JOB_TOKEN_PARAM
].
presence
||
env
[
JOB_TOKEN_HEADER
].
presence
Ci
::
Build
.
find_by_token
(
token_string
)
&
.
user
if
token_string
end
end
def
route_authentication_setting
def
route_authentication_setting
return
{}
unless
respond_to?
(
:route_setting
)
return
{}
unless
respond_to?
(
:route_setting
)
...
...
lib/api/helpers.rb
View file @
cfe1ff8b
...
@@ -408,7 +408,7 @@ module API
...
@@ -408,7 +408,7 @@ module API
end
end
def
job_token_authentication?
def
job_token_authentication?
initial_current_user
&&
initial_current_user
==
find_user_by_job_toke
n
initial_current_user
&&
@job_token_authenticatio
n
end
end
def
warden
def
warden
...
...
spec/requests/api/helpers_spec.rb
View file @
cfe1ff8b
...
@@ -182,31 +182,47 @@ describe API::Helpers do
...
@@ -182,31 +182,47 @@ describe API::Helpers do
end
end
describe
"when authenticating using a job token"
do
describe
"when authenticating using a job token"
do
let
(
:job
)
{
create
(
:ci_build
,
user:
current_user
)
}
let
(
:job
)
{
create
(
:ci_build
,
user:
user
)
}
let
(
:route_authentication_setting
)
{
{
job_token_allowed:
true
}
}
before
do
context
'when route is allowed to be authenticated'
do
allow_any_instance_of
(
described_class
).
to
receive
(
:doorkeeper_guard
).
and_return
(
nil
)
let
(
:route_authentication_setting
)
{
{
job_token_allowed:
true
}
}
end
it
"returns nil
for an invalid token"
do
it
"returns a 401 response
for an invalid token"
do
env
[
API
::
APIGuard
::
JOB_TOKEN_HEADER
]
=
'invalid token'
env
[
API
::
APIGuard
::
JOB_TOKEN_HEADER
]
=
'invalid token'
expect
(
current_user
).
to
be_nil
expect
{
current_user
}.
to
raise_error
/401/
end
end
it
"returns nil for a user without access"
do
it
"returns a 403 response for a user without access"
do
env
[
API
::
APIGuard
::
JOB_TOKEN_HEADER
]
=
job
.
token
env
[
API
::
APIGuard
::
JOB_TOKEN_HEADER
]
=
job
.
token
allow_any_instance_of
(
Gitlab
::
UserAccess
).
to
receive
(
:allowed?
).
and_return
(
false
)
allow_any_instance_of
(
Gitlab
::
UserAccess
).
to
receive
(
:allowed?
).
and_return
(
false
)
expect
{
current_user
}.
to
raise_error
/403/
end
expect
(
current_user
).
to
be_nil
it
'returns a 403 response for a user who is blocked'
do
user
.
block!
env
[
API
::
APIGuard
::
JOB_TOKEN_HEADER
]
=
job
.
token
expect
{
current_user
}.
to
raise_error
/403/
end
it
"sets current_user"
do
env
[
API
::
APIGuard
::
JOB_TOKEN_HEADER
]
=
job
.
token
expect
(
current_user
).
to
eq
(
user
)
end
end
end
it
"returns nil for a user with access, but route not allowed to be authenticated"
do
context
'when route is not allowed to be authenticated'
do
env
[
API
::
APIGuard
::
JOB_TOKEN_HEADER
]
=
job
.
token
let
(
:route_authentication_setting
)
{
{
job_token_allowed:
false
}
}
allow_any_instance_of
(
Gitlab
::
UserAccess
).
to
receive
(
:allowed?
).
and_return
(
true
)
it
"sets current_user to nil"
do
env
[
API
::
APIGuard
::
JOB_TOKEN_HEADER
]
=
job
.
token
allow_any_instance_of
(
Gitlab
::
UserAccess
).
to
receive
(
:allowed?
).
and_return
(
true
)
expect
(
current_user
).
to
be_nil
expect
(
current_user
).
to
be_nil
end
end
end
end
end
end
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment