2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2017-03-09 11:44:51 +00:00
|
|
|
module ApplicationController::Authenticates
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def permission_check(key)
|
2020-03-19 09:39:51 +00:00
|
|
|
ActiveSupport::Deprecation.warn("Method 'permission_check' is deprecated. Use Pundit policy and `authorize!` instead.")
|
|
|
|
|
2017-03-09 11:44:51 +00:00
|
|
|
if @_token_auth
|
|
|
|
user = Token.check(
|
2018-12-19 17:31:51 +00:00
|
|
|
action: 'api',
|
|
|
|
name: @_token_auth,
|
2017-03-09 11:44:51 +00:00
|
|
|
permission: key,
|
|
|
|
)
|
|
|
|
return false if user
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2021-11-15 15:58:19 +00:00
|
|
|
raise Exceptions::Forbidden, __('Not authorized (token)!')
|
2017-03-09 11:44:51 +00:00
|
|
|
end
|
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
return false if current_user&.permissions?(key)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2021-11-15 15:58:19 +00:00
|
|
|
raise Exceptions::Forbidden, __('Not authorized (user)!')
|
2017-03-09 11:44:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def authentication_check(auth_param = {})
|
|
|
|
user = authentication_check_only(auth_param)
|
|
|
|
|
|
|
|
# check if basic_auth fallback is possible
|
|
|
|
if auth_param[:basic_auth_promt] && !user
|
2020-05-25 16:26:06 +00:00
|
|
|
request_http_basic_authentication
|
|
|
|
return false
|
2017-03-09 11:44:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# return auth not ok
|
|
|
|
if !user
|
2021-11-15 15:58:19 +00:00
|
|
|
raise Exceptions::Forbidden, __('Authentication required')
|
2017-03-09 11:44:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# return auth ok
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def authentication_check_only(auth_param = {})
|
2021-11-04 13:40:58 +00:00
|
|
|
if Rails.env.test? && ENV['FAKE_SELENIUM_LOGIN_USER_ID'].present? && session[:user_id].blank?
|
|
|
|
session[:user_id] = ENV['FAKE_SELENIUM_LOGIN_USER_ID'].to_i
|
|
|
|
end
|
|
|
|
|
2021-07-16 13:44:10 +00:00
|
|
|
# logger.debug 'authentication_check'
|
|
|
|
# logger.debug params.inspect
|
|
|
|
# logger.debug session.inspect
|
|
|
|
# logger.debug cookies.inspect
|
2021-02-04 08:28:41 +00:00
|
|
|
authentication_errors = []
|
2017-03-09 11:44:51 +00:00
|
|
|
|
|
|
|
# already logged in, early exit
|
|
|
|
if session.id && session[:user_id]
|
2018-03-20 17:47:49 +00:00
|
|
|
logger.debug { 'session based auth check' }
|
2017-03-09 11:44:51 +00:00
|
|
|
user = User.lookup(id: session[:user_id])
|
|
|
|
return authentication_check_prerequesits(user, 'session', auth_param) if user
|
2021-02-04 08:28:41 +00:00
|
|
|
|
|
|
|
authentication_errors.push("Can't find User with ID #{session[:user_id]} from Session")
|
2017-03-09 11:44:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# check http basic based authentication
|
|
|
|
authenticate_with_http_basic do |username, password|
|
|
|
|
request.session_options[:skip] = true # do not send a session cookie
|
2018-03-20 17:47:49 +00:00
|
|
|
logger.debug { "http basic auth check '#{username}'" }
|
2017-03-09 11:44:51 +00:00
|
|
|
if Setting.get('api_password_access') == false
|
2021-02-04 08:28:41 +00:00
|
|
|
raise Exceptions::Forbidden, 'API password access disabled!'
|
2017-03-09 11:44:51 +00:00
|
|
|
end
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2021-08-16 06:49:32 +00:00
|
|
|
auth = Auth.new(username, password)
|
|
|
|
return authentication_check_prerequesits(auth.user, 'basic_auth', auth_param) if auth.valid?
|
2021-02-04 08:28:41 +00:00
|
|
|
|
2021-11-15 15:58:19 +00:00
|
|
|
authentication_errors.push(__('Invalid BasicAuth credentials'))
|
2017-03-09 11:44:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# check http token based authentication
|
|
|
|
authenticate_with_http_token do |token_string, _options|
|
2018-03-20 17:47:49 +00:00
|
|
|
logger.debug { "http token auth check '#{token_string}'" }
|
2017-03-09 11:44:51 +00:00
|
|
|
request.session_options[:skip] = true # do not send a session cookie
|
|
|
|
if Setting.get('api_token_access') == false
|
2021-02-04 08:28:41 +00:00
|
|
|
raise Exceptions::Forbidden, 'API token access disabled!'
|
2017-03-09 11:44:51 +00:00
|
|
|
end
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-03-09 11:44:51 +00:00
|
|
|
user = Token.check(
|
2018-12-19 17:31:51 +00:00
|
|
|
action: 'api',
|
|
|
|
name: token_string,
|
2017-03-09 11:44:51 +00:00
|
|
|
inactive_user: true,
|
|
|
|
)
|
|
|
|
if user && auth_param[:permission]
|
2020-03-19 09:39:51 +00:00
|
|
|
ActiveSupport::Deprecation.warn("Paramter ':permission' is deprecated. Use Pundit policy and `authorize!` instead.")
|
|
|
|
|
2017-03-09 11:44:51 +00:00
|
|
|
user = Token.check(
|
2018-12-19 17:31:51 +00:00
|
|
|
action: 'api',
|
|
|
|
name: token_string,
|
|
|
|
permission: auth_param[:permission],
|
2017-03-09 11:44:51 +00:00
|
|
|
inactive_user: true,
|
|
|
|
)
|
2021-11-15 15:58:19 +00:00
|
|
|
raise Exceptions::NotAuthorized, __('Not authorized (token)!') if !user
|
2017-03-09 11:44:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if user
|
|
|
|
token = Token.find_by(name: token_string)
|
|
|
|
|
|
|
|
token.last_used_at = Time.zone.now
|
|
|
|
token.save!
|
|
|
|
|
|
|
|
if token.expires_at &&
|
|
|
|
Time.zone.today >= token.expires_at
|
2021-11-15 15:58:19 +00:00
|
|
|
raise Exceptions::NotAuthorized, __('Not authorized (token expired)!')
|
2017-03-09 11:44:51 +00:00
|
|
|
end
|
2020-03-19 09:39:51 +00:00
|
|
|
|
|
|
|
@_token = token # remember for Pundit authorization / permit!
|
2017-03-09 11:44:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@_token_auth = token_string # remember for permission_check
|
|
|
|
return authentication_check_prerequesits(user, 'token_auth', auth_param) if user
|
2021-02-04 08:28:41 +00:00
|
|
|
|
2021-11-15 15:58:19 +00:00
|
|
|
authentication_errors.push(__("Can't find User for Token"))
|
2017-03-09 11:44:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# check oauth2 token based authentication
|
|
|
|
token = Doorkeeper::OAuth::Token.from_bearer_authorization(request)
|
|
|
|
if token
|
|
|
|
request.session_options[:skip] = true # do not send a session cookie
|
2021-02-04 08:28:41 +00:00
|
|
|
logger.debug { "OAuth2 token auth check '#{token}'" }
|
2017-03-09 11:44:51 +00:00
|
|
|
access_token = Doorkeeper::AccessToken.by_token(token)
|
|
|
|
|
2021-11-15 15:58:19 +00:00
|
|
|
raise Exceptions::NotAuthorized, __('Invalid token!') if !access_token
|
2017-03-09 11:44:51 +00:00
|
|
|
|
|
|
|
# check expire
|
|
|
|
if access_token.expires_in && (access_token.created_at + access_token.expires_in) < Time.zone.now
|
2021-11-15 15:58:19 +00:00
|
|
|
raise Exceptions::NotAuthorized, __('OAuth2 token is expired!')
|
2017-03-09 11:44:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# if access_token.scopes.empty?
|
|
|
|
# raise Exceptions::NotAuthorized, 'OAuth2 scope missing for token!'
|
|
|
|
# end
|
|
|
|
|
|
|
|
user = User.find(access_token.resource_owner_id)
|
|
|
|
return authentication_check_prerequesits(user, 'token_auth', auth_param) if user
|
2021-02-04 08:28:41 +00:00
|
|
|
|
|
|
|
authentication_errors.push("Can't find User with ID #{access_token.resource_owner_id} for OAuth2 token")
|
2017-03-09 11:44:51 +00:00
|
|
|
end
|
|
|
|
|
2021-02-04 08:28:41 +00:00
|
|
|
return false if authentication_errors.blank?
|
|
|
|
|
|
|
|
raise Exceptions::NotAuthorized, authentication_errors.join(', ')
|
2017-03-09 11:44:51 +00:00
|
|
|
end
|
|
|
|
|
2019-09-05 14:02:31 +00:00
|
|
|
def authentication_check_prerequesits(user, auth_type, auth_param)
|
2021-11-15 15:58:19 +00:00
|
|
|
raise Exceptions::Forbidden, __('Maintenance mode enabled!') if in_maintenance_mode?(user)
|
2020-08-14 12:52:20 +00:00
|
|
|
|
|
|
|
raise_unified_login_error if !user.active
|
2020-03-19 09:39:51 +00:00
|
|
|
|
|
|
|
if auth_param[:permission]
|
|
|
|
ActiveSupport::Deprecation.warn("Parameter ':permission' is deprecated. Use Pundit policy and `authorize!` instead.")
|
|
|
|
|
|
|
|
if !user.permissions?(auth_param[:permission])
|
2021-11-15 15:58:19 +00:00
|
|
|
raise Exceptions::Forbidden, __('Not authorized (user)!')
|
2020-03-19 09:39:51 +00:00
|
|
|
end
|
|
|
|
end
|
2017-03-09 11:44:51 +00:00
|
|
|
|
|
|
|
current_user_set(user, auth_type)
|
|
|
|
user_device_log(user, auth_type)
|
2018-03-20 17:47:49 +00:00
|
|
|
logger.debug { "#{auth_type} for '#{user.login}'" }
|
2019-09-05 14:02:31 +00:00
|
|
|
user
|
2017-03-09 11:44:51 +00:00
|
|
|
end
|
2020-08-14 12:52:20 +00:00
|
|
|
|
|
|
|
def raise_unified_login_error
|
2021-11-15 15:58:19 +00:00
|
|
|
raise Exceptions::NotAuthorized, __('Login failed. Have you double-checked your credentials and completed the email verification step?')
|
2020-08-14 12:52:20 +00:00
|
|
|
end
|
2017-03-09 11:44:51 +00:00
|
|
|
end
|