2018-11-28 14:06:02 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# == SessionlessAuthentication
|
|
|
|
#
|
2019-07-22 10:56:40 -04:00
|
|
|
# Controller concern to handle PAT, RSS, and static objects token authentication methods
|
2018-11-28 14:06:02 -05:00
|
|
|
#
|
|
|
|
module SessionlessAuthentication
|
2019-07-22 10:56:40 -04:00
|
|
|
# This filter handles personal access tokens, atom requests with rss tokens, and static object tokens
|
2018-11-28 14:06:02 -05:00
|
|
|
def authenticate_sessionless_user!(request_format)
|
2021-04-28 11:09:35 -04:00
|
|
|
user = request_authenticator.find_sessionless_user(request_format)
|
2018-11-28 14:06:02 -05:00
|
|
|
sessionless_sign_in(user) if user
|
|
|
|
end
|
|
|
|
|
2021-04-28 11:09:35 -04:00
|
|
|
def request_authenticator
|
|
|
|
@request_authenticator ||= Gitlab::Auth::RequestAuthenticator.new(request)
|
|
|
|
end
|
|
|
|
|
2018-11-28 14:06:02 -05:00
|
|
|
def sessionless_user?
|
2019-07-24 14:11:18 -04:00
|
|
|
current_user && !session.key?('warden.user.user.key')
|
2018-11-28 14:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def sessionless_sign_in(user)
|
2022-03-23 02:07:44 -04:00
|
|
|
signed_in_user =
|
|
|
|
if user.can_log_in_with_non_expired_password?
|
|
|
|
# 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)
|
|
|
|
elsif request_authenticator.can_sign_in_bot?(user)
|
|
|
|
# we suppress callbacks to avoid redirecting the bot
|
|
|
|
sign_in(user, store: false, message: :sessionless_sign_in, run_callbacks: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
reset_auth_user! if respond_to?(:reset_auth_user!, true)
|
|
|
|
|
|
|
|
signed_in_user
|
2018-11-28 14:06:02 -05:00
|
|
|
end
|
2019-09-26 08:06:00 -04:00
|
|
|
|
2020-03-02 07:07:57 -05:00
|
|
|
def sessionless_bypass_admin_mode!(&block)
|
2021-03-17 20:08:58 -04:00
|
|
|
return yield unless Gitlab::CurrentSettings.admin_mode
|
2019-12-11 07:08:10 -05:00
|
|
|
|
2020-03-02 07:07:57 -05:00
|
|
|
Gitlab::Auth::CurrentUserMode.bypass_session!(current_user.id, &block)
|
2019-09-26 08:06:00 -04:00
|
|
|
end
|
2018-11-28 14:06:02 -05:00
|
|
|
end
|