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
0
Merge Requests
0
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
Léo-Paul Géneau
gitlab-ce
Commits
79e4bb8d
Commit
79e4bb8d
authored
Sep 14, 2016
by
Kamil Trzcinski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor Gitlab::Auth to simplify the data flow
parent
11337217
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
34 deletions
+40
-34
lib/gitlab/auth.rb
lib/gitlab/auth.rb
+40
-34
No files found.
lib/gitlab/auth.rb
View file @
79e4bb8d
module
Gitlab
module
Auth
Result
=
Struct
.
new
(
:user
,
:project
,
:type
,
:capabilities
)
class
Result
attr_reader
:user
,
:project
,
:type
,
:capabilities
def
initialize?
(
user
=
nil
,
project
=
nil
,
type
=
nil
,
capabilities
=
nil
)
@user
,
@project
,
@type
,
@capabilities
=
user
,
project
,
type
,
capabilities
end
def
success?
user
.
present?
||
[
:ci
,
:missing_personal_token
].
include?
(
type
)
end
end
class
<<
self
def
find_for_git_client
(
login
,
password
,
project
:,
ip
:)
raise
"Must provide an IP for rate limiting"
if
ip
.
nil?
result
=
Result
.
new
if
valid_ci_request?
(
login
,
password
,
project
)
result
=
Result
.
new
(
nil
,
project
,
:ci
,
restricted_capabilities
)
else
result
=
populate_result
(
login
,
password
)
end
result
=
service_access_token_check
(
login
,
password
,
project
)
||
build_access_token_check
(
login
,
password
)
||
user_with_password_for_git
(
login
,
password
)
||
oauth_access_token_check
(
login
,
password
)
||
personal_access_token_check
(
login
,
password
)
||
Result
.
new
success
=
result
.
user
.
present?
||
[
:ci
,
:missing_personal_token
].
include?
(
result
.
type
)
rate_limit!
(
ip
,
success:
success
,
login:
login
)
rate_limit!
(
ip
,
success:
result
.
success?
,
login:
login
)
result
end
...
...
@@ -57,10 +65,10 @@ module Gitlab
private
def
valid_ci_request?
(
login
,
password
,
project
)
def
service_access_token_check
(
login
,
password
,
project
)
matched_login
=
/(?<service>^[a-zA-Z]*-ci)-token$/
.
match
(
login
)
return
false
unless
project
&&
matched_login
.
present?
return
unless
project
&&
matched_login
.
present?
underscored_service
=
matched_login
[
'service'
].
underscore
...
...
@@ -69,31 +77,24 @@ module Gitlab
# in the Service.available_services_names whitelist.
service
=
project
.
public_send
(
"
#{
underscored_service
}
_service"
)
service
&&
service
.
activated?
&&
service
.
valid_token?
(
password
)
end
end
def
populate_result
(
login
,
password
)
result
=
build_access_token_check
(
login
,
password
)
||
user_with_password_for_git
(
login
,
password
)
||
oauth_access_token_check
(
login
,
password
)
||
personal_access_token_check
(
login
,
password
)
if
result
result
.
type
=
nil
unless
result
.
capabilities
if
result
.
user
&&
result
.
user
.
two_factor_enabled?
&&
result
.
type
==
:gitlab_or_ldap
result
.
type
=
:missing_personal_token
if
service
&&
service
.
activated?
&&
service
.
valid_token?
(
password
)
Result
.
new
(
nil
,
project
,
:ci
,
restricted_capabilities
)
end
end
result
||
Result
.
new
end
def
user_with_password_for_git
(
login
,
password
)
user
=
find_with_user_password
(
login
,
password
)
Result
.
new
(
user
,
:gitlab_or_ldap
,
nil
,
full_capabilities
)
if
user
return
unless
user
type
=
if
user
.
two_factor_enabled?
:missing_personal_token
else
:gitlab_or_ldap
end
Result
.
new
(
user
,
type
,
nil
,
full_capabilities
)
end
def
oauth_access_token_check
(
login
,
password
)
...
...
@@ -101,7 +102,7 @@ module Gitlab
token
=
Doorkeeper
::
AccessToken
.
by_token
(
password
)
if
token
&&
token
.
accessible?
user
=
User
.
find_by
(
id:
token
.
resource_owner_id
)
Result
.
new
(
user
,
nil
,
:oauth
,
full
_capabilities
)
Result
.
new
(
user
,
nil
,
:oauth
,
read
_capabilities
)
end
end
end
...
...
@@ -140,11 +141,16 @@ module Gitlab
]
end
def
full
_capabilities
def
read
_capabilities
restricted_capabilities
+
[
:download_code
,
:read_container_image
]
end
def
full_capabilities
read_capabilities
+
[
:push_code
,
:read_container_image
,
:update_container_image
]
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