2018-11-29 20:51:05 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# == SessionlessAuthentication
|
|
|
|
#
|
2019-12-04 20:38:33 +05:30
|
|
|
# Controller concern to handle PAT, RSS, and static objects token authentication methods
|
2018-11-29 20:51:05 +05:30
|
|
|
#
|
|
|
|
module SessionlessAuthentication
|
2019-12-04 20:38:33 +05:30
|
|
|
# This filter handles personal access tokens, atom requests with rss tokens, and static object tokens
|
2018-11-29 20:51:05 +05:30
|
|
|
def authenticate_sessionless_user!(request_format)
|
2021-04-28 17:22:55 +05:30
|
|
|
user = request_authenticator.find_sessionless_user(request_format)
|
2018-11-29 20:51:05 +05:30
|
|
|
sessionless_sign_in(user) if user
|
|
|
|
end
|
|
|
|
|
2021-04-28 17:22:55 +05:30
|
|
|
def request_authenticator
|
|
|
|
@request_authenticator ||= Gitlab::Auth::RequestAuthenticator.new(request)
|
|
|
|
end
|
|
|
|
|
2018-11-29 20:51:05 +05:30
|
|
|
def sessionless_user?
|
2019-10-12 21:52:04 +05:30
|
|
|
current_user && !session.key?('warden.user.user.key')
|
2018-11-29 20:51:05 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def sessionless_sign_in(user)
|
2022-03-02 08:16:31 +05:30
|
|
|
if user.can_log_in_with_non_expired_password?
|
2018-11-29 20:51:05 +05:30
|
|
|
# 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)
|
2022-05-07 20:08:51 +05:30
|
|
|
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)
|
2018-11-29 20:51:05 +05:30
|
|
|
end
|
|
|
|
end
|
2019-12-21 20:55:43 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
def sessionless_bypass_admin_mode!(&block)
|
2021-04-29 21:17:54 +05:30
|
|
|
return yield unless Gitlab::CurrentSettings.admin_mode
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
Gitlab::Auth::CurrentUserMode.bypass_session!(current_user.id, &block)
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
2018-11-29 20:51:05 +05:30
|
|
|
end
|