summaryrefslogtreecommitdiff
path: root/lib/gitlab/api_authentication/token_locator.rb
diff options
context:
space:
mode:
authorRobert Speicher <rspeicher@gmail.com>2021-01-20 13:34:23 -0600
committerRobert Speicher <rspeicher@gmail.com>2021-01-20 13:34:23 -0600
commit6438df3a1e0fb944485cebf07976160184697d72 (patch)
tree00b09bfd170e77ae9391b1a2f5a93ef6839f2597 /lib/gitlab/api_authentication/token_locator.rb
parent42bcd54d971da7ef2854b896a7b34f4ef8601067 (diff)
downloadgitlab-ce-6438df3a1e0fb944485cebf07976160184697d72.tar.gz
Add latest changes from gitlab-org/gitlab@13-8-stable-eev13.8.0-rc42
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