Skip to content

Commit

Permalink
Refactor Application#authenticate_using_access_tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
lfdebrux committed Dec 19, 2023
1 parent 5445024 commit c09f41e
Showing 1 changed file with 4 additions and 15 deletions.
19 changes: 4 additions & 15 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class ApplicationController < ActionController::API
include ActionController::HttpAuthentication::Token::ControllerMethods
include ActionController::HttpAuthentication::Token

rescue_from ActiveRecord::RecordNotFound, with: :not_found
rescue_from ActiveRecord::RecordInvalid, with: :invalid_record
Expand Down Expand Up @@ -38,22 +38,11 @@ def authenticate_using_old_env_vars
end

def authenticate_using_access_tokens
if request.headers["X-Api-Token"].present?
token = request.headers["X-Api-Token"]
(request.headers["X-Api-Token"].presence || token_and_options(request)&.first).try! do |token|
@access_token = AccessToken.active.find_by_token_digest(Digest::SHA256.hexdigest(token))
if @access_token.present? && AccessTokenPolicy.new(@access_token, request).request?
@access_token.update!(last_accessed_at: Time.zone.now)
true
else
false
end
else
authenticate_with_http_token do |token|
@access_token = AccessToken.active.find_by_token_digest(Digest::SHA256.hexdigest(token))
return unless @access_token.present? && AccessTokenPolicy.new(@access_token, request).request?
return nil unless @access_token.present? && AccessTokenPolicy.new(@access_token, request).request?

@access_token.update!(last_accessed_at: Time.zone.now)
end
@access_token.update!(last_accessed_at: Time.zone.now)
end
end

Expand Down

0 comments on commit c09f41e

Please sign in to comment.