gitlab-org--gitlab-foss/app/controllers/concerns/sessionless_authentication.rb
Stan Hu 3e001d29cc Enable Rubocop Performance/InefficientHashSearch
When used with a Hash, `.keys.include?` is bad because:

1. It performs a O(n) search instead of the efficient `.has_key?`
2. It clones all keys into separate array.

Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/64975
2019-07-24 11:47:58 -07:00

28 lines
932 B
Ruby

# frozen_string_literal: true
# == SessionlessAuthentication
#
# Controller concern to handle PAT and RSS token authentication methods
#
module SessionlessAuthentication
# This filter handles personal access tokens, and atom requests with rss tokens
def authenticate_sessionless_user!(request_format)
user = Gitlab::Auth::RequestAuthenticator.new(request).find_sessionless_user(request_format)
sessionless_sign_in(user) if user
end
def sessionless_user?
current_user && !session.key?('warden.user.user.key')
end
def sessionless_sign_in(user)
if user && can?(user, :log_in)
# Notice we are passing store false, so the user is not
# actually stored in the session and a token is needed
# for every request. If you want the token to work as a
# sign in token, you can simply remove store: false.
sign_in(user, store: false, message: :sessionless_sign_in)
end
end
end