2018-09-23 15:44:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-12-19 09:15:29 -05:00
|
|
|
class Oauth::AuthorizationsController < Doorkeeper::AuthorizationsController
|
2019-10-18 17:06:37 -04:00
|
|
|
include Gitlab::Experimentation::ControllerConcern
|
2019-12-11 07:08:10 -05:00
|
|
|
include InitializesCurrentUserMode
|
2022-02-04 07:17:40 -05:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
2019-12-11 07:08:10 -05:00
|
|
|
|
2022-05-11 11:07:26 -04:00
|
|
|
before_action :verify_confirmed_email!
|
2020-05-27 17:08:05 -04:00
|
|
|
|
2015-05-01 04:39:11 -04:00
|
|
|
layout 'profile'
|
2014-12-19 09:15:29 -05:00
|
|
|
|
2018-10-30 06:53:01 -04:00
|
|
|
# Overridden from Doorkeeper::AuthorizationsController to
|
2016-12-09 12:37:18 -05:00
|
|
|
# include the call to session.delete
|
2014-12-19 09:15:29 -05:00
|
|
|
def new
|
|
|
|
if pre_auth.authorizable?
|
|
|
|
if skip_authorization? || matching_token?
|
|
|
|
auth = authorization.authorize
|
2021-06-01 17:10:06 -04:00
|
|
|
parsed_redirect_uri = URI.parse(auth.redirect_uri)
|
2016-02-19 08:22:06 -05:00
|
|
|
session.delete(:user_return_to)
|
2021-06-01 17:10:06 -04:00
|
|
|
render "doorkeeper/authorizations/redirect", locals: { redirect_uri: parsed_redirect_uri }, layout: false
|
2014-12-19 09:15:29 -05:00
|
|
|
else
|
|
|
|
render "doorkeeper/authorizations/new"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
render "doorkeeper/authorizations/error"
|
|
|
|
end
|
|
|
|
end
|
2020-05-27 17:08:05 -04:00
|
|
|
|
2021-01-14 10:10:46 -05:00
|
|
|
private
|
|
|
|
|
2022-02-04 07:17:40 -05:00
|
|
|
def pre_auth_params
|
|
|
|
# Cannot be achieved with a before_action hook, due to the execution order.
|
|
|
|
downgrade_scopes! if action_name == 'new'
|
|
|
|
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
# limit scopes when signing in with GitLab
|
|
|
|
def downgrade_scopes!
|
2022-05-06 11:09:03 -04:00
|
|
|
return unless Feature.enabled?(:omniauth_login_minimal_scopes, current_user)
|
2022-03-24 05:07:33 -04:00
|
|
|
|
2022-02-04 07:17:40 -05:00
|
|
|
auth_type = params.delete('gl_auth_type')
|
|
|
|
return unless auth_type == 'login'
|
|
|
|
|
|
|
|
ensure_read_user_scope!
|
|
|
|
|
|
|
|
params['scope'] = Gitlab::Auth::READ_USER_SCOPE.to_s if application_has_read_user_scope?
|
|
|
|
end
|
|
|
|
|
|
|
|
# Configure the application to support read_user scope, if it already
|
|
|
|
# supports scopes with greater levels of privileges.
|
|
|
|
def ensure_read_user_scope!
|
|
|
|
return if application_has_read_user_scope?
|
|
|
|
return unless application_has_api_scope?
|
|
|
|
|
|
|
|
add_read_user_scope!
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_read_user_scope!
|
|
|
|
return unless doorkeeper_application
|
|
|
|
|
|
|
|
scopes = doorkeeper_application.scopes
|
|
|
|
scopes.add(Gitlab::Auth::READ_USER_SCOPE)
|
|
|
|
doorkeeper_application.scopes = scopes
|
|
|
|
doorkeeper_application.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def doorkeeper_application
|
|
|
|
strong_memoize(:doorkeeper_application) { ::Doorkeeper::OAuth::Client.find(params['client_id'])&.application }
|
|
|
|
end
|
|
|
|
|
|
|
|
def application_has_read_user_scope?
|
|
|
|
doorkeeper_application&.includes_scope?(Gitlab::Auth::READ_USER_SCOPE)
|
|
|
|
end
|
|
|
|
|
|
|
|
def application_has_api_scope?
|
|
|
|
doorkeeper_application&.includes_scope?(*::Gitlab::Auth::API_SCOPES)
|
|
|
|
end
|
|
|
|
|
2020-05-27 17:08:05 -04:00
|
|
|
def verify_confirmed_email!
|
|
|
|
return if current_user&.confirmed?
|
|
|
|
|
|
|
|
pre_auth.error = :unconfirmed_email
|
|
|
|
render "doorkeeper/authorizations/error"
|
|
|
|
end
|
2014-12-19 09:15:29 -05:00
|
|
|
end
|