summaryrefslogtreecommitdiff
path: root/lib/gitlab/api_authentication/token_locator.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/gitlab/api_authentication/token_locator.rb')
-rw-r--r--lib/gitlab/api_authentication/token_locator.rb37
1 files changed, 37 insertions, 0 deletions
diff --git a/lib/gitlab/api_authentication/token_locator.rb b/lib/gitlab/api_authentication/token_locator.rb
new file mode 100644
index 00000000000..32a98908e5b
--- /dev/null
+++ b/lib/gitlab/api_authentication/token_locator.rb
@@ -0,0 +1,37 @@
+# frozen_string_literal: true
+
+module Gitlab
+ module APIAuthentication
+ class TokenLocator
+ UsernameAndPassword = Struct.new(:username, :password)
+
+ include ActiveModel::Validations
+ include ActionController::HttpAuthentication::Basic
+
+ attr_reader :location
+
+ validates :location, inclusion: { in: %i[http_basic_auth] }
+
+ def initialize(location)
+ @location = location
+ validate!
+ end
+
+ def extract(request)
+ case @location
+ when :http_basic_auth
+ extract_from_http_basic_auth request
+ end
+ end
+
+ private
+
+ def extract_from_http_basic_auth(request)
+ username, password = user_name_and_password(request)
+ return unless username.present? && password.present?
+
+ UsernameAndPassword.new(username, password)
+ end
+ end
+ end
+end