Skip to content

Commit 29af0e6

Browse files
committed
Refactor Application#authenticate_using_access_tokens
1 parent 61efb37 commit 29af0e6

File tree

1 file changed

+7
-17
lines changed

1 file changed

+7
-17
lines changed

app/controllers/application_controller.rb

+7-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class ApplicationController < ActionController::API
2-
include ActionController::HttpAuthentication::Token::ControllerMethods
2+
include ActionController::HttpAuthentication::Token
33

44
rescue_from ActiveRecord::RecordNotFound, with: :not_found
55
rescue_from ActiveRecord::RecordInvalid, with: :invalid_record
@@ -38,23 +38,13 @@ def authenticate_using_old_env_vars
3838
end
3939

4040
def authenticate_using_access_tokens
41-
if request.headers["X-Api-Token"].present?
42-
token = request.headers["X-Api-Token"]
43-
@access_token = AccessToken.active.find_by_token_digest(Digest::SHA256.hexdigest(token))
44-
if @access_token.present? && AccessTokenPolicy.new(@access_token, request).request?
45-
@access_token.update!(last_accessed_at: Time.zone.now)
46-
true
47-
else
48-
false
49-
end
50-
else
51-
authenticate_with_http_token do |token|
52-
@access_token = AccessToken.active.find_by_token_digest(Digest::SHA256.hexdigest(token))
53-
return unless @access_token.present? && AccessTokenPolicy.new(@access_token, request).request?
41+
token = (request.headers["X-Api-Token"].presence || token_and_options(request)&.first)
42+
return if token.blank?
5443

55-
@access_token.update!(last_accessed_at: Time.zone.now)
56-
end
57-
end
44+
@access_token = AccessToken.active.find_by_token_digest(Digest::SHA256.hexdigest(token))
45+
return unless @access_token.present? && AccessTokenPolicy.new(@access_token, request).request?
46+
47+
@access_token.update!(last_accessed_at: Time.zone.now)
5848
end
5949

6050
def not_found

0 commit comments

Comments
 (0)