Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDouwe Maan <douwe@selenight.nl>2017-10-12 15:38:39 +0300
committerDouwe Maan <douwe@selenight.nl>2017-11-02 13:39:03 +0300
commit3f24f9ed182f5226210349b8e67e484e132ce971 (patch)
tree6f6a9148f3d89ea2d19cefe90470a8dca4dabc64 /lib/api/api_guard.rb
parenta1781a49416790f727b3dd3453bf704723e72b90 (diff)
Add sudo API scope
Diffstat (limited to 'lib/api/api_guard.rb')
-rw-r--r--lib/api/api_guard.rb104
1 files changed, 38 insertions, 66 deletions
diff --git a/lib/api/api_guard.rb b/lib/api/api_guard.rb
index 0ff376bbab6..b9c7d443f6c 100644
--- a/lib/api/api_guard.rb
+++ b/lib/api/api_guard.rb
@@ -42,62 +42,42 @@ module API
# Helper Methods for Grape Endpoint
module HelperMethods
- def find_current_user
- user =
- find_user_from_personal_access_token ||
- find_user_from_oauth_token ||
- find_user_from_warden
+ def find_current_user!
+ user = find_user_from_access_token || find_user_from_warden
+ return unless user
- return nil unless user
-
- raise UnauthorizedError unless Gitlab::UserAccess.new(user).allowed? && user.can?(:access_api)
+ forbidden!('User is blocked') unless Gitlab::UserAccess.new(user).allowed? && user.can?(:access_api)
user
end
- def private_token
- params[PRIVATE_TOKEN_PARAM] || env[PRIVATE_TOKEN_HEADER]
- end
-
- private
-
- def find_user_from_personal_access_token
- token_string = private_token.to_s
- return nil unless token_string.present?
+ def access_token
+ return @access_token if defined?(@access_token)
- access_token = PersonalAccessToken.find_by_token(token_string)
- raise UnauthorizedError unless access_token
-
- user = find_user_by_access_token(access_token)
+ @access_token = find_oauth_access_token || find_personal_access_token
+ end
- raise UnauthorizedError unless user
+ def validate_access_token!(scopes: [])
+ return unless access_token
- user
+ case AccessTokenValidationService.new(access_token, request: request).validate(scopes: scopes)
+ when AccessTokenValidationService::INSUFFICIENT_SCOPE
+ raise InsufficientScopeError.new(scopes)
+ when AccessTokenValidationService::EXPIRED
+ raise ExpiredError
+ when AccessTokenValidationService::REVOKED
+ raise RevokedError
+ end
end
- # Invokes the doorkeeper guard.
- #
- # If token is presented and valid, then it sets @current_user.
- #
- # If the token does not have sufficient scopes to cover the requred scopes,
- # then it raises InsufficientScopeError.
- #
- # If the token is expired, then it raises ExpiredError.
- #
- # If the token is revoked, then it raises RevokedError.
- #
- # If the token is not found (nil), then it returns nil
- #
- # Arguments:
- #
- # scopes: (optional) scopes required for this guard.
- # Defaults to empty array.
- #
- def find_user_from_oauth_token
- access_token = find_oauth_access_token
+ private
+
+ def find_user_from_access_token
return unless access_token
- find_user_by_access_token(access_token)
+ validate_access_token!
+
+ access_token.user || raise(UnauthorizedError)
end
# Check the Rails session for valid authentication details
@@ -115,34 +95,26 @@ module API
end
def find_oauth_access_token
- return @oauth_access_token if defined?(@oauth_access_token)
-
token = Doorkeeper::OAuth::Token.from_request(doorkeeper_request, *Doorkeeper.configuration.access_token_methods)
- return @oauth_access_token = nil unless token
+ return unless token
- @oauth_access_token = OauthAccessToken.by_token(token)
- raise UnauthorizedError unless @oauth_access_token
+ # Expiration, revocation and scopes are verified in `find_user_by_access_token`
+ access_token = OauthAccessToken.by_token(token)
+ raise UnauthorizedError unless access_token
- @oauth_access_token.revoke_previous_refresh_token!
- @oauth_access_token
+ access_token.revoke_previous_refresh_token!
+ access_token
end
- def find_user_by_access_token(access_token)
- scopes = scopes_registered_for_endpoint
+ def find_personal_access_token
+ token = (params[PRIVATE_TOKEN_PARAM] || env[PRIVATE_TOKEN_HEADER]).to_s
+ return unless token.present?
- case AccessTokenValidationService.new(access_token, request: request).validate(scopes: scopes)
- when AccessTokenValidationService::INSUFFICIENT_SCOPE
- raise InsufficientScopeError.new(scopes)
-
- when AccessTokenValidationService::EXPIRED
- raise ExpiredError
-
- when AccessTokenValidationService::REVOKED
- raise RevokedError
+ # Expiration, revocation and scopes are verified in `find_user_by_access_token`
+ access_token = PersonalAccessToken.find_by(token: token)
+ raise UnauthorizedError unless access_token
- when AccessTokenValidationService::VALID
- access_token.user
- end
+ access_token
end
def doorkeeper_request
@@ -226,7 +198,7 @@ module API
class InsufficientScopeError < StandardError
attr_reader :scopes
def initialize(scopes)
- @scopes = scopes
+ @scopes = scopes.map { |s| s.try(:name) || s }
end
end
end